Wednesday, September 12, 2012

eclipse short cuts for good coding

Ctrl + Shift + F – Formats the source code.

Ctrl + Shift + O – Organizes the imports and removes the unused ones.

Instead of you manually invoking these two functions, you can tell Eclipse to auto-format and auto-organize whenever you save a file. To do this, in Eclipse, go to Window -> Preferences -> Java -> Editor -> Save Actions and then enable Perform the selected actions on save and check Format source code + Organize imports.

Avoid multiple returns (exit points) in methods: 

private boolean isEligible(int age){
  if(age > 18){
    return true;
  }else{
    return false;
  }
}

can be replaced with the below code 


private boolean isEligible(int age){
  boolean result;
  if(age > 18){
    result = true;
  }else{
    result = false;
  }
  return result;
}


which one is better

Boolean b = new Boolean(true)  or Boolean.valueOf(true)  ?

Do not create new instances of Boolean, Integer or String: 

Avoid creating new instances of Boolean, Integer, String etc. For example, instead of using new Boolean(true), useBoolean.valueOf(true). The later statement has the same effect of the former one but it has improved performance.



Mark method parameters as final, wherever applicable: 

Always mark the method parameters as final wherever applicable. If you do so, when you accidentally modify the value of the parameter, you’ll get a compiler warning. Also, it makes the compiler to optimize the byte code in a better way. 


private boolean isEligible(final int age){ ... }


Name public static final fields in UPPERCASE: 

Always name the public static final fields (also known as Constants) in UPPERCASE. This lets you to easily differentiate constant fields from the local variables. 

NOT RECOMMENDED
public static final String testAccountNo = '12345678'; 

RECOMMENDED
public static final String TEST_ACCOUNT_NO = '12345678';,


Tuesday, September 11, 2012

copying from one array to another array in python


array1 = ['ramesh']
array2 =array1

print(array2)

Monday, September 10, 2012

python how to use if else , simple example


a  = input()
if a == 'ramesh':
    print ('hi '+a)
elif  a == 'babu':
    print ('hi '+a+' ela unnavu')
else :
    print ('hi ')


save it with ifelse.py and run using f5