本文翻譯自:What's the purpose of using braces (i.e. {}) for a single-line if or loop?
I'm reading some lecture notes of my C++ lecturer and he wrote the following: 我正在閱讀我的C ++講師的一些講義,他寫了以下內容:
- Use Indentation // OK 使用縮進//確定
- Never rely on operator precedence - Always use parentheses // OK 永遠不要依賴運算符優先級 - 始終使用括號//確定
- Always use a { } block - even for a single line // not OK , why ??? 總是使用{}塊 - 即使是單行// 不行 ,爲什麼???
- Const object on left side of comparison // OK 比較左側的Const對象// OK
- Use unsigned for variables that are >= 0 // nice trick 對於> = 0 //好玩法的變量使用無符號
- Set Pointer to NULL after deletion - Double delete protection // not bad 刪除後將指針設置爲NULL - 雙刪除保護//不錯
The 3rd technique is not clear to me: what would I gain by placing one line in a { ... }
? 第三種技術對我來說並不清楚:在{ ... }
放置一行會獲得什麼?
For example, take this weird code: 例如,拿這個奇怪的代碼:
int j = 0;
for (int i = 0 ; i < 100 ; ++i)
{
if (i % 2 == 0)
{
j++;
}
}
and replace it with: 並替換爲:
int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;
What's the benefit of using the 1st version? 使用第一個版本有什麼好處?
#1樓
參考:https://stackoom.com/question/pA02/對於單行if或循環使用大括號-即-的目的是什麼
#2樓
Let's attempt to also modify i
when we increment j
: 當我們遞增j
時,讓我們嘗試修改i
:
int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;
i++;
Oh no! 不好了! Coming from Python, this looks ok, but in fact it isn't, as it's equivalent to: 來自Python,這看起來不錯,但實際上並非如此,因爲它相當於:
int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;
i++;
Of course, this is a silly mistake, but one that even an experienced programmer could make. 當然,這是一個愚蠢的錯誤,但即使是經驗豐富的程序員也可以做到。
Another very good reason is pointed out in ta.speot.is's answer . 在ta.speot.is的回答中指出了另一個很好的理由 。
A third one I can think of is nested if
's: 我能想到的第三個是嵌套if
:
if (cond1)
if (cond2)
doSomething();
Now, assume you now want to doSomethingElse()
when cond1
is not met (new feature). 現在,假設您現在想要在cond1
時cond1
doSomethingElse()
(新功能)。 So: 所以:
if (cond1)
if (cond2)
doSomething();
else
doSomethingElse();
which is obviously wrong, since the else
associates with the inner if
. 這顯然是錯誤的,因爲else
與內部同事if
。
Edit: Since this is getting some attention, I'll clarify my view. 編輯:由於這引起了一些關注,我將澄清我的觀點。 The question I was answering is: 我回答的問題是:
What's the benefit of using the 1st version? 使用第一個版本有什麼好處?
Which I have described. 我已經描述過了。 There are some benefits. 有一些好處。 But, IMO, "always" rules don't always apply. 但是,IMO,“永遠”規則並不總是適用。 So I don't wholly support 所以我不完全支持
Always use a { } block - even for a single line // not OK, why ??? 總是使用{}塊 - 即使是單行//不行,爲什麼???
I'm not saying always use a {}
block. 我並不是說總是使用{}
塊。 If it's a simple enough condition & behavior, don't. 如果這是一個簡單的條件和行爲,不要。 If you suspect someone might come in later & change your code to add functionality, do. 如果您懷疑有人可能會稍後進入並更改您的代碼以添加功能,請執行此操作。
#3樓
it is more intuitive and easily understandable. 它更直觀,更容易理解。 It makes the intent clear. 它使意圖清晰。
And it ensures that the code doesn't break when a new user might unknowingly miss the {
, }
while adding a new code statement. 並且它確保當新用戶在添加新代碼語句時可能在不知不覺中錯過{
, }
時代碼不會中斷。
#4樓
It's very easy to accidentally change control-flow with comments if you do not use {
and }
. 如果不使用{
和}
則很容易通過註釋意外更改控制流。 For example: 例如:
if (condition)
do_something();
else
do_something_else();
must_always_do_this();
If you comment out do_something_else()
with a single line comment, you'll end up with this: 如果你用單行評論註釋掉do_something_else()
,你最終會得到:
if (condition)
do_something();
else
//do_something_else();
must_always_do_this();
It compiles, but must_always_do_this()
isn't always called. 它編譯,但must_always_do_this()
並不總是被調用。
We had this issue in our code base, where someone had gone in to disable some functionality very quickly before release. 我們在代碼庫中遇到了這個問題,有人在發佈之前很快就禁用了某些功能。 Fortunately we caught it in code review. 幸運的是,我們在代碼審查中發現了它。
#5樓
It makes your code more readable by clearly defining the scope of your loops and conditional blocks. 它通過清楚地定義循環和條件塊的範圍,使您的代碼更具可讀性。 It also saves you from accidental mistakes. 它還可以避免意外錯誤。
#6樓
If you are a compiler, it doesn't make any difference. 如果您是編譯器,它沒有任何區別。 Both are the same. 兩者都是一樣的。
But for programmers, the first one is more clear, easy to read and less error-prone. 但對於程序員來說,第一個更清晰,易於閱讀且不易出錯。