Over a million developers have joined DZone.

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

· Java Zone

Learn more about how the Java language, tools and frameworks have been the foundation of countless enterprise systems, brought to you in partnership with Salesforce.

This is a real piece of code I found some time ago, and I still think it has some great internal greatness, therefore I decided to publish some kind of a special tutorial about it:

public boolean falseBoolean( boolean trueValue ) {
	boolean returnValue = false;
	if ( trueValue == true ) {
	  returnValue = false;
	}
	else {
		returnValue = true;
	}
	return returnValue;
}

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.

Here is the slightly improved code:

public boolean falseBoolean( final boolean value ) {
	final boolean result;
	if ( value == true ) {
	  result = false;
	}
	else {
		result = true;
	}
	return result;
}

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.

public boolean falseBoolean( final boolean value ) {
	if ( value == true ) {
	  return false;
	}
	else {
		return true;
	}
}

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.

public boolean falseBoolean( final boolean value ) {
	if ( value ) {
	  return false;
	}
	else {
		return true;
	}
}

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:

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

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

boolean dump = ...;

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:

boolean dump = ...;

boolean garb = dump ? false : true;

Yes, this is it: short, easy to maintain, everybody can understand it, and it has some kind of internal glow.

This was 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.

Discover how the Force.com Web Services Connector (WSC) is a code-generation tool and runtime library for use with Force.com Web services, brought to you in partnership with Salesforce.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}