Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 28th, 2004, 06:14 PM
Toridas Toridas is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 1 Toridas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Small question about variables

Is it good "form" to declare variables that aren't really needed? In my Java class (a university class, not a *.class) we had to make a program that retreived the area of a circle, using the areaOf method from our Circle class. The circle1 is an object of the Circle class.

She said that the proper way to do it is:
double area = circle1.areaOf();
System.out.println(area);


But I find it better to just do:
System.out.println(circle1.areaOf());
and get rid of the unneeded declaration.

Is there a preferred way to do things like this or are both equally acceptable?

The reason I ask is because our newest assignment is to make a program where we are supposed to have 8 or 9 variables like this, although I have mine working just fine with only 1.

Reply With Quote
  #2  
Old January 28th, 2004, 10:34 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Toridas,

While your code would definitely work, it's recommended to use the former example you provided, mainly for code readability.

If you're develping in J2ME, there are arguments for both sides of the table. One side saying you should use as few variables as possible, the other arguing that lack of variable use can slow down the performance of your application.

It's basically a performance vs. application size... Choose wisely, as they say!

Reply With Quote
  #3  
Old January 29th, 2004, 12:10 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
I prefer the first example...

I find it easier for readability, let alone if you decide on you want to use the area later on in the program, you don't need to call the method again... Similarly if you're calling it remotely, calling the method would take significantly longer and it would be undesirable to call it a couple times to acheive the same results.

I must say that I tend to get hypocritical...
sometimes my laziness takes control of me. =)

Reply With Quote
  #4  
Old January 29th, 2004, 08:12 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
My view is that if there's ambiguity or muddiness that assigning something to an aptly-named variable will help prevent ("hypoteneuse=sqrt((x*x)-(y*y))" for example), it's good to use a variable (or add appropriate comments). Additionally, if a value's going to be used more than once, particularly across functions/objects, it's probably better to stick it in a variable that can be passed around (preferably by reference).

To my mind, usign circle1.areaOf() is sufficiently descriptive that if you're doing it only once or twice, there's no need to assign it to a variable. It's largely a matter of personal coding style, though.

Reply With Quote
  #5  
Old January 29th, 2004, 09:25 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
Toridas,
Just to understand this correctly? YOur instructor said the best way is using the variable assignment (your first example)?
I guess this is mostly so she/he will understand the code a little bit easier... As dhouston's touched on, its the best way to avoid variable ambiguity... just make sure you name the variables something useful and not just "a" and "b"

Quite frankly, if that's what you're teacher prefers, you're best off doing it that way =)
You'll figure out which way you prefer and begin doing it that way as well...

Reply With Quote
  #6  
Old February 6th, 2004, 10:22 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Exactly... You'll find your own style eventually. As for the teacher's instructions, if there's one thing I've learned, is to _always_ follow them... MadCow knows what I'm talking about (ie: My CSS use when it wasn't required - LOL).

As you progress further into your program, you'll find that most teachers don't care which style of coding you use, so long as they can understand it. Remember, you're not coding just for yourself... You're coding for the person who determines your grade!
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #7  
Old February 7th, 2004, 12:50 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
Quote:
...you're not coding just for yourself...


I wish more developers would consider this when they're using variables...
I'm trying to keep the usual rant about indenting and variable names from appearing as its kind of off-topic here... but again, I think its up to you as to which type of variable/argument passing you use...

I'll plug my preference again... =)
double area = circle1.areaOf();
System.out.println(area);


I personally find this clearly written and easy for another developer to read and understand.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Small question about variables


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap