Can these two statements be made into one statement?

O

Oxnard

I'm still trying to beome a bit more comfortable with abstract classes. Here
are the statements:

FontMetrics fm = c.getFontMetrics(c.getFont());
int i = 2 * (fm.getMaxAscent() + fm.getMaxDescent());

Thanks
 
P

Paul Lutus

Oxnard said:
I'm still trying to beome a bit more comfortable with abstract classes.
Here are the statements:

FontMetrics fm = c.getFontMetrics(c.getFont());
int i = 2 * (fm.getMaxAscent() + fm.getMaxDescent());

Why do you want to combine them? They look entire reasonable and readable as
they are. The shortest code expression is not necessarily the best.
 
A

Ann

how about a 'for' statement

for(FontMetrics fm=c.getFontMetrics(c.getFont()),int
i=2*(fm.getMaxAscent()+fm.getMaxDescent());i<0;;){}
 
P

Paul Lutus

Ann said:
how about a 'for' statement

for(FontMetrics fm=c.getFontMetrics(c.getFont()),int
i=2*(fm.getMaxAscent()+fm.getMaxDescent());i<0;;){}

A perverse piece of code, but I think you knew that. :)
 
J

Joona I Palaste

Oxnard said:
I'm still trying to beome a bit more comfortable with abstract classes. Here
are the statements:
FontMetrics fm = c.getFontMetrics(c.getFont());
int i = 2 * (fm.getMaxAscent() + fm.getMaxDescent());

This is really as close as you're going to get. If you can guarantee
c.getFontMetrics(c.getFount()) will always return the same instance of
FontMetrics for the same c.getFount() you can try:
int i = 2 * (c.getFontMetrics(c.getFont()).getMaxAcent() +
c.getFontMetrics(c.getFont()).getMaxDescent())));

But I have to agree with the other replies, your code is fine as it is.
Brevity is not an end-in-itself, only combined with clarity.
Note that this has nothing to do with abstract classes. Your question,
and its answers, would remain the same even with concrete classes.

I've myself often wished for temporary variables in Java, though... so I
could do something like this:

int i = 2 * (FontMetrics fm =
c.getFontMetrics(c.getFont()).getMaxAscent() + fm.getMaxDescent());

However, I don't think that's going to be pretty easy to implement in
the compiler. I know a little about compiler theory so I know
something about what compilers can easily do and what they can't.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
- MAD Magazine
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top