简体   繁体   English

需要知道良好的做法,我应该遵循以下代码

[英]Need to know good practice should i follow on below code

i have code as : 我的代码为:

 int good_practice(void)
    {
      if (somethings == TRUE)
       return true;  //i am asked to remove it coz it will reach anyhow at end and do same but i feels it is good practice and why should i wait for end?
      else
       return false
      }
     }
     return true; 
    }

i am asked remove first return since it will ultimately reach end of function and will return true.Is this good ? 我被要求删除第一个返回值,因为它最终将到达函数的结尾并返回true。这好吗?

what i feel why do i need to wait for cpu to go at end of function and return though i can do early.if i have decide to return with some condition why should i return from end why not there itself why to wait for end ? 我觉得为什么我需要等待cpu在函数结束时返回并尽早返回。如果我决定以某种条件返回,为什么我应该从结尾返回,为什么本身不为什么要等待结尾?

And also if i am wait for end the i increase time complexity(i know it won't make any difference) but no of instruction is also increased. 而且如果我在等待结束,我会增加时间复杂度(我知道这不会有任何区别),但是指令的增加也不会增加。

please help me to get out of this confusion? 请帮助我摆脱这种困惑?

This is called an “early return” and I don't think there is a clear consensus. 这被称为“早期回报”,我认为没有明确的共识。

Pros: 优点:

  • You can't execute some other code by mistake if you return early. 如果您提早返回,则不能错误地执行其他一些代码。
  • If you are reading this particular execution branch, the early return makes the flow more explicit. 如果您正在阅读此特定的执行分支,则提早返回将使流程更明确。
  • It's also possible to decrease the indent level by returning early. 还可以通过提早返回来降低缩进级别。

Cons: 缺点:

  • When you're looking at the whole function, it's easier to miss an early return. 当您查看整个功能时,更容易错过早日回报。

There are some similar previous questions about early returns, see here for example . 以前有一些关于提早回报的类似问题,例如请参见此处

You are asked to remove the first return true because you can simply check like this, and not have an additional redundant return true : 要求您删除第一个return true因为您可以像这样简单地检查,而没有额外的冗余return true

int good_practice(void) {
    if(!somethings) { 
        return false;
    }
    return true;
}

Anyway, the code in question is pretty much redundant . 无论如何,所讨论的代码几乎是多余的

You can replace it with an even better version, like this: 您可以用更好的版本替换它,如下所示:

int good_practice(void)
{
    return somethings;
}

About the early return: 关于提前归还:

Its generally better to have one return at the end, because the performance gained, when returning earlier is nowhere near the gained readability by having one return only . 通常最好在末尾有一个返回,因为在更早返回时获得的性能与通过返回一个返回的可读性相差甚远。

(Thats just my own experience though). (尽管那只是我自己的经验)。

暂无
暂无

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

相关问题 这个链表代码是一个好习惯吗? - is this linkedlist code a good practice? 我在下面编写的线性搜索c代码的时间复杂度是多少? 我的代码是好是坏? - What is the time complexity of the Linear Search c code that i have written below? is my code good or bad? 如何修复代码中的分段错误以及将来修复此问题应遵循的过程是什么? - How can i fix segmentation fault in my code and what is the process I should follow to fix this in the future? 我需要知道此源代码如何生成输出 - I need to know how the output was generated by this source code 我写了这段代码,需要知道更新变量值是否存在同步问题? - I wrote this code and need to know if there is synchronization problem in updating the variable value? 我应该遵循《 C编程语言第二版》这本书吗,但其中的某些代码不起作用 - should i follow this book “c programming language 2nd edition” but some code in it doesn't work 我的课程对我的作业来说足够好,但我知道它不好 - My program is good enough for my assignment but I know its not good 下面我的 C 选择排序代码有什么问题? 我应该在我的无效选择排序中做什么 - What is wrong with my C selection sort code below? what should I do in my void selection sort 我应该如何在下面的代码中使用 C 中的结构指针数组? - How should I use an array of pointers of structs in C in my code below? 为什么我的程序的output 0.000000 应该是我在提示时输入的值。 下面是代码 - why is the output of my program 0.000000 when it should be the value i entered when prompted. below is the code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM