gpt4 book ai didi

编码风格 : Best way to program function calls dependent on some flag

转载 作者:太空宇宙 更新时间:2023-11-04 04:10:28 25 4
gpt4 key购买 nike

这是一道简单的C题。我这样设计我的代码:

main()
{
...
do_my_func1();
...
}

do_my_func1() 在哪里做的:

void do_my_func1()
{
if (flag)
{
..do the processing for this function
}
}

我的老板说实际上应该这样做:

main()
{
...
if (flag)
{
do_my_func1();
}
...
}

void do_my_func1()
{
..do the processing for this function
}

那么进行这样的函数调用的最佳做法是什么 - 我认为我的方法是最好的方法,尤其是当您调用许多函数时依赖于许多标志?

最佳答案

像您的 do_my_func1() 这样的函数应该始终坚持做简单的事情并且调用它的检查应该事先完成,所以 if (flag) 应该始终在父函数中。

另外一个好的做法是在同一文件中调用的函数前添加static,它可以减轻编译后的可执行文件

关于编码风格 : Best way to program function calls dependent on some flag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58165505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com