将这些应考虑的幻数? [英] Would These Be Considered Magic Numbers?
问题描述
我刚刚完成了编写程序的编程类,我想避免使用幻数,所以这里是我的问题:
I've just completed writing a program for a programming class, and I want to avoid use of magic numbers, so here's my question:
在下面的功能,将我的数组索引被视为神奇数字?
In the function below, would my array indexers be considered magic numbers?
code:
string CalcGrade(int s1, int s2, int s3, double median)
{
const int SIZE = 23;
const int LETTER_GRADE_BARRIERS[SIZE] = { 400, 381, 380, 361, 360, 341, 340, 321, 320, 301, 300, 281, 280, 261, 260, 241, 240, 221, 220, 201, 200, 181, 180 };
double finalGrade;
string letterGrade;
finalGrade = s1 + s2 + s3 + median;
if (finalGrade >= LETTER_GRADE_BARRIERS[1] && finalGrade <= LETTER_GRADE_BARRIERS[0])
{
letterGrade = "A";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[3] && finalGrade <= LETTER_GRADE_BARRIERS[2])
{
letterGrade = "A-";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[5] && finalGrade <= LETTER_GRADE_BARRIERS[4])
{
letterGrade = "B+";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[7] && finalGrade <= LETTER_GRADE_BARRIERS[6])
{
letterGrade = "B";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[9] && finalGrade <= LETTER_GRADE_BARRIERS[8])
{
letterGrade = "B-";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[11] && finalGrade <= LETTER_GRADE_BARRIERS[10])
{
letterGrade = "C+";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[13] && finalGrade <= LETTER_GRADE_BARRIERS[12])
{
letterGrade = "C";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[15] && finalGrade <= LETTER_GRADE_BARRIERS[14])
{
letterGrade = "C-";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[17] && finalGrade <= LETTER_GRADE_BARRIERS[16])
{
letterGrade = "D+";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[19] && finalGrade <= LETTER_GRADE_BARRIERS[18])
{
letterGrade = "D";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[21] && finalGrade <= LETTER_GRADE_BARRIERS[20])
{
letterGrade = "D-";
}
else if (finalGrade <= LETTER_GRADE_BARRIERS[22])
{
letterGrade = "Fail";
}
return letterGrade;
}
谢谢!
推荐答案
是的,任何数量比-1,0或1的可能是一个神奇的数字。
Yes, any number other than -1,0 or 1 is probably a magic number.
除非你是一个真正的大师,那么你很可能获准使用的两个大国自由,以及: - )
Unless you're a real guru, then you're probably allowed to use powers of two freely as well :-)
顺便说一句,你很可能重构了code是一个小更易于理解,是这样的:
As an aside, you could probably refactor that code to be a little more understandable, something like:
string CalcGrade (int s1, int s2, int s3, double median) {
// Grade lookup arrays. If grade is >= limit[n], string is grades[n].
// Anything below D- is a fail.
static const int Limits[] = {400, 380, 360, 340,320, 300, 280,260, 240, 220,200,180 };
static const int Grades[] = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","D-"};
double finalGrade = s1 + s2 + s3 + median;
// Check each element of the array and, if the final grade is greater
// than or equal to, return the grade string.
for (int i = 0; i < sizeof(Limits) / sizeof(*Limits); i++)
if (finalGrade >= Limits[i])
return Grades[i];
// Otherwise, failed.
return "Fail";
}
这消除了魔术时S $ P $垫遍布code到这是显而易见的,他们是如何工作的地区(假设你对准他们很好)。
This removes the magic numbers spread all over the code to an area where it's immediately obvious how they work (assuming you align them nicely).
这也与原来的解决方案中删除一个问题,因为什么,我们与别人,达到380.5分做 - 这不是的真正的公平失败:-)或者那些的BOD分配一个档次,
来的400以上(因为似乎没有要回的方式A +
)。
It also removes a problem with your original solution as to what we do with someone that achieved a score of 380.5 - it's not really fair to fail those bods :-) Or to assign a grade to ""
to those above 400 (since there doesn't appear to be a way to return "A+"
).
这篇关于将这些应考虑的幻数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!