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

C

Carl

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

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Carl said:
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
 
J

Jeffrey Schwab

Arne said:
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.
 
E

EJP

Jeffrey said:
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.
 
O

Oliver Wong

Robert Klemme said:
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
 
S

sgeos

Jeffrey said:
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
 
L

Luke Webber

sgeos said:
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
 
M

Michael Rauscher

EJP said:
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
 
S

sgeos

Luke said:
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
 
P

Patricia Shanahan

sgeos said:
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
 
C

Chris Uppal

sgeos said:
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
 
C

Chris Uppal

Patricia said:
. 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
 
P

Patricia Shanahan

Patricia said:
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
 
E

Eric Sosman

sgeos said:
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
 
S

sgeos

Eric said:
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
 
L

Luke Webber

sgeos said:
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
 
P

Patricia Shanahan

sgeos said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top