N
neelu111
what does this code means
data.getname() == null ) ? 0 : data.getname().size()
data.getname() == null ) ? 0 : data.getname().size()
what does this code means
data.getname() == null ) ? 0 : data.getname().size()
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.
what does this code means
data.getname() == null ) ? 0 : data.getname().size()
what does this code means
data.getname() == null ) ? 0 : data.getname().size()
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.
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
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
Much better to do something like:
name = data.getname();
size = ( name == null ) ? 0 : name.size();
That has nothing to do with style and everything with premature optimization.
Which is to be avoided.
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.