Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Java Best practices

Thread view: 
Philipp - 10 Apr 2007 13:42 GMT
Hello
I've been running TPTP static analysis in Eclipse over my code, and it
complains about:
"Always place constants on the left side of the equals()"

Example code:
if (fileExtension.substring(0, 1).equals(".")){
  fileExtension = fileExtension.substring(1); // get rid of the dot
}

wants to be fixed to:
if (".".equals( fileExtension.substring(0, 1) )){
  fileExtension = fileExtension.substring(1); // get rid of the dot
}

Why is it better to have constants on left side of equals() ?

In my opinion the first code is much more readable than the second. Is
readability of higher priority than such coding best practices?

Thanks for your answers
Philipp
Daniel Dyer - 10 Apr 2007 14:03 GMT
> Hello
> I've been running TPTP static analysis in Eclipse over my code, and it  
[quoted text clipped - 12 lines]
>
> Why is it better to have constants on left side of equals() ?

It's to avoid NullPointerExceptions.  If a method can return null it may  
cause problems if you try to invoke the equals method on the returned  
reference.  By ensuring that you call equals on the String constant and  
pass the (possibly null) value as an argument, you should never get a  
NullPointerException.

> In my opinion the first code is much more readable than the second. Is  
> readability of higher priority than such coding best practices?

I wouldn't blindly follow "best practices" just because somebody says they  
are a good idea.  As you have acknowledged by asking the question, it's  
best to find out the justifications and make up your own mind.

Dan.

Signature

Daniel Dyer
http://www.uncommons.org

Chris Uppal - 10 Apr 2007 14:31 GMT
> In my opinion the first code is much more readable than the second. Is
> readability of higher priority than such coding best practices?

In my opinion, yes -- readability is nearly always more important than such
"micro" practices.

(And, as an aside, I don't think that automated tools should ever be taken as
arbiters of best practice.  Indeed, I have never found them to be very useful
even as mere sources of suggestions for consideration.)

   -- chris
Wojtek - 10 Apr 2007 15:20 GMT
Philipp wrote :
> Hello
> I've been running TPTP static analysis in Eclipse over my code, and it
[quoted text clipped - 5 lines]
>    fileExtension = fileExtension.substring(1); // get rid of the dot
> }

Or you could have:

if (fileExtension.startsWith("."))
{
  fileExtension = fileExtension.substring(1); // get rid of the dot
}

Signature

Wojtek :-)

Philipp - 11 Apr 2007 06:17 GMT
> Philipp wrote :
>> Hello
[quoted text clipped - 13 lines]
>   fileExtension = fileExtension.substring(1); // get rid of the dot
> }

Absolutely :-)
When I wrote this, I didn't know startsWith() yet...

Thanks to all for your answers
Phil


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.