Code

C

Christophe Vanfleteren

what does this code means
data.getname() == null ) ? 0 : data.getname().size()

This is called the ternary operator.

It consists of 3 parts:
boolean condition ? value when true : value when false

So this code will return 0 if getName() returns null, or size() when
getName() is not null.
 
G

Guest

Christophe said:
This is called the ternary operator.

It consists of 3 parts:
boolean condition ? value when true : value when false

So this code will return 0 if getName() returns null, or size() when
getName() is not null.

But it call a second-time getName() if the first result was != null.
Probably it's OK, probably it's not...

- Dario
 
W

Will

what does this code means
data.getname() == null ) ? 0 : data.getname().size()

If you're asking what the ? : operator is, it is a shorthand if-else
statement i.e. if data.getname() == null evaluates as true, 0 is returned,
otherwise data.getname().size() is returned. For example:

int size = data.getname() == null ? 0 : data.getname().size();

Regards, Will
 
B

Balaji. M.

what does this code means
data.getname() == null ) ? 0 : data.getname().size()

Basically, the code does the job of calling the "size" method of the
object, which returned by data.getname().

if we try to attempt to do an operation with a null object, it will
raise an error.

to avoid causing error, the code is checking for the returned object is
null or not.
 
L

Liz

Balaji. M. said:
Basically, the code does the job of calling the "size" method of the
object, which returned by data.getname().

If I understand correctly, size() is not always called.
 
J

Joona I Palaste

If I understand correctly, size() is not always called.

You understand correctly. Balaji gives furthe details below.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
 
F

Fred L. Kleinschmidt

Joona said:
You understand correctly. Balaji gives furthe details below.


--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge

A poor coding style in general, however. For this particular case it may
be OK, if one assumes that data.getname() is some single-line function
that merely returns a private String, so its overhead is insignificant.

However, it is possible that the getname() method might do a lot of work
(such as making a DB query, or solving a 12000 variable linear equation,
or...). In this case it it very irresponsible to call getname() twice
(it can't be optimized to keepo the results of the first call, sionce
there may be internal side effects).
Much better to do something like:
name = data.getname();
size = ( name == null ) ? 0 : name.size();
 
M

Michael Borgwardt

Fred said:
A poor coding style in general, however. For this particular case it may
be OK, if one assumes that data.getname() is some single-line function
that merely returns a private String, so its overhead is insignificant.

However, it is possible that the getname() method might do a lot of work
(such as making a DB query, or solving a 12000 variable linear equation,
or...). In this case it it very irresponsible to call getname() twice

That has nothing to do with style and everything with premature optimization.
Which is to be avoided.
Much better to do something like:
name = data.getname();
size = ( name == null ) ? 0 : name.size();

Not better. Slightly worse, actually, as far as style is concerned, because
it creates an additional variable, i.e. status.
 
R

Roedy Green

That has nothing to do with style and everything with premature optimization.
Which is to be avoided.

There is a another reason to avoid the duplication -- it is the more
normal way people code. You don't want to do things that make people
do a double take.
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top