stuttering : Java Glossary
home S words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : 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 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 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.18] Spread the Net
You are visitor number 556.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/stuttering.html J:\mindprod\jgloss\stuttering.html