2012-02-04

Real Story: One of my favorite pieces of code (including advanced tutorial)

<sarcasm>

This is a real piece of code I found some time ago and I a still think, it has some great internal greatness, so I decided to publish some kind of a special tutorial about it:
1 public boolean falseBoolean( boolean trueValue ) { 2 boolean returnValue = false; 3 if ( trueValue == true ) { 4 returnValue = false; 5 } 6 else { 7 returnValue = true; 8 } 9 return returnValue;10 }
One can easily see the advanced mind of the programmer who wrote that exceptional piece of code.
So why we don't take the code and analyze the important Java idioms and rules one can find there, and maybe we can also improve it a little bit.

First it is a good idea to change names accordingly: the name of the method variable 'trueValue' is probably not so good, because one might think that this variable always contains a true value, or if there are some 'wrong variable' somewhere, so I will change this to 'value' which is quite neutral.

Also 'returnValue' is a little bit long and it might confuse to have all variable names containing 'value', so I change this to the shorter name 'result'.

Then there is the idea that variables should be final, what actually means that they are not real variable, but fixed, but let us ignore this tiny point now.

If we change the local variable to final, we are not allowed to assign anything to it in line 2, because otherwise we would not be able to assign a new value to it in lines 4 and 6.

So here is the slightly improved code:

1 public boolean falseBoolean( final boolean value ) { 2 final boolean result; 3 if ( value == true ) { 4 result = false; 5 } 6 else { 7 result = true; 8 } 9 return result;10 }

You see the code confirms to an old rule: "Not more than 1 return statement in a method". Well, to be quite direct, let me say that I don't like that rule. It does not make sense for me, because there  might be of course some situations where it is OK, to return from a method in the middle of the method. I really think that it depends on the context or on the coding conventions you have to use at your office.

Let's assume that I can do what I want..., so I will use two return statements here, so that the method is quite shorter. In order to do so, we will remove the local variable.

1 public boolean falseBoolean( final boolean value ) {2 if ( value == true ) {3 return false;4 }5 else {6 return true;7 }8 }

Another small issue is that I do not like comparisons of the kind 'a == true', where a is a boolean variable. So I change this, too.

1 public boolean falseBoolean( final boolean value ) {2 if ( value ) {3 return false;4 }5 else {6 return true;7 }8 }

As I like to use what Java offers, I also like the ternary (conditional) operator. Some people do not like it, because the code might be more difficult to read then, but I think one can use it for short statements, as follows:

1 public boolean falseBoolean( final boolean value ) {2 return value ? false : true;3 }

I hope, everybody got that last change...

Now we should check how this method is actually called, maybe somehow like as follows (ignoring that it is a static method and should perhaps have the class name before the method name):

1 boolean dump = ...;2 3 boolean garb = falseBoolean( dump );

Here dump gets a boolean value somehow and later the new value is assigned to garb by calling our optimized method.

As the method is quite short, one might consider inlining the contents of the method. Then one would have:

1 boolean dump = ...;2 3 boolean garb = dump ? false : true;

Yes, this is it...

This is my final version for all the programmers who have never heard of the ! operator.
I thank you for your attention.

In my next post I will show you how to convert a long value to an int value. Stay tuned.

</sarcasm>

No comments:

Post a Comment