问题描述
我想写好代码。我刚刚开始阅读Clean Code,我正在努力遵循它的建议。
我的问题的一个例子是:
#include <cs50.h>
#include <stdio.h>
int llamasBorn(int n);
int llamasDeceased(int n);
int main(void)
{
// Prompt for start size
int startSize;
do
{
startSize = get_int("Start size: "); //-----> WATCH HERE!
}
while (startSize < 9);
// Prompt for end size
int endSize;
do
{
endSize = get_int("End size: ");
}
while (endSize < startSize);
// Calculate number of years until we reach threshold
int yearsUntilThreshold = 0;
int currentSize = startSize; //-----> AND HERE!
while (currentSize < endSize)
{
currentSize = currentSize + llamasBorn(currentSize) - llamasDeceased(currentSize);
yearsUntilThreshold++;
}
// Print number of years
printf("Years: %i\n",yearsUntilThreshold);
}
// Define method for the number of new llamas per year
int llamasBorn(int n)
{
int born = n / 3;
return born;
}
// Define method for the number of dead llamas per year
int llamasDeceased(int n)
{
int deceased = n / 4;
return deceased;
}
我想知道根据变量输入时的含义为其命名,然后将其存储在另一个变量中以增强下一段代码的可读性是否太过分了。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)