Are Constants Really Appropriate Here, Or Is There Another Approach? - C++
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 tabl开发者_如何转开发es, 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?
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.
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;
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.
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
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!
精彩评论