|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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! ![]() |
|
#3
|
||||
|
||||
|
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. =) |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
||||
|
||||
|
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... |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
||||
|
||||
|
Quote:
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. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Small question about variables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|