C++核心準則ES.8:避免看起來差不多的名稱

ES.8: Avoid similar-looking names

ES.8:避免看起來差不多的名稱

 

Reason(原因)

Code clarity and readability. Too-similar names slow down combiiaoprehension and increase the likelihood of error.

代碼整潔性和可讀性。過於相似的名稱會減緩理解進程並增加出錯的可能性。

 

Example, bad(反面示例)

 

if (readable(i1 + l1 + ol + o1 + o0 + ol + o1 + I0 + l0)) surprise();

Example, bad(反面示例)

Do not declare a non-type with the same name as a type in the same scope. This removes the need to disambiguate with a keyword such as struct or enum. It also removes a source of errors, as struct X can implicitly declare X if lookup fails.

不要用一個名稱定義類型之後,在同一個作用域中又使用這個名稱定義非類型。這種做法使消除名稱和像struct或enum那樣的關鍵詞之間的歧義不再必要。同時也減少了一個錯誤的源頭,例如如果名稱檢索失敗,struct X可以隱性聲明X類型。

struct foo { int n; };
struct foo foo();       // BAD, foo is a type already in scope
struct foo x = foo();   // requires disambiguation

Exception(例外)

Antique header files might declare non-types and types with the same name in the same scope.

特別早期的頭文件可能會使用同一個名稱聲明類型和非類型。

 

Enforcement(實施建議)

  • Check names against a list of known confusing letter and digit combinations.

  • 使用一個已知的容易混淆的字母和數字的列表檢查名稱。

  • Flag a declaration of a variable, function, or enumerator that hides a class or enumeration declared in the same scope.

  • 標記同一作用域中可能隱藏類或枚舉類型聲明的變量、函數、枚舉類型的聲明。

 

原文鏈接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es8-avoid-similar-looking-names

 


 

覺得本文有幫助?歡迎點贊並分享給更多的人。

閱讀更多更新文章,請關注微信公衆號【面向對象思考】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章