• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Incrementing and Decrementing Variables

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

someVariable--;
someVariable--;
someVariable--;

Or is it better to do this:

someVariable = someVariable - 3;
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm, I would use

someVariable -= 3;

with a good chance of replacing the 3 with a meaningful constant name (as EFH noted).
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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_METHOD1 = 3;
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 ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


For example...

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





Sorry, couldn't resist
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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."
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic