Posts

Showing posts from 2009

Prefer primitives over boxed primitives in java

This blog is one of the item in famous "Effective Java 2/e" by Joshua Bloch.

In java, we got a flexibility of auto boxing and auto-unboxing of Java primitives like int,long,boolean etc..,

int i=0;
Integer j = i;

In the above example i is autoboxed to j and the vice-versa is also possible.

Consider the following example:
public class TestClass {
private static Integer i;
public static void main(String[] args) {

if(i == 42) {
System.out.println("Unbelievable");
}
}
}
If the above program is executed, what do you expect? it prints "unbelievable" ? no it wiil not but it behaves as strange are printing "unbelievable". It throws NullPointerException !!.

What might be the reason? well this is caused because of one of the side effects of famous auto-boxing. When ever a primitive and boxed primitive involves in an operation, then auto-unboxing of boxed primitive happens.

In the above example, i will be auto unboxed and it is a null reference so it throw…

Be careful before setting innerHTML

When working with javascript, the most common thing a developer will do for adding content by setting innerHTML of container element.

ele.innerHTML ='<div><span>This is working fine</span>';
if(user == 'amar') {
ele.innerHTML += '<span>Hello amar welcome to your blog!</span>';
}

ele.innerHTML+='</div>';

Above code seems to be ok but there is a big mistake. Element's innerHTML is set and the div is not closed before evaluating the condition. Browser closes the div at the point and the span that is added in the condition will be sibling of div instead of child !!.

This problem can be solved by using a variable, adding the content to that variable and finally setting innerHTML to the element.

Enhancing user experience by improving website performance

From my experience I device some points and tools used in the development of the website.Any website's user experience depends on the speed of data transfer( including images, js and actual page content), amount of the data transferred and number of requests made.
Caching the static content : All the static files like js, css and images can be cached by the browser so that the same content will not be requested and thus reducing the data transferred through wire. Server can tell the browser to cache the content by setting the following headers. ( Tested in chrome, IE, and firefox). Expires header (setting near future expires header with one year from current time) Cache control header (Cache-control header (public, max-age=)Adding these headers will permanently cache the files on the client side. Versioning of the static content is must for controlling of the data flow between client and server. Versioning of the static implies including version number in the url which should…

Returning content type of 'application/json' using jQuery ajax form

Ajax form in the jQuery is a wonderful feature for the developers as it allows multi-part upload without refreshing the page.
Ajax form call creates a iframe which is invisible and then submits the form from with in iframe. Browser checks for the content type of the returned content and verifies whether it can handle or not before passing content to the iframe.
If the response content type is appliction/json, the browser donot know how to handle the content and opens popup to save the content instead of handling to the iframe, the source of the request.
So basically, ajax form of jQuery cannot be used for api calls returning content type of application/json which is a major issue while working with REST API calls that accepts multi-part/formdata requests and return content of type application/json.
Alternatives : Return content of type application/xml and parse the same on the client side.Use servelts to handle the multi-part request and return json string form of the object you wish to s…