The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Java Coding Questions

Posted by victor kamat on January 16, 2002 at 4:54 PM

I've written the method shown below method that does a parameter check,
and if the check fails throws a ModelException.

I show two versions of the code, Version A and Version B. The only
difference is that Version A has a boolean flag variable that is set true
if an Exception has to be thrown.
Is any one version preferable to the other ? Same ?


/* Code: Version B */
private void check(int cycle, int size)
{
StringBuffer mesg = null;

if ( cycle < 10 ) { // cycle s/b >= 10
mesg.append("ModelException: cycle has value less than 10" );
}

if ( size < 30 ) { // size s/b >= 30
mesg.append("ModelException: size has value > 20" );
}

if ( mesg != null )
throw new ModelException( mesg.toString() );
}


/* Code: Version A */
private void check(int cycle, int size)
{
StringBuffer mesg = null;
boolean flag = false;

if ( cycle < 10 ) { // cycle s/b >= 10
flag = true;
mesg.append( "ModelException: cycle has value less than 10" );
}

if ( size < 30 ) { // size s/b >= 30
flag = true;
mesg.append( "ModelException: size has value > 20" );
}

if ( flag == true )
throw new ModelException( mesg.toString() );
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us