четверг, 7 января 2010 г.

Bad books samples

Today I read a blog entry How Programming Books Promote Code Smells.

This is true: many books contain examples of bad code.

One of my favorite examples is the main method with try/catch like this one.


public static void main(String args[]) {
try{
FileInputStream fstream = ...
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}


This try/catch is absolutely NOT NEEDED! If you skip them, the JVM does exactly the same work. the well-known "Throw early catch late" principle says that you shoudn't catch exception until you know what to do with it (except the trivial actions that the system does by default).

Комментариев нет:

Отправить комментарий