среда, 21 июля 2010 г.

Consider the following code snippet:

public class SomeClass {

 @SuppressWarnings("unused")
 private Validator validator;

 ... 
}

Variable validator really isn't use. It could be simply removed, by author decided to mark it as @SuppressWarnings("unused") - just in order to make compiler and Eclipse silent. 

Stupid!

четверг, 22 апреля 2010 г.

Collection setter

There is two major ways to write setters for collection-type members.
This example combines both of them.
public void setPhones(List<PhoneNumber> phones) throws Exception
{
this.phones = phones;
this.phones.clear();
this.phones.addAll(phones);
}

четверг, 4 марта 2010 г.

Compare float numbers

This is how Chuck Norris compares float numbers:
if (!Float.valueOf("0.0").equals(price.getActualPrice()))
{
return true;
}

понедельник, 22 февраля 2010 г.

As to me, this is a really unobvious way to parse strings:

protected void parseSummaryLines()
{
...

// NOTE: First letters are ommited in order to
// support capitalized words as well
String RESULT_GOOD_TEXT_1 = "othing"; // Nothing
String RESULT_GOOD_TEXT_2 = "uccessful"; // Successful
String RESULT_BAD_TEXT_1 = "assword"; // Password
String RESULT_BAD_TEXT_2 = "failed"; // Failed

...
}

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

Next generation programming style

A very good article about programming:
http://codemonkeyism.com/generation-java-programming-style/

in particular, this one is great:
http://codemonkeyism.com/never-never-never-use-string-in-java-or-at-least-less-often/

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).