Program effiency

M

mmm.guitar

Hello,

I have a slighly odd question regarding coding style and program
efficiency which I would appreciate a short answer to.

Taking these two code examples to call a method and send a String from
an object and String made up on the spot:

Example A:

myMethod( myObject.getString(), "This is some text" );

Example B:

String my_string;
String my_text;

my_string = myObject.getString();
my_text = "This is some text";

myMethod (my_string , my_text);


Which one would more efficient to run and which would be seen as a
'better' coding style.

My thoughts - running effiency would the same, because the compiler I
think will compile everything down (i.e. great difference between
compiled code and written code) but I'm not sure about the specifics of
compiling, so given that Example B will take more time to compile. I
think example B is easier to understand as well and thus the better
coding style? - also considering a method may have say 10 inputs.

Or a compromise?

String my_string;

my_string = myObject.getString();

myMethod (my_string , "This is some text");


Thanks for any help, I am just trying to find a descent coding style.
 
L

lordy

Hello,

Thanks for any help, I am just trying to find a descent coding style.

Whichever looks best. Sometimes declaring holding variables with
meaningful names adds to the clarity. Sometimes it adds to the clutter.

If the value's purpose is not obvious from how it was obtained then
a suitably named holding variable makes it more obvious.

If the values purpose is clearly obvious from how it was obtained, then
a holding variable probably adds clutter, but not always.

Lordy
 
M

mmm.guitar

Thanks, was just thinking how exmaple A is quick and easy to read
becuase all the relavent info/variables are there on the one line (or
if many could be split onto multiple lines).

Cheers.
 
C

Chris Uppal

Which one would more efficient to run and which would be seen as a
'better' coding style.

Using intermediate variables to make the meaning of the code clear is in
general a good idea -- especially as Java's syntax doesn't attach a manifest
meaning to each parameter.

However it can be overdone, so it's not a thing to make a "religion" about.
Use extra variables wherever you think it makes the code easier to read and/or
understand.

BTW, if you are looking for a style to settle on, I heartily advocate the
practise of putting all the parameters onto the same line OR each one on a
separate line. I.e. don't write this:

aMethod(param1, 22, true, "some long string which doesn't fit",
param5, new JLable("stupid code", 22), false);

but:

aMethod(
param1,
22,
true,
"some long string which doesn't fit",
param5,
new JLable("stupid code", 22),
false);

Obviously there is nothing wrong with putting all the parameters on the same
line if they fit, and are not unreadable (as can be the case with nested method
calls).

-- chris
 
A

Alan Krueger

Thanks, was just thinking how exmaple A is quick and easy to read
becuase all the relavent info/variables are there on the one line (or
if many could be split onto multiple lines).

That's my personal preference for reading code. It does makes setting a
debugger breakpoint a little harder, since most debuggers have
line-based breakpoint identification.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top