Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Program effiency

Thread view: 
mmm.guitar@gmail.com - 15 Jul 2006 00:33 GMT
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.
lordy - 15 Jul 2006 00:39 GMT
> 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
mmm.guitar@gmail.com - 15 Jul 2006 00:50 GMT
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.

> > Hello,
> >
[quoted text clipped - 10 lines]
>
> Lordy
Alan Krueger - 16 Jul 2006 20:47 GMT
> 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.
Chris Uppal - 15 Jul 2006 11:07 GMT
> 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
mmm.guitar@gmail.com - 15 Jul 2006 16:36 GMT
Ah thanks, that makes some good sense as well.  Cheers.

> > Which one would more efficient to run and which would be seen as a
> > 'better' coding style.
[quoted text clipped - 30 lines]
>
>     -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.