views:

171

answers:

5

Hi there,

I'm a programming student in my 2nd OOP course, which is taught in C++. I know that it is generally bad practice to use magic numbers in code, so here's my question:

In the next program I have to write for this class, there is over 120 numbers given to us in tax tables, and we need to use them to compute tax and other relevant info. With such a large amount of numbers, do I define a constant for each number? Or is there something else I can do?

+8  A: 

Constants would be more appropriate than magic numbers -

However, with that many "constants", and something that changes over time (tax tables), I personally would load these via a configuration file, and use some type of dictionary lookup for the individual values. This would make it much easier to adjust to new tax tables without recompilation.

Reed Copsey
I don't know what your jurisdiction is like, but round my way it pretty much never happens that the government changes the tax rates and thresholds, without also tinkering with the taxation system itself sufficiently that you'd have to rewrite the code anyway (new deductions, allowances, etc). I know a non-programmer who worked as the interface between some programmers and some financial regulations. He didn't enjoy it that much. I'd still use a config file, though, if only to help with testing and errata, and the tiny hope that maybe next year's changes will be simple ;-)
Steve Jessop
Yeah - true. In the US, this isn't an issue. Some countries, though, change tax rates much more often than whole tax codes... Also, some states and local jurisdictions have more fluid (local) tax rates.
Reed Copsey
@Reed Your answer was clear and concise, thank you!
Alex
+1  A: 

The question is, how will your code use them? If you really have to write code that uses each of these in a specific and idiosyncratic way, then you might as well turn them all into constants.

If, on the other hand, you iterate through them, some sort of associative data structure (think, 'STL map') might be more helpful.

One more consideration: if someone expects your program to digest a new set every so often, then you need constants for the names, and an data structure you load with the values.

bmargulies
+3  A: 

Think what the data structures you would end up with look like, then decide on what constants or enums you need. (Not a tax expert so I am going to guess a bit here)

A tax rates in various states

 std::map<std::string, double> stateRates;
 stateRates["CA"] = 20.7;
 stateRates["MN"] = 1.2;
 ....

I dont see a need for constants here although some people would enum the state names

 enum States
 {
     CA, MN
 };
 std::map<States, double> stateRates;
 stateRates[CA] = 20.7;
 stateRates[MN] = 1.2;

But then you also need to convert from string CA to enum CA

But for non tabular stuff then you really need constants

 const int EXTRA_TAX_FOR_BIG_HOUSE = 2;
 const int BIG_HOUSE_LIMIT = 6000; // sqft

 if(houseSize > BIG_HOUSE_LIMIT)
    rate += EXTRA_TAX_FOR_BIG_HOUSE;
pm100
The above will be perfect for the scenario). I was just about to answer and pm100 already did it. So +1 for that. Use of maps will be perfect.
rocknroll
A: 

Reed Copsey has the right answer, but I don't know whether the answer was clear enough.

When you have so many different "constants", it is better to put them in something external, like a file or a database. These so-called "constants" tend to change: tax rates for different states change as legislatures decide that they need more money.

If you store these magic numbers in a file, then use ifstream to read these constants. If you use a database to store these magic numbers, then you need to read the documentation for that database.

What you should store the numbers in depends on what you use them for. For example, if you just have state taxes, use a map to go from a state name (or abbreviation) to its tax rate.

Good luck!

Chip Uni
+1  A: 

Usually with assignments such as this, you will be taught how to read/write from a file. You can then load those numbers into arrays or vectors or whatever you may need. If you haven't been taught the read and write from a file, it's fairly simple to pick up.

Here's a tutorial you could look at, but there are plenty more with a quick google search http://fredosaurus.com/notes-cpp/io/readtextfile.html

Justen