Graduate Program KB

Clean Code

Chapter 2

  • Variables, functions and classes are used everywhere, they must have meaningful names

  • Meaningful names should be descriptive and intentive

    • Why does it exist
    • What it does
    • How's it used
    • Variable names should not require comments
    • Ties into the concept of clean code, when you read code you should already understand what it does
  • Distinct styles of naming

    • Styles such as upper case for constants, or using nouns/verbs for classes/functions

      const PI = 3.14
      function executesAllTasksInOrder(listOfTasks) { ... }
      class Animal { ... }
      
    • Variable names shouldn't be similar meaning wise or syntactically to other names

      • Don't use 'fetch' and 'retrieve', just pick one and stay consistent
    • Pronouncable names helps us to understand the code, remember it more effectively and explain it to others

      • Unreasonably long names should be avoided if possible
      • Select a descriptive variable as simple as possible
  • Names should be searchable, should be decent length and avoid as many keywords from the language

  • Everyone's experienced looking for a specific part of code in a large file using the search functions but a lot of search results appear

    • Names containing 'array', 'list'
    • Names containing digits
    • Single letter names such as i
  • Our audience are still programmers, so we can still use technical terms in names which is a more familiar language

    • Even though the variable name i is not considered a good name, if it's used in a loop then programmers will understand it because its a traditional convention
  • Learning how to choose good names comes from experience

    • Take feedback when other people review your code
    • When reading other people's code, consider what areas are vague and could be more descriptive