Thursday 20 September 2007

To be null or not to be ?


Book myBook = getBookFromSomeWhere();
System.out.println("myBook: " + myBook);

if (myBook == null) {
System.out.println("myBook is null");
}
else {
System.out.println("myBook is not null");
}


Run it ! And that display :

myBook: null
myBook is not null


How is it possible ?

Hahaha.

It is because we have:

Book {
...

public String toString() {
return "null";
}


Funny, isn't it ?

Wednesday 29 August 2007

Out of subject : Errare Humanum Est

Basically the implementation look like :

if client => send email to client
if enterprise => send email to client
if others => send email to client

BUT this behavior was not as specified. It should be :

if client => send NO email
if enterprise => send email to enterprise
if others => send email to enterprise

And guess what, it was in production because of ME. Yes, ME. I'm gonna kill myself. How it could be, how I could make such a mistake. By focusing too much on code aspect.

This teach me a great thing. Of course the code aspect, the architecture and so on are important, but the business target IS MORE IMPORTANT than every thing.

Today, I was the stupid guy because Errare Humanum Est.

Friday 3 August 2007

What time is it ?


// defined previously
// Date aDate = ...

Date date = new Date();
date = new Date(date.getTime());

if (aDate != null && aDate == date) {
date = new Date(date.getTime() + 10);
}

Hum, the biggest problem with this one is the equality condition test (aDate == date).
In java, == DOESN'T compare value, except for primitive data type, it compares pointer.
That's it, it test if two objects are same. To test if two object are equal (in value) use Object.equals method.


Then, it is trivial that
Date date = new Date();
date = new Date(date.getTime());
is stupid.
Date date = new Date();
is sufficient.

Friday 13 July 2007

cast it


public static int roundSup(float n){
int round=0;

if(n > (new Float(n).intValue()))
round = new Float(n+1).intValue();
else round=new Float(n).intValue();

return round;
}

Sure it needs to create new Float object to do this operation. And the code is ugly.

Try this,

public static int roundSup(
final float n
) {
return ((int) n) + 1;
}

Wow ! Cleaner, clearer, better !
Why just a cast will do it ?

See Java Language Specification 3rd : Narrowing Primitive Conversions

But don't forget to COMMENT you're code ! A code without comments it's like a life without meaning.

Thursday 12 July 2007

Program Identifier Naming Conventions

Microsoft 1975 Charles Simonyi's explication of the Hungarian notation identifier naming convention. What to say ? Most people thinks that they need to prefix program identifier (say variable, member...) with the system type. For boolean prefix with 'b' like bDone. For int prefix with 'i' like iCount. That's stupid to be so close to the system type. The thing that is important it's the meaning, count, or index, not the hard system type that can be changed. Let's read these: I’m Hungary Hungarian Notation Cleaner, more elegant, and harder to recognize Dropping the 'I' from interface names?

Tuesday 10 July 2007

try surprise me, catch my surprise, finally I'm surprised


public static int doSomething() {
int i = 0;
try {
i = 1;
System.out.print("a");
return i;
}
catch (Exception e) {
i = 2;
System.out.print("b");
return i;
}
finally {
i = 3;
System.out.print("c");
return i;
}
} // end

return ?

return 3 ! and print "ac".

This code behave like the finally block is an inside method:

public static int doSomething() {
int i = 0;
try {
i = 1;
System.out.print("a");
{ // finally block
int another_i = 3;
System.out.print("c");
return another_i;
}
return i;
}
catch (Exception e) {
i = 2;
System.out.print("b");
{ // finally block
int another_i = 3;
System.out.print("c");
return another_i;
}
return i;
}
} // end

...
So, and if there is no return in finally block.

public static int doSomething() {
int i = 0;
try {
i = 1;
System.out.print("a");
return i;
}
catch (Exception e) {
i = 2;
System.out.print("b");
return i;
}
finally {
i = 3;
System.out.print("c");
// return i;
}
} // end

It return 1 and print "ac".
Behave like that:

public static int doSomething() {
int i = 0;
try {
i = 1;
System.out.print("a");
{ // finally block
int another_i = 3;
System.out.print("c");
}
return i;
}
catch (Exception e) {
i = 2;
System.out.print("b");
{ // finally block
int another_i = 3;
System.out.print("c");
}
return i;
}
} // end


The try-catch-finally doesn't really behave as first expected.
See Java Hall Of Shame
and finally.

Monday 9 July 2007

space is too much


/**
* Return the concatenation of first name and last name separated by a space.
*/
public String getFullname() {
String s = "";

if (this.getFirstName() != null) {
s += this.getFirstName() + " ";
}

if (this.getLastName() != null) {
s += this.getLastName();
}

return s;
}

Let's say that firstName = 'Pitt' and lastName = 'Dirk'. We got "Pitt Dirk". And what if the firstName is null, we got " Dirk". Hum, less good.

And what if both are null, we got " ", a space!


This f**king space mess up our database. The better, this kind of method are copied/pasted every where the same behavior was needed.


What do we need then ? A nice join method that concatenate elements of an Object array inserting a separator between elements and ignoring null and/or empty string.

Tuesday 3 July 2007

Eclipse 3.3 Europa




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.