Results 1 to 4 of 4
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default passing arguments

    Please advise which method is preferred and explain why?

    p is an Object of some kind.

    A
    Java Code:
    if (new LaunchCompareJob(p).go()) {}
    B
    Java Code:
    if (new LaunchCompareJob().go(p)) {}

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by mac View Post
    Please advise which method is preferred and explain why?

    p is an Object of some kind.

    A
    Java Code:
    if (new LaunchCompareJob(p).go()) {}
    B
    Java Code:
    if (new LaunchCompareJob().go(p)) {}

    imho: i would prefer the second because the object LaunchCompareJob don't need an explicit constructor to handle the argument p.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by mac View Post
    Please advise which method is preferred and explain why?

    p is an Object of some kind.

    A
    Java Code:
    if (new LaunchCompareJob(p).go()) {}
    B
    Java Code:
    if (new LaunchCompareJob().go(p)) {}
    What happens here? Is this even possible? If so, yuck!

    C
    Java Code:
    if (new LaunchCompareJob(p).go(p)) {}
    D
    Java Code:
    if (new LaunchCompareJob().go()) {}
    kind regards,

    Jos

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    if the only thing LaunchCompareJob ever does is perform one operation on p, (A) is preferable. The user of the API won't end up in the situation when he (mistakenly) created a LaunchCompareJob and only then realized it cannot be used for Object p.

    for (B), LaunchCompareJob must be stateless.
    (B) will allow re-using of LaunchCompareJob for multiple executions with different arguments. If that's a possibility, (B) is better, cause you then create fewer objects.

Similar Threads

  1. Replies: 6
    Last Post: 04-05-2010, 10:58 PM
  2. arguments in command-line
    By girlet18 in forum New To Java
    Replies: 2
    Last Post: 01-21-2010, 03:05 PM
  3. Arguments in Main
    By CyberFrog in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 10:37 PM
  4. repetition of 'arguments'(?)
    By Igor in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 11:08 AM
  5. Variable No. of Arguments
    By Gajesh Tripathi in forum New To Java
    Replies: 2
    Last Post: 10-31-2007, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •