Friday 29 June 2007

Copy/Paste


client.setSiteId(clientForm.getSiteId());
client.setPrestaId(clientForm.getSiteId());

(Stupid): Oooops boss. I've done a copy/paste bug 1 year ago. And now the database is a mess.

(Boss): Stupid ! You're fired !


I'd would be this boss BUT if this kind of bug is forgivable, it is unforgivable to let this bug pass tests. So, this boss is not a good boss.

Data in database are EVERYTHING for the application. Applications without data are useless. See google ! They sell data, they make free software ;-)

Copy/paste ok ! Human being makes a lot of stupid error. So, make test !!!

Tuesday 26 June 2007

SolarFlaresException


try {
doSomethingInSpace();
}
catch (SolarFlaresException e) {
LOG.warn("Watch the solar winds !" + e);
}


This is just a joke from an article about programming for spacecraft.

Three Minutes With Mike Deliman

A Conversation with Mike Deliman

Monday 25 June 2007

Bool It


new Boolean(true);

Correct ? No ! Because

new Boolean(true) != new Boolean(true);

But

Boolean.TRUE == Boolean.TRUE;

and

Boolean.valueOf(true) == Boolean.valueOf(true);

It is a java design problem but it is too late to correct it, the constructor is public.
Some serialization and deserialization failed because of it.

Wednesday 20 June 2007

for a while


for (Iterator iter = myList.iterator(); iter.hasNext(); /* noop */) {
Object element = iter.next();
...
} // end for iter

OR

Iterator iter = myList.iterator();
while (iter.hasNext()) {
Object element = iter.next();
...
} // end while iter

Constant++


// FYI: Constants is a class (not an interface)

if (ts != null && ts.getTs().intValue() == 1){
nbPc = Constants.CONST1A;
Constants.CONST1A = Constants.CONST1A + 1;

nbPs = Constants.CONST1B;
Constants.CONST1B = Constants.CONST1B + 1;
} else if (ts != null && ts.getTs().intValue() == 2){
nbPc = Constants.CONST2A;
Constants.CONST2A = Constants.CONST2A + 1;

nbPs = Constants.CONST2B;
Constants.CONST2B = Constants.CONST2B + 1;
} else if (ts != null && ts.getTs().intValue() == 3){
nbPc = Constants.CONST3A;
Constants.CONST3A = Constants.CONST3A + 1;

nbPs = Constants.CONST3B;
Constants.CONST3B = Constants.CONST3B + 1;
} else if (ts != null && ts.getTs().intValue() == 4){
nbPc = Constants.CONST4A;
Constants.CONST4A = Constants.CONST4A + 1;

nbPs = Constants.CONST4B;
Constants.CONST4B = Constants.CONST4B + 1;
}

Oh that ignominious code.
First, it appears that if some fields were in a Class named Constants, they will be final.
The ts variable can be tested different from null only once.
Use myVariable++ instead of myVariable = myVariable + 1
At last, the use of if-statement to compare ts.getTs().intValue() to an int is not the best technic.

Is empty String ?

How to test that a String is empty?

A lot of people use:


myString.equals("")

Some use:

"".equals(myString)

Few use:

myString.length() == 0

Fewer use:

myString != null && myString.length() == 0

Lazy people use:

org.apache.commons.lang.StringUtils.isEmpty(myString)

Which one is the best solution ?

I tell you ! Be lazy ! Knows libraries like you learn new word from dictionary.
Why ? Because, apache people ARE good. Because it is already done by another. Because it is free.

Else? Use

myString != null && myString.length() == 0

because a String might be null and because it is really fast to test the String length instead of its content.

No brackets !

Let's have:


if (condition)
doSomething();

And later, add doOtherThing() method to the condition.

if (condition)
doSomething();
doOtherThing();

And later, it appends. Ooops. I forgot the brackets.
So, I don't know why it is not required in every languages, BUT you MUST always use brackets.

if (condition) {
doSomething();
}

And then

if (condition) {
doSomething();
doOtherThing();
}

This time I didn't forget the brackets 8-))

Thursday 14 June 2007

Switch for dummy


// sql is a StringBuffer
// params is an array of String defined previously
// paramsTmp is a String.

switch(i) {
case 1 :
sql.append(", ");
i++;
params[1] = paramsTmp;
break;
case 2:
sql.append(", ");
i++;
params[2] = paramsTmp;
break;
case 3:
sql.append(", ");
i++;
params[3] = paramsTmp;
break;
case 4 :
sql.append(", ");
i++;
params[4] = paramsTmp;
break;
case 5 :
sql.append(", ");
i++;
params[5] = paramsTmp;
break;
case 6 :
sql.append(", ");
i++;
params[6] = paramsTmp;
break;
case 7 :
sql.append(", ");
i++;
params[7] = paramsTmp;
break;
}

This is insane. The guy who develops this is seriously sick. I'd known him ;-p
He told me: "Just 3 lines? How are you sure that's working ?"
These kind of developers don't understand what they do.

Of course a simpler solution was:

sql.append(", ");
params[i] = paramsTmp;
i++;

Not 40 lines, just 3 lines and very clear ones.

Generally, we don't need a switch. It is for bad developer, one who doesn't know pattern. Use if-else statement instead but there are better solutions.
Use an array or a List when possible, like:

final String[] myValues = {"a", "b", "c"};
final String value = myValues[index];

If the switch values is a fixed enumeration, use enum pattern instead. It is really powerfull.
Look at Java 1.5 Enums.

You can do such thing:

/**
* MyEnum declaration.
*/
enum MyEnum {
A("A"),
B("B");

private final String mValue;

MyEnum(
final String value
) {
this.mValue = value;
}

public String getValue() {
return this.mValue;
}

public static MyEnum forValue(
final String value
) {
for (final MyEnum myEnum : values()) {
if (myEnum.getValue().equals(value)) {
return myEnum;
}
return null;
}
}

}

MyEnum use:

final String aValue = getValueSomewhere(...)
MyEnum.forValue(aValue);

See? no more switch needed.

Code completion and a lazy developer

(Integer) Integer.getInteger("12345")

Wow, that a nice stupid code. I lost 1 hour because of it. Thanks to YOU! The static Integer.getInteger(String) method return the value of a SYSTEM, I repeat a SYSTEM, property as an Integer. Hey! Lazy developers, read the javadoc before code completion.

Use this instead:

(Integer)
Integer.valueOf(String)


or

(int) Integer.parseInt(String)

Intro

This blog is about all stupid line of codes wrote by java developers (mostly). I will comment why they're stupid.

Let's start:

new String("").toString();
It is a really usefull code. Just to be sure that we've got an empty String. Yes, why not complete it like that just to be sure we have an empty String.
new String("").toString().substring(0, 0).trim();
Of course it is a stupid code. You want an empty String.
""
Got it.

It is stupid to do so because "" will create a new instance of String(), new String("") will do the same and toString() is just a shortcut to this String.