Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Java Ternary Operator

You can use these shortcuts for better readability when you have lots of if-else statements

A = B == C 
This is read as:
If B = C then A is true, else A is false
The statement above is the same as this if-else statement:
if(B == C){
A = true;
} else {
A = false;
}
The other one has a more familiar form than the previous:
A = A ? B : C
This is read as:
If A is true it will take the value of B, If A is false else A will be C.
The statement above is the same as this if-else statement:
if(A == true){
A = B;
} else {
A = C;
}
Note: A,B, and C should return a boolean value.

Posted at at Monday, July 20, 2009 on 7/20/09 by Posted by amj | 0 comments   | Filed under: ,

Java Variable Naming

I am not sure if this is the best practice for naming variables but I find the codes easier to read this way. Append the letter 'a' to every Class variables, 'p' to method/Constructor parameter, and 'l' to local variables .

If you do this I'm sure the guy who will maintain your code will not have a hard time debugging your code.

Class Animal {
private String aAnimalType;
public void setAnimalType(String pAnimalType){
aAnimalType = aAnimalType;
}
public String getAnimalType(){
return aAnimalType;
}
}

Posted at at Friday, July 17, 2009 on 7/17/09 by Posted by amj | 0 comments   | Filed under: , , ,

When to create a new method

I think this is one of the question that they don't teach in training camps - when to you create a new method.

For code reuse, you should put a piece of code in a new method if some Class/method other than the method you are in is also performing that routine or if you anticipate that this piece of code will be use by other classes in the future.

Utility Class and Utility Method

Most of the application that I'm working right now has Utility Classes wherein you put your utility methods. A utility method is a method that doesn't need Class instance variable to operate and they are usually declared as public and static.

public static String
stripHeader(String pRawXML)
{
//implementation
}


Code Readability

For code readability and easy maintenance I choose to create a new method if that block of code performs a certain task even if it is not reused by my other classes.

Posted at at Thursday, July 16, 2009 on 7/16/09 by Posted by amj | 0 comments   | Filed under: , , ,