Why use "return (null);" instead of "return null;" ?

Discussion in 'Java' started by Carl, Aug 21, 2006.

  1. Carl

    Carl Guest

    I found "return (null);" in the source of struts's ActionForm class.
    Why not "return null;" ? Is "return (null);" better than "return null;"
    ?
     
    Carl, Aug 21, 2006
    #1
    1. Advertising

  2. Carl wrote:
    > I found "return (null);" in the source of struts's ActionForm class.
    > Why not "return null;" ? Is "return (null);" better than "return null;"
    > ?


    No difference.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 21, 2006
    #2
    1. Advertising

  3. Arne Vajhøj wrote:
    > Carl wrote:
    >> I found "return (null);" in the source of struts's ActionForm class.
    >> Why not "return null;" ? Is "return (null);" better than "return null;"
    >> ?

    >
    > No difference.


    Except that one has more line noise. return(null) is a pet peeve of mine.

    Some people prefer to use the parentheses for consistency with function
    calls, loop tests, etc. I think they're also secretly insecure about
    operator precedence, and can't shake the feeling that a statement like
    "return 5 + x" will return five, discarding the value of x and losing
    forever the knowledge of what it contained.
     
    Jeffrey Schwab, Aug 21, 2006
    #3
  4. Carl

    EJP Guest

    Jeffrey Schwab wrote:

    > return(null) is a pet peeve of mine.


    and mine

    > Some people prefer to use the parentheses for consistency with
    > function calls, loop tests, etc.


    Apparently so. The trouble with that position is that 'return' *isn't* a
    function call or a loop. And in C/C++/Java the parentheses are
    specifically required around loop-expressions only because there is no
    'do' as in Algol, PL/1, Pascal, ...

    And it is certainly a pet peeve of mine that we went to all the trouble
    of inventing formula translators fifty years ago so we could use
    arithmetic precedence rather than reverse Polish, and there are *still*
    people afraid to use it.
     
    EJP, Aug 21, 2006
    #4
  5. Carl

    Ed Guest

    Ed, Aug 21, 2006
    #5
  6. On 21.08.2006 10:02, Ed wrote:
    > Carl skrev:
    >
    >> I found "return (null);" in the source of struts's ActionForm class.
    >> Why not "return null;" ? Is "return (null);" better than "return null;"
    >> ?

    >
    > http://java.sun.com/docs/codeconv/html/CodeConventions.doc6.html#438


    This sample from that URL is similarly silly:

    return (size ? size : defaultSize);

    This statement has exactly the same semantics and there is absolutely no
    need for the brackets:

    return size ? size : defaultSize;

    Kind regards

    robert
     
    Robert Klemme, Aug 21, 2006
    #6
  7. Carl

    Oliver Wong Guest

    "Robert Klemme" <> wrote in message
    news:...
    > On 21.08.2006 10:02, Ed wrote:
    >> Carl skrev:
    >>
    >>> I found "return (null);" in the source of struts's ActionForm class.
    >>> Why not "return null;" ? Is "return (null);" better than "return null;"
    >>> ?

    >>
    >> http://java.sun.com/docs/codeconv/html/CodeConventions.doc6.html#438

    >
    > This sample from that URL is similarly silly:
    >
    > return (size ? size : defaultSize);
    >
    > This statement has exactly the same semantics and there is absolutely no
    > need for the brackets:
    >
    > return size ? size : defaultSize;


    With the parenthesis, the return value is "more obvious in some way".

    - Oliver
     
    Oliver Wong, Aug 21, 2006
    #7
  8. Carl

    EJP Guest

    Oliver Wong wrote:

    > With the parenthesis, the return value is "more obvious in some way".


    To whom? Someone who doesn't understand the syntax of the statement? So?
     
    EJP, Aug 22, 2006
    #8
  9. Carl

    sgeos Guest

    Jeffrey Schwab wrote:
    > Some people prefer to use the parentheses for consistency with function
    > calls, loop tests, etc. I think they're also secretly insecure about
    > operator precedence,


    I follow the "BEDMAS, parentheses around everything else" rule.
    Easy to remember and everyone (with what, at least a sixth grade
    education?)
    should be able to understand the code even if they don't like
    parentheses.

    if ( (x || y) && z )
    return x;

    -Brendan
     
    sgeos, Aug 22, 2006
    #9
  10. Carl

    Luke Webber Guest

    sgeos wrote:
    > Jeffrey Schwab wrote:
    >> Some people prefer to use the parentheses for consistency with function
    >> calls, loop tests, etc. I think they're also secretly insecure about
    >> operator precedence,

    >
    > I follow the "BEDMAS, parentheses around everything else" rule.
    > Easy to remember and everyone (with what, at least a sixth grade
    > education?)
    > should be able to understand the code even if they don't like
    > parentheses.
    >
    > if ( (x || y) && z )
    > return x;


    Even BEDMAS doesn't hold true in Java, or in any other language I've
    used. Division and multiplication have the same precedence, as do
    subtraction and addition. Where precedence is equal, it goes left to
    right. Whereas with BEDMAS, division comes before multiplication and
    addition before subtraction. Which is a pretty screwed up rule, when you
    think about it. But I guess /some/ kind of rule is better than none at
    all. <g>

    Luke
     
    Luke Webber, Aug 22, 2006
    #10
  11. EJP schrieb:
    > Oliver Wong wrote:
    >
    >> With the parenthesis, the return value is "more obvious in some way".

    >
    > To whom? Someone who doesn't understand the syntax of the statement? So?


    It works similiar to serifs in fonts. The additional grouping
    "highlights" the return value, making it more obvious for human readers.

    Another example:

    a = f = b == c ? d : e;

    To figure out the return value one would have to _read_ the line.

    If this expression had been written as

    a = f = (b == c ? d : e);

    everyone knows immediately what the return value is - just by looking at
    the line.

    Bye
    Michael
     
    Michael Rauscher, Aug 22, 2006
    #11
  12. Carl

    sgeos Guest

    Luke Webber wrote:
    > Even BEDMAS doesn't hold true in Java, or in any other language I've
    > used. Division and multiplication have the same precedence, as do
    > subtraction and addition.


    Division and multiplication are the same operation, as are addition and
    subtraction. In any case, unless you are implicitly flooring your
    results, puting division before multiplication shouldn't change
    anything. I can't see a situation where addition before subtraction
    make a difference. Care to enlighten me?

    -Brendan
     
    sgeos, Aug 22, 2006
    #12
  13. sgeos wrote:
    > Luke Webber wrote:
    >> Even BEDMAS doesn't hold true in Java, or in any other language I've
    >> used. Division and multiplication have the same precedence, as do
    >> subtraction and addition.

    >
    > Division and multiplication are the same operation, as are addition and
    > subtraction. In any case, unless you are implicitly flooring your
    > results, puting division before multiplication shouldn't change
    > anything. I can't see a situation where addition before subtraction
    > make a difference. Care to enlighten me?
    >
    > -Brendan
    >


    I don't quite understand what you are saying here. First of all, (a-b)+c
    is not at all the same expression as a-(b+c), and (a/b)*c is not the
    same as a/(b*c). Addition and subtraction are different operations,
    although they have equal precedence in Java.

    Even ignoring that issue, for integer types multiplication and division
    are non-associative, and for floating point types none of
    multiplication, division, addition, and subtraction is associative. The
    fact that Java evaluates a+b+c as (a+b)+c not a+(b+c) really does matter.

    Patricia
     
    Patricia Shanahan, Aug 22, 2006
    #13
  14. Carl

    Chris Uppal Guest

    sgeos wrote:

    > I can't see a situation where addition before subtraction
    > make a difference. Care to enlighten me?


    ============
    import java.math.BigDecimal;

    public class Test
    {
    public static void
    main(String[] args)
    {
    double big = (double)(1L<<40);
    double ulp = Math.ulp(big);
    double halfUlp = ulp / 2.0;

    print("big", big);
    print("ulp", ulp);
    print("halfUlp", halfUlp);
    print("(big - ulp) + halfUlp", (big - ulp) + halfUlp);
    print("big - (ulp + halfUlp)", big - (ulp + halfUlp));
    }

    static void
    print(String s, double d)
    {
    System.out.print(s);
    System.out.print('\t');

    // Patricia's technique for easily visualising the exact
    // value of a double
    BigDecimal bd = new BigDecimal(d);
    System.out.println(bd.toPlainString());
    }
    }
    ============

    -- chris
     
    Chris Uppal, Aug 22, 2006
    #14
  15. Carl

    Chris Uppal Guest

    Patricia Shanahan wrote:

    >. First of all, (a-b)+c
    > is not at all the same expression as a-(b+c),


    Um, yes. So it isn't...

    Please ignore my own reply to Brendan (sgeos).

    I should think more when coding/posting :-(

    -- chris
     
    Chris Uppal, Aug 22, 2006
    #15
  16. Patricia Shanahan wrote:
    > sgeos wrote:
    >> Luke Webber wrote:
    >>> Even BEDMAS doesn't hold true in Java, or in any other language I've
    >>> used. Division and multiplication have the same precedence, as do
    >>> subtraction and addition.

    >>
    >> Division and multiplication are the same operation, as are addition and
    >> subtraction. In any case, unless you are implicitly flooring your
    >> results, puting division before multiplication shouldn't change
    >> anything. I can't see a situation where addition before subtraction
    >> make a difference. Care to enlighten me?
    >>
    >> -Brendan
    >>

    >
    > I don't quite understand what you are saying here. First of all, (a-b)+c
    > is not at all the same expression as a-(b+c), and (a/b)*c is not the
    > same as a/(b*c). Addition and subtraction are different operations,
    > although they have equal precedence in Java.
    >
    > Even ignoring that issue, for integer types multiplication and division
    > are non-associative, and for floating point types none of
    > multiplication, division, addition, and subtraction is associative. The
    > fact that Java evaluates a+b+c as (a+b)+c not a+(b+c) really does matter.


    Correction: integer multiplication is associative, although a mix of
    multiplication and division is not.

    Patricia
     
    Patricia Shanahan, Aug 22, 2006
    #16
  17. Carl

    Eric Sosman Guest

    sgeos wrote:

    > Luke Webber wrote:
    >
    >>Even BEDMAS doesn't hold true in Java, or in any other language I've
    >>used. Division and multiplication have the same precedence, as do
    >>subtraction and addition.

    >
    >
    > Division and multiplication are the same operation, as are addition and
    > subtraction. In any case, unless you are implicitly flooring your
    > results, puting division before multiplication shouldn't change
    > anything. I can't see a situation where addition before subtraction
    > make a difference. Care to enlighten me?


    a - b + c

    With "addition before subtraction" this is equivalent to

    a - (b + c)

    With "additions and subtractions together, left to right"
    (as in Java) it is equivalent to

    (a - b) + c

    --
    Eric Sosman
    lid
     
    Eric Sosman, Aug 22, 2006
    #17
  18. Carl

    sgeos Guest

    Eric Sosman wrote:
    > sgeos wrote:
    > > I can't see a situation where addition before subtraction
    > > make a difference. Care to enlighten me?

    >
    > a - b + c
    >
    > With "addition before subtraction" this is equivalent to
    >
    > a - (b + c)
    >
    > With "additions and subtractions together, left to right"
    > (as in Java) it is equivalent to
    >
    > (a - b) + c


    My bad. Sorry. Should have double checked on paper
    before posting. =P

    I tend to think of subtraction as shorthand for + -value, in
    which case the above resolves to:

    a + -b + c

    Unless I've made a logic error, the following should be true:

    (a + -b) + c == a + (-b + c)

    Likewise, I think of division as shorthand for * (1/value).

    -Brendan
     
    sgeos, Aug 23, 2006
    #18
  19. Carl

    Luke Webber Guest

    sgeos wrote:
    > Eric Sosman wrote:
    >> sgeos wrote:
    >>> I can't see a situation where addition before subtraction
    >>> make a difference. Care to enlighten me?

    >> a - b + c
    >>
    >> With "addition before subtraction" this is equivalent to
    >>
    >> a - (b + c)
    >>
    >> With "additions and subtractions together, left to right"
    >> (as in Java) it is equivalent to
    >>
    >> (a - b) + c

    >
    > My bad. Sorry. Should have double checked on paper
    > before posting. =P
    >
    > I tend to think of subtraction as shorthand for + -value, in
    > which case the above resolves to:
    >
    > a + -b + c
    >
    > Unless I've made a logic error, the following should be true:
    >
    > (a + -b) + c == a + (-b + c)
    >
    > Likewise, I think of division as shorthand for * (1/value).


    It all works out to the same thing in Java, but not in BEDMAS. That's
    why I'm kinda down on BEDMAS. In Java...

    a * b / c
    means
    (a * b) / c

    ....but if you apply BEDMAS, it means

    a * (b / c)

    And before somebody (more or less correctly) points out that these
    amount to the same thing, try it with the integer terms a=20, b=2, c=10. <g>

    The whole "division before multiplication / addition before subtraction"
    thing is perfectly aritrary. I find the rules of precedence embodied in
    Java (and every other programming language I've used) to be much more
    sensible.

    Luke
     
    Luke Webber, Aug 23, 2006
    #19
  20. sgeos wrote:
    > Eric Sosman wrote:
    >> sgeos wrote:
    >>> I can't see a situation where addition before subtraction
    >>> make a difference. Care to enlighten me?

    >> a - b + c
    >>
    >> With "addition before subtraction" this is equivalent to
    >>
    >> a - (b + c)
    >>
    >> With "additions and subtractions together, left to right"
    >> (as in Java) it is equivalent to
    >>
    >> (a - b) + c

    >
    > My bad. Sorry. Should have double checked on paper
    > before posting. =P
    >
    > I tend to think of subtraction as shorthand for + -value, in
    > which case the above resolves to:
    >
    > a + -b + c
    >
    > Unless I've made a logic error, the following should be true:


    I think you are making the logic error of assuming that identities that
    are true in some arithmetic systems, such as real number arithmetic,
    necessarily apply to other arithmetic systems.

    >
    > (a + -b) + c == a + (-b + c)


    public class AssociativityTest {
    public static void main(String[] args) {
    double a = 1e-30;
    double b = 1;
    double c = 1;

    System.out.printf("(a + -b) + c: %g, a + (-b + c): %g",
    (a + -b) + c,
    a + (-b + c)
    );
    }
    }


    prints:

    (a + -b) + c: 0.00000, a + (-b + c): 1.00000e-30

    >
    > Likewise, I think of division as shorthand for * (1/value).


    public class IntDivideTest {
    public static void main(String[] args) {
    System.out.printf("80/20: %d, 80*(1/20): %d", 80/20, 80*(1/20));
    }
    }

    prints:

    80/20: 4, 80*(1/20): 0

    Patricia
     
    Patricia Shanahan, Aug 23, 2006
    #20
    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. Usenet Poster!!!
    Replies:
    4
    Views:
    1,830
    Eric Sosman
    Sep 30, 2004
  2. Mick
    Replies:
    2
    Views:
    9,557
    Marcin Grunwald
    Feb 23, 2005
  3. Replies:
    44
    Views:
    948
    Timo Virkkala
    Jan 6, 2005
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    913
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,008
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page