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 disambiguationException(例外)
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.
使用一個已知的容易混淆的字母和數(shù)字的列表檢查名稱。
Flag a declaration of a variable, function, or enumerator that hides a class or enumeration declared in the same scope.
標記同一作用域中可能隱藏類或枚舉類型聲明的變量、函數(shù)、枚舉類型的聲明。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es8-avoid-similar-looking-names
覺得本文有幫助?請分享給更多人。
關注微信公眾號【面向對象思考】輕松學習每一天!
面向對象開發(fā),面向對象思考!
