English 中文(简体)
是否有根据论点确定变量的公约?
原标题:Is there a convention for setting variables based on arguments?

Say I have a function with a flag 或 something:

void foo (Param p1, Param p2, bool setVariable)
{
    //if setVariable is true, set some bool var to true, else false
}

是否有强烈的优待?

if (setVariable)
    _someClassVariable = true;
else
    _someClassVariable = false;

_someClassVariable = setVariable;

Obviously the second is less typing, but the first strikes me as m或e readable. Which one would be preferred?

最佳回答

我一般倾向于第二种。 对我来说,首先强烈警告说,无论谁写这部法典,谁都完全有能力。

尽管如此,我也倾向于建议不要将ool作为准参数。 它很少立即表明:<代码>foo(true); vsfoo(false); really means。 通常最好使用一个清单,以便你获得<代码>foo(do_this); vsfoo(do_ that);

问题回答

I would go for the second, it is just as readable. Readability is more on the names of the variables than on the choice of both options. If the variables and parameters have good names the assignment will be natural. This is similar to returning a boolean from a function you would not do:

bool conditionHolds() {
    if (condition)
       return true;
    else
       return false;
}

(如果你正在考虑这样做,请重新思考)

我倾向于第二种。 如果打字不好,那么你就有足够的论据,也考虑你们的同事的意见。 如果我看到像我这样一些东西,我个人会感到 la笑。

if (condition)
   return true;
else
   return false;

in production code. (provided the variables are bool, and you re not using this to implement some casting mechanism).

后者更为可取,如果仅仅看看不使用,则在守则和汇编者编造的代码(尽管PROBABLY选择不适用)。

I would also avoid the leading underscore notation, some of those name are reserved fro the standard library and compiler.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签