This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

To Variable Or Not To Variable  RSS feed

 
Rancher
Posts: 539
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fellow ranchers

I started programming in Java a itle over 2 years ago and it kinda feels weird to post something in the beginning java forum, but my question is actually a verry basic one, java 101 sort to say.
One of my three teachers who learned me how to code always hammered on the fact to eliminate the need for variables where ever possible.
The problem I have with that is for example let's say I have a personClass and that person has a name obviously.
I need that name in several places of my code, following his design I would call the getName method whenever I need the person's name, that seems to unnecessesarily slow down the program, wouldn't it make more sence to only call the getNamemethod once and asign it's value to a variable and use that in your code?
 
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that the difference between personClass and person.getClass() is vanishingly small, but we should test that assumption.

The advice, "Use as few variables as possible" needs to be taken with a grain of salt.  You can write some very unclear code trying to avoid creating a variable.  Especially local variables in a short method are fine.  Maybe what your teacher was saying is, "Use as few global variables as possible."  This a would agree with.
 
Daniel Demesmaecker
Rancher
Posts: 539
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Maybe what your teacher was saying is, "Use as few global variables as possible."  This a would agree with.


What do you mean by global variables? instance variables or static ones? Cause for the lather, my teacher would shoot me if he ever saw me declaring a static variable.
He already gets an hardattack when he sees me implementing a interface with a static utility method
 
Marshal
Posts: 67163
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:that seems to unnecessesarily [sic] slow down the program


Please post how you tested this, and the data that supports this assertion.
 
Daniel Demesmaecker
Rancher
Posts: 539
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:
Please post how you tested this, and the data that supports this assertion.


I haven't tested it, it was an assumption...
But by the general response I can conclude I'm better to folow his design?
 
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When presented with general context-free rules like that, I tend to be skeptical.

Andy Hunt's Rule #1: Always consider context.

"Eliminate the need for variables wherever possible" is a context-free rule. Did your professor tell you why variables should be avoided? Did he give examples that illustrate why they can cause problems?

There are many possible reasons for this "rule" but until we know what your instructor's specific reasons are, it's hard to say whether or not it's good advice.
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:that seems to unnecessesarily [sic] slow down the program


Since you say "seems" I assume you based that judgement on intuition/gut feelings. Intuition is not a good performance tuning tool. Neither are your gut feelings.

A very wise and respected computer scientist once said, "Premature optimization is the root of all evil." (Donald Knuth)
 
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:I started programming in Java a itle over 2 years ago and it kinda feels weird to post something in the beginning java forum, but my question is actually a verry basic one


After 5 years you may well post to the same forum again, and there wouldn't be anything bad about it.

You can work on the same fairly simple problem forever, each time trying to tackle it different way. You don't need to work on very complex problems to learn more advanced programming techniques. I think it is other way round, you can easier understand more advanced techniques by working on code subtleties of simple/-er problems.

And it is good for beginners at the same time, so we can all learn.
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get an idea of how important context is, take a look at these two seemingly conflicting refactorings. Both are in Martin Fowler's catalog of refactorings.

Extract Variable (aka Introduce Explaining Variable) - https://refactoring.com/catalog/extractVariable.html
Inline Variable - https://refactoring.com/catalog/inlineVariable.html

Examine both examples and see why the "after" version might be preferable to the "before" version.

This is just one example of a how some refactoring techniques are like certain pairs of sayings, like:

"Look before you leap" vs. "He who hesitates is lost."
"Many hands make light work" vs. "Too many cooks spoil the broth."
and many others.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think a good rule of thumb is to use a variable when you need to and that's dependent on what you're working on but if you require multiple calls like in your example I'd prefer to make a single call and assign that result to a variable, it's cleaner to me.

As a real life example the system I work on right now has very stringent code quality checks performed both locally for maven builds and as part of a jenkins build, and also the jenkins job to merge features to develop.

One sonar enforced build breaking rule is that you cannot have multiple return statements in any one method. So in this instance its considered better practice to assign to a single variable which is also the return variable.

I personally find code easier to read when variables are used over lots of nested method calls.
 
Liutauras Vilda
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Beeimbolo Paul wrote:I personally find code easier to read when variables are used over lots of nested method calls.


When you find that you have lots of nested method calls, probably isn't a bad idea to extract nested structures to methods with self revealing names, so you wouldn't end up dealing with arrow anti-pattern structure.

But you might had in mind chained method calls. In which case probably also could be suspicious why you need to pass over and over same stuff to various (or worse each and every) method(s).
 
Beeimbolo Paul
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess either, what I am trying to say is that I use variables when it makes sense to, and if that means code is easier to read then I would opt for it.

I don't work on systems with low memory requirements so declaring variables isn't any issue. I just use them when I need to and don't specifically try to go out of my way to write code that avoids using them.

On the same note I don't use them when there is no need, for example to hold the result of a call that I then return immediately.

As one of the posts above describes, it's context specific.
 
Bear Bibeault
Marshal
Posts: 67163
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Andy Hunt's Rule #1: Always consider context.


Quoted for emphasis.
 
Liutauras Vilda
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Beeimbolo Paul wrote:... if that means code is easier to read then I would opt for it.


That sounds like a very good measurement metric
 
Knute Snortum
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:


If there's one thing the internet has taught me, it's that words printed with a picture of Albert Einstein have to be true!
 
Knute Snortum
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:

Knute Snortum wrote:Maybe what your teacher was saying is, "Use as few global variables as possible."  This a would agree with.


What do you mean by global variables? instance variables or static ones? Cause for the lather, my teacher would shoot me if he ever saw me declaring a static variable.
He already gets an hardattack when he sees me implementing a interface with a static utility method


Yeah, "global variables" was a poor choice of words.  Is there a term for variables that are declared outside of any method, be they static or instance?  If there's a phrase of that, I'll use it next time.
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Is there a term for variables that are declared outside of any method, be they static or instance?  If there's a phrase of that, I'll use it next time.


Those are referred to as "member variables," which can be either class members or instance members. https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:my teacher would shoot me if he ever saw me declaring a static variable.
He already gets an hardattack when he sees me implementing a interface with a static utility method



Remind your teacher that Math.PI is a public static variable in the Standard Java Library, as are many others. Remind him also that java.util.Math is all static utility methods. There is a time and place for everything so don't just blindly follow rules like that, especially if they don't provide any context. Know why the reasons behind the rule and what following or not following it does for you.

 
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe the teacher was trying to explain that the more fields there are the more there is to go wrong, and this instruction was simply encouragement to use as few fields as possible.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!