NSWI170 Computer Systems

Guidelines to write a better C/C++ code

all curses | next curse

Rule #0: Consistent naming

Babel Curse

The Babel Curse: Chaos arises when every programmer speaks a different language
The Clarity Charm Counter-Spell: Use clear, English, consistently‑styled names so your code speaks with one voice

Naming consistency is a fundamental code quality principle in which all identifiers - variables, constants, functions, structures, and other symbols - are written in a unified, predictable style that reflects clear intent. In the context of this course, this means using English terminology, adhering to the framework’s naming conventions, and choosing descriptive, semantically meaningful names throughout the program. Clear and consistent naming forms the basis of readable, maintainable code: before addressing deeper structural or logical design issues, a program must “speak” with one coherent voice so that its purpose is immediately understandable. Without this clarity, every subsequent code quality practice becomes harder to apply, harder to reason about, and far less effective.

Motivation

Explanation

Self-check

Are all my variable and function names in English?
Do all identifiers follow the same naming style consistent with my legacy code and the funshield library.
Would another student immediately understand what all names mean?

Example

💩 Bad code 👍 Good code
// Inconsistent spacing
// Do NOT use personal abbreviations that are not widely understood
// Prefer descriptive names instead of saving a few letters
// Be consistent with naming used by the framework, i.e., constants with capitals
constexpr  int ButtonAr[3] = {BUTTON1_PIN ,BUTTON2_PIN ,BUTTON3_PIN };

// Do NOT mix multiple languages in identifiers;
// also avoid names that may have unintended meanings in English
constexpr  int PocetButt = sizeof(ButtonAr) / sizeof(ButtonAr[0]);
// Fixed spacing
// ButtonAr --> ButtonArray / Buttons: more descriptive naming
// (Plural usually implies a collection, such as an array)
// name constans with capitals to be constistent with the funshield library
constexpr int BUTTONS[3] = { BUTTON1_PIN, BUTTON2_PIN, BUTTON3_PIN };

// PocetButt --> ButtonsCount/NumberOfButtons/ButtonsSize
// Note: for consistency, you should name all the upcomming array sizes with postfix _COUNT
constexpr int BUTTONS_COUNT = sizeof(BUTTONS) / sizeof(BUTTONS[0]);