stuttering : Java Glossary

go to home page S words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2009 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
stuttering
Inexperienced Java programmers often balloon out their code with a nervous sort of stuttering redundancy. Here are some typical examples:

Boolean Redundancy

Newbies love to balloon out their code with redundancy. Newbies will write:
if ( i < 7 )
   {
   return true;
   }
else
   {
   return false;
   }
An experienced programmer would handle that as:
return i < 7;
A newbie might try:
return b ? true : false;
It should be handled:
return b;
A newbie might code:
return b ? false : true;
It should be handled:
return !b;
avoid:
boolean b;
if ( i < 7 )
   {
   b = true;
   }
else
   {
   b = false;
   }
just use:
boolean b = i < 7;
Similarly avoid this redundancy:
if ( b == true ) j++;
just use:
if ( b ) j++;
A newbie might code:
if ( a.startsWith ( "c$$" ) )
   {
   System.out.println( "true" );
   }
else
   {
   System.out.println( "false" );
   }
You could abbreviate that as:
System.out.println( a.startsWith( "c$$" ) );

String Redundancy

Newbies love to write things like:
String s = new String( "abc" );
To avoid littering the constant pool with redundant Strings, this should be written:
String s = "abc";
Newbies write things like:
String s = new String();
To avoid littering the constant pool with redundant copies of "", this should be written:
String s = "";
new String( String) rarely has legitimate use. One use is freeing up RAM of a large base String when you no longer need the base String, but only want the substring kept around. If you use it otherwise you create needless duplicate Strings in your literal pool. For details on when new String( String) is valid, see intern.

If Redundancy

if ( i <= 10 )
   {
   /*... */
   }
else if ( 11 <= i && i <= 15 )
   {
   /*... */
   }
should be written:
if ( i <= 10 )
   {
   /*... */
   }
else if ( i <= 15 )
   {
   /*... */
   }
A newbie might code:
public String getNull()
   {
   return(String)null;
   }
There is no need to cast null, so this method is not needed at all. Plain null will do.

Conversions

Instead of consulting conversion in the Java glossary to find the optimal code, newbies, concoct preposterously complicated chains of conversions to convert a double to a String for example, leaving behind a trail of discarded dummy objects. Here is some typical newbie code:
String s = "1";
int i= new Integer(s).intValue(); /* newbie */
int i = Integer.parseInt( s ); /* correct */
//...
int i = 1;
String s = new Integer( i ).toString(); /* newbie */
String s = i + ""; /* lazy */
String s = Integer.toString( i ); /* correct */

Too Big And Flexible a Hammer

Newbies discover exceptions and use them where simpler and more efficient constructs would work better. For example they might use them in place of a loop ending condition or even a boolean return flag.

Reflection dazzles, and newbies will use it where simple interfaces and delegate objects would suffice.

Sometimes an old-fashioned if or switch statement is easier to understand and maintain than a complicated nest of inherited classes. A newbie is intoxicated with the power of OO and wants to use it everywhere.

The more advanced newbie might discover the facade design pattern, and go overboard thinking that every class should implement every method with a wrapper.

If you enjoyed this essay you might like this one on how to write unmaintainable code.


CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.62] The information on this page is for non-military use only.
You are visitor number 2,091. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/stuttering.html J:\mindprod\jgloss\stuttering.html