jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Incrementing and Decrementing Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incrementing and Decrementing Variables" Watch "Incrementing and Decrementing Variables" New topic

Incrementing and Decrementing Variables

Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
I have a program in which I have to use a loop and a series of if statements to count through something. Each if statement calls a different method, but the for loop enclosing them all using a variable each if statement uses.

In each method, the functions inside depend on the value of the variable (which I pass to them) to maintain their correct spot within an array. They also use another variable (which is passed to them, them returned) to maintain the index of an array in which I don't want the calling function to have any kind of manipulation.

So, when I pass the value that they don't return, but I need it decremented, is it bad programming practice to do this:


Or is it better to do this:

someVariable = someVariable - 3;

The very existence of flamethrowers proves that at some time, some where, some place, someone once said to themselves "I'd really like to set those people on fire over there, but I just can't get close enough".
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The second form is better; it's clearer, and you also have the chance to replace the meaningless "3" with a named constant like RECORD_LENGTH or whatever "3" actually means.

[Jess in Action][AskingGoodQuestions]
M. Lee Smith

Joined: Aug 29, 2006
Posts: 2
The second method is by far the better route. Cleaner code and the ability to expand on it, rather than being stuck with a preset solution.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Hm, I would use

someVariable -= 3;

with a good chance of replacing the 3 with a meaningful constant name (as EFH noted).

"I'm not back." - Bill Harding, Twister
Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
3 is just that, 3. I'm using a reverse counter to count down until I know there are no more array indexes left.

I appreciate the responses. You've helped me solve something that was really bugging me (what looked like sloppy code).
marc weber

Joined: Aug 31, 2004
Posts: 11343

From Wikipedia - Magic number (programming)...
The term magic number also refers to the bad programming practice of using numbers directly in source code without explanation. In most cases this makes programs harder to read, understand, and maintain. Although most guides make an exception for the numbers zero and one, it is a good idea to define all other numbers in code as named constants.

For example...

static final MILLISECS_IN_DAY = 1000 * 60 * 60 * 24;

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
So even if we have a method that does something, and we know exactly by how much it does something, we should assign a number controlling it a variable name?

Like in my case, I know that the method will only deal with three indexes in the array (and other methods a differing amount) because that's all the method can handle at a time.

So rather than:
marker -= 3;

It would be proper to put:
marker -= MARKER_METHOD1;

Edit:: Ok, after reading the article more thoroughly I see why. Of course I know what the number is and why I use it, but if I've written hieroglyphics and included no Stone, another programmer later may wish death upon me.
[ December 13, 2006: Message edited by: Nathan Leniz ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

For example...

static final MILLISECS_IN_DAY = 1000 * 60 * 60 * 24;

Sorry, couldn't resist

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Garrett Rowe:
...Sorry, couldn't resist

I knew that example would draw fire, but I thought it would be for daylight savings adjustments.

Note: 1000 * 60 * 60 * 24 is just a nicety, saving someone the trouble of verifying that 86400000 is (usually) correct. But yeah, it's still technically "magic numbers."
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

So even if we have a method that does something, and we know exactly by how much it does something, we should assign a number controlling it a variable name?

Personally, I would say that yes, you should. In this example, you're right, it's really not that big a deal. But let's examine this like it was a "real world" program.

It is going to be developed and maintained over time - perhaps years. New features will be added. Enhancements made. You know there are only 3 indexes, so you keep using the contstant of 3, over and over and over. Then, somebody else adds in some new feature, that by coincidence, also uses the number 3. You get to the point where there are dozens and dozens of "3"'s in your code.

Now, the new boss comes along. He says, "Yeah, 3 is great for your stuff, but we really need it to be 4!!!".

What do you do? You have to slog through hundreds of lines of code, looking at each and every 3, and decide if that specific one needs to be changed, or if it's a 3 that is not related to your three. It might take days, and you're probably going to miss a few that may not be discovered until it's caused serious damage somehow.

Now, write your code with a defined constant. Every new feature uses that defined constant. When somebody writes those new, unrelated features, they create their OWN constant, which also happens to have a value of 3.

NOW when it comes time to change yours to a 4, it'll take you about five minutes to find where it's defined, change one charater, save the file, and recompile. You're done, you KNOW you got ALL the changes you needed, and ONLY the changes you needed.

So, for a simple program like this, it probably doesn't make much difference. But it's a DARN good habit to get into now.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Don't get me started about those stupid light bulbs.
subject: Incrementing and Decrementing Variables
It's not a secret anymore!