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 / November 2007

Tip: Looking for answers? Try searching our database.

Which one is better?

Thread view: 
howa - 11 Nov 2007 11:41 GMT
Aim: To add a member into the system

Method 1:

If ( system.validation ( member ) ) {
   system.addMember( member );
}

Method 2:

if (! system.addMember( member ) ) {
   System.out.println("Adding failed");
}

where the validation logic is in the addMember method.

Which one you prefer?
Piotr Kobzda - 11 Nov 2007 14:10 GMT
> Aim: To add a member into the system
>
[quoted text clipped - 13 lines]
>
> Which one you prefer?

2 or:

Method 3:

system.addMember( member );

where addMember() throws IllegalArgumentException (or more specific
exception).

piotr
Arne Vajhøj - 11 Nov 2007 15:30 GMT
>> Method 1:
>>
[quoted text clipped - 7 lines]
>>     System.out.println("Adding failed");
>> }

> 2 or:
>
[quoted text clipped - 4 lines]
> where addMember() throws IllegalArgumentException (or more specific
> exception).

If adding failing is an exceptional occurrence then #3 else #2.

Arne
Patricia Shanahan - 11 Nov 2007 15:36 GMT
>>> Method 1:
>>>
[quoted text clipped - 18 lines]
>
> If adding failing is an exceptional occurrence then #3 else #2.

Agree. Both are better than #1 because #1 incorporates two unnecessary
assumptions:

1. That the result of system.validation does not change between the two
calls. It is possible that the critical portion of addMember is
synchronized.

2. That system.validation failure is the *only* way addMember can fail.
Even if that is the case now, #1 would not be robust under future
development in addMember.

Of course, #2 is better than #3 if addMember cannot be changed.

Patricia
Mark Space - 11 Nov 2007 16:56 GMT
> Of course, #2 is better than #3 if addMember cannot be changed.

Do you think it would be better to have addMember() throw a Runtime
exception instead if it cannot be changed?
Patricia Shanahan - 11 Nov 2007 17:03 GMT
>> Of course, #2 is better than #3 if addMember cannot be changed.
>
> Do you think it would be better to have addMember() throw a Runtime
> exception instead if it cannot be changed?

If it cannot be changed to throw an appropriate exception, with the
implication that any existing code will be changed appropriately, I
would stick with the existing interface and use #2.

Suppose there is an existing user of #2, and addMember is changed to
throw an appropriate checked exception. The #2 code will not compile
until it is changed to deal with the new exception. If addMember were
changed to throw a RuntimeException, the existing #2 code would fail
mysteriously when addMember fails.

Patricia
Roedy Green - 11 Nov 2007 17:24 GMT
>Method 1:
>
[quoted text clipped - 7 lines]
>    System.out.println("Adding failed");
>}

There is a third more Javaesque method.

Method 3:
try {
...
 system.addMember( member );
...
  catch ( InvalidMemberException e )
     {
     // Note that one catch often services
     // several possible places the exception could be triggered.
      System.out.println("Cannot add invalid member.");
      System.out.println( e.getMessage() );
     }

The two techniques you showed are more common in C code.
They might be used in Java if the probability were of failure were
high, especially method 2.

Method 1 in mainly used in methods like "hasNext".

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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



©2009 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.