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.