简体   繁体   English

尝试重写此代码,使其不违反“ Code Complete 2nd Edition中讨论的原理”

[英]trying to rewrite this so it doesnt violate "prinicples discussed in Code Complete 2nd edition

function profit(){
    int totalSales=0;
    for (int i=0; i<12;i++) // computer yearly sales
          totalSales+=montlysales[i];
   return get_profit_from_sales(totalsales);
}

So i've already determined that the 12 in the for loop should be a constant instead of just using an integer and that the montlysales should be passed as a parameter into the function so then a check can be run to see if the length of sales is equal to the integer value of months which is also twelve. 因此,我已经确定for循环中的12应该是一个常量而不是仅使用整数,并且montlysales应该作为参数传递到函数中,以便随后可以进行检查以查看销售时长等于月份的整数值,该整数也为十二。 I'm not sure if those are all the violations of the princples cause. 我不确定是否所有这些都是违反原则的原因。 I feel the last line 我感觉到最后一行

return get_profit_from_sales(totalsales) 

is wrong and its really bothering me cause I can't seem to figure out why it is in fact bothering me and I think I might have skipped something else. 是错的,它真的困扰我,因为我似乎无法弄清为什么它实际上困扰我,我想我可能已经跳过了其他事情。

can anyone help me verify? 有人可以帮我验证吗?

Summary - you should refactor out the call to another function and make this function so that it is pure and does only one thing, reducing complexity, and improving your ability to reason abstractly about your program and its correctness. 总结-您应该重构对另一个函数的调用,并使该函数成为纯函数,并且只做一件事,从而降低复杂性,并提高您对程序及其正确性进行抽象推理的能力。

Your spidey sense is tingling and you should trust it - you are correct, but what is wrong is subtle. 您的间谍意识刺痛,您应该信任它-您是正确的,但是错误是微妙的。

Routines are best when they do one thing, and one thing only. 例行程序只能做一件事,而只能做一件事,是最好的。 So purity of vision is important in the prime imperative, management of complexity -- it allows our brains to be able to juggle more things because they are simpler. 因此,视力的纯净度在首要任务,复杂性管理中很重要-它使我们的大脑能够处理更多的事情,因为它们更简单。 That is, you can just look at the function and know what it does, and you don't have to say, "it totals the sales, but it also calls another function at the end", which sort of clouds its "mission". 也就是说,您只需要看一下功能就可以知道它的作用,而不必说“它实现了销售总额,但最后它又调用了另一个功能”,这掩盖了它的“使命” 。

This is also part of functional programming and where I feel that languages have to adopt to try to implement the prime imperative spoken of in Code Complete. 这也是函数式编程的一部分,在我看来,语言必须采用这种语言来尝试实现Code Complete中所说的主要命令。 Functional programming has as one of its tenets, "no side effects", which is similar to "one mission" or "one purpose". 函数式编程的宗旨之一是“无副作用”,类似于“一个任务”或“一个目的”。 What I've done to your code can also be seen as making it more functional, just inputs and outputs and nothing in or out via any other path. 我对您的代码所做的工作也可以看作是使其更具功能性,只是输入和输出,而没有通过任何其他路径输入或输出的内容。

Note also that function get_profit() reads like pseudocode, making it somewhat self-documenting, and you don't have to delve into any of the functions to understand what the function does or how it does it (ideally). 还要注意,函数get_profit()的读取方式类似于伪代码,使其具有一定的自记录性,并且您不必深入研究任何函数即可了解该函数的作用或理想情况。

So, here is my idea of what I explained above (loosely coded, and not checked!). 因此,这就是我上面所解释的想法(松散编码,未经检查!)。

function get_total_sales(int montlysales[]){
    int totalSales=0;
    for (int i=0; i<12;i++) // computer yearly sales
        totalSales+=montlysales[i];
    return totalSales;
}

function calc_profit(int all_sales, int all_commissions, int all_losses)(){
    // your calculations here
    int profit = all_sales - all_commissions - all_losses;  // ... etc. 
    return profit;
}

function get_profit(){
    int montlysales[] = read_from_disk();
    int total_sales = get_total_sales(montlysales);
    int tot_commissions = get_tot_commissions();
    int tot_losses = get_tot_losses();
    int profit_made = calc_profit(total_sales, tot_commissions, tot_losses);
    return profit_made;
}

I read Code Complete about once a year, as coding is truly subtle at times, because it is so multidimensional. 我大约每年读一次《 Code Complete》,因为有时编码确实很微妙,因为它是多维的。 This has been very helpful to me. 这对我很有帮助。 Regards - Stephen 问候-斯蒂芬

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM