Tech Tips Digest: 5

It's been a while since I last posted anything, but you may excuse me once you know that I've extremely busy lately.

Now let's get down to business ;-)

Previous Tips: Tip No. 1 | Tip No. 2 | Tip No. 3 | Tip No. 4

Tech Tip No26: Minimize exit points of your methods (functions), yet better try to have one exit point, this makes tracing easier

When tracing methods or functions (or routines! whatever you want to call them) it's usually best to have at most one exist point.

This will help you better understand where the method breaks, and know exactly how it will behave under most circumstances.

Imagine having a method spanning twenty lines or more, and having four or five exit points, one being a thrown exception and the rest being normal return statements, tracing it would be a nasty job!

Below are examples of a simple method having multiple exist points, and how it may be enhanced

public int example(final int input) {
    if (input == 1) {
        return 11;
    } else if (input == 2) {
        return 22;
    }
}

And now the better version:

public int example(final int input) {
    int result = 0;

    if (input == 1) {
        result = 11;
    } else if (input == 2) {
        result = 22;
    }

    return result;
}


Tech Tip No27: Keep an eye for multi-threaded access of code, failing to handle this properly will lead to unexpected results!

Writing your code with the possible end use scenarios in mind will help you better understand what it takes to make sure it is thread-safe.

Enterprise or web applications are mostly a great area for this kind of thinking, since those sorts of applications are built with multiple concurrent users in mind.

As an example, if you’re using Java as your language of choice, never create instance variables in Servlets, since those will not be thread-safe at all (You may of course create fields)!


Tech Tip No28: Avoid performing expensive operations inside iterative blocks of code

This tip is so trivial I feel that I don’t really need to say anything more! Except that I’m wrong. It so happens that I keep coming across some iterative code with ridiculously slow operations in its block, operations that could be quite easily moved out without a hustle.

Keep this tip in mind next time you’re about to create an iterative block.

As an example to provoke your thinking a bit

for (int i = 0; i < list.size(); i++) {
    // Logic!
}

Could be better re-written as shown below

final int size = list.size();

for (int i = 0; i < size; i++) {
    // Logic!
}


Tech Tip No29: [Java] Avoid concatenating Strings using '+' or 'concat' especially in loops

The String class is immutable, what that means is that you cannot do anything to it’s content once it has been instantiated. Using the ‘+’ or the concat method, you’ll be simply creating new Strings to hold the appended parts, which might be bad for performance and memory if used inside of an iterative block.

StringBuffer or StringBuilder on the other hand are mutable, which means that their content can be modified, hence it’s always recommended to use the latter two options.

The difference between those is that StringBuilder was introduced since JDK 5 and it’s not synchronized.


Tech Tip No30: [Java] Avoid instantiating wrappers for primitive types, use the 'valueOf' method instead

I should start by telling you guys, don’t use wrapper classes for primitive types unless you need to. Having that out of the way, there are two possible ways for creating wrapped objects of primitive type, one is instantiating a new object and the other would be using the valueOf method.

See the example below

Integer intValue = new Integer(12);
int intValue2 = Integer.valueOf(12);

The latter approach was introduced since JDK 5 and is highly recommended, since frequently used int values will be cached (it caches integers within the range -128 and 127).


Hope you found this article useful enough, drop a comment if you feel like it :)

Comments

Popular posts from this blog