Friday, March 20, 2009

Inner Classes

Inner Classes gives us a good introductions. Inner classes come in four flavors:: Static member class, Member class, Local class, Anonymous class. And the advantages of inner classes can be divided into three categories: an object-oriented advantage, an organizational advantage, and a call-back advantage.

The OO advantage is not well known for me compared with call-back advantage. One good example is to separate search algorithm as inner class in Tree class. Another is to nest MuListElement class inside BeastGetActiveMuRespMsg - composition relationship in UML.


public class BeastGetActiveMuRespMsg {
private int retCode;
private MuListElement[] muList;
....
public static class MuListElement {
private String macAddress;
private String ipAddress;
...
}
}

A static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.

Like a static member class, a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.

Tuesday, March 17, 2009

Javascript/css/image loading

In Q&A-Javascript Execution & Onload Techniques in Web Browsers, It states that "Inline and external scripts are executed in the order in which they appear in an (x)HTML document…It should be noted that the loading of an (x)HTML page is halted while loading external JavaScript files."

In 14 Rules for Faster-Loading Web Sites, Rule 6 “Put script at the bottom” is used to avoid such halting. See Example.

Other resources (css/images) are loaded asynchronously and you cannot be certain when they will complete.

onload vs onDomReady
The onload event fires when the document and it's script/style/image resources are loaded, but you probably don't want to wait for images if you are doing any javascript when the page is loaded. Also, it may cause UI errors if the user tries to interact with the page before the JavaScript event handlers have been applied to the DOM via the onload event - a limitation of onload event. Instead, use something like jQuery's "ready" event or fire your own "DOMReady" event by placing a script tag at the end of the body:


<body>
<!-- your page contents here -->
<script type="text/javascript">
// DOM is ready, do scripty stuff now
DOMReady();
</script>
</body>

Single threaded script processing
The client-side javascript is single threaded. This means that the parsing of (x)HTML documents stops while scripts are loaded and executed, and that web browsers stop responding to user input while event handlers are being executed.

This is a good thing, and a bad thing. It's good because you can rest comfortable knowing that two event handlers will never run at the same time. Also, you can manipulate document content knowing that no other thread is attempting to modify the same piece of content.

It's bad in that intensive scripts can hold up the user experience by causing periods of unresponsiveness from a JavaScript driven UI.