Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Which one is better?

Reply
Thread Tools

Which one is better?

 
 
howa
Guest
Posts: n/a
 
      11-11-2007
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?

 
Reply With Quote
 
 
 
 
Piotr Kobzda
Guest
Posts: n/a
 
      11-11-2007
howa wrote:
> 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?


2 or:

Method 3:

system.addMember( member );

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


piotr
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      11-11-2007
Piotr Kobzda wrote:
> howa wrote:
>> Method 1:
>>
>> If ( system.validation ( member ) ) {
>> system.addMember( member );
>> }
>>
>> Method 2:
>>
>> if (! system.addMember( member ) ) {
>> System.out.println("Adding failed");
>> }


> 2 or:
>
> Method 3:
>
> system.addMember( member );
>
> where addMember() throws IllegalArgumentException (or more specific
> exception).


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

Arne
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-11-2007
Arne Vajh°j wrote:
> Piotr Kobzda wrote:
>> howa wrote:
>>> Method 1:
>>>
>>> If ( system.validation ( member ) ) {
>>> system.addMember( member );
>>> }
>>>
>>> Method 2:
>>>
>>> if (! system.addMember( member ) ) {
>>> System.out.println("Adding failed");
>>> }

>
>> 2 or:
>>
>> Method 3:
>>
>> system.addMember( member );
>>
>> where addMember() throws IllegalArgumentException (or more specific
>> exception).

>
> 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
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      11-11-2007
Patricia Shanahan wrote:

> 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?
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-11-2007
Mark Space wrote:
> Patricia Shanahan wrote:
>
>> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-11-2007
On Sun, 11 Nov 2007 11:41:58 -0000, howa <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>Method 1:
>
>If ( system.validation ( member ) ) {
> system.addMember( member );
>}
>
>Method 2:
>
>if (! system.addMember( member ) ) {
> 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".

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
Which one is the correct one.... ? Jonathan Smith ASP .Net 3 12-11-2004 01:36 AM
Samsung SCD6040 - Anyone have one, used one, or have reviews on one? g wills Digital Photography 0 09-08-2004 08:06 PM
CSS aligning two things on one line with one left and one right news.frontiernet.net HTML 6 04-16-2004 02:44 AM
Using One XSLT and multiple XML Problem (One is XML and another one is XBRL) loveNUNO XML 2 11-20-2003 06:47 AM



Advertisments