Which one is better?

Discussion in 'Java' started by howa, Nov 11, 2007.

  1. howa

    howa Guest

    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?
     
    howa, Nov 11, 2007
    #1
    1. Advertising

  2. howa

    Piotr Kobzda Guest

    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
     
    Piotr Kobzda, Nov 11, 2007
    #2
    1. Advertising

  3. 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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 11, 2007
    #3
  4. 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
     
    Patricia Shanahan, Nov 11, 2007
    #4
  5. howa

    Mark Space Guest

    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?
     
    Mark Space, Nov 11, 2007
    #5
  6. 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
     
    Patricia Shanahan, Nov 11, 2007
    #6
  7. howa

    Roedy Green Guest

    On Sun, 11 Nov 2007 11:41:58 -0000, howa <> 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
     
    Roedy Green, Nov 11, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr Alphaque
    Replies:
    14
    Views:
    1,133
    Raymond DeCampo
    Aug 2, 2003
  2. RSB
    Replies:
    0
    Views:
    391
  3. Hafez
    Replies:
    14
    Views:
    712
    Thomas Matthews
    Oct 6, 2003
  4. Himanshu Singh Chauhan

    Memory usage v/s complexity -- which one better?

    Himanshu Singh Chauhan, Jan 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    414
    Barry Schwarz
    Jan 25, 2004
  5. N/A
    Replies:
    2
    Views:
    1,517
    Alexander Schmolck
    May 11, 2006
Loading...

Share This Page