Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clean Code Highlights for Real Projects

Avatar for Florin Trifu Florin Trifu
February 07, 2019

Clean Code Highlights for Real Projects

Clean code highlights for variables, functions, and classes naming and functions definition that can be used in real in real life.

Avatar for Florin Trifu

Florin Trifu

February 07, 2019

Other Decks in Programming

Transcript

  1. Content • What is Clean Code? • The Productivity Trap

    • Code Rot • Names, the invisible enemy • Functions
  2. Disclaimer The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL

    NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119. (https://www.ietf.org/rfc/rfc2119.txt)
  3. Mathematical intervals • (a, b) – open • [a, b]

    – closed • (a, b] – open left • [a, b) - open right
  4. What is clean code? Bjarne Stroustrup: “I like my code

    to be elegant and efficient. Clean code does one thing.”
  5. What is Clean Code? Michael Feathers (author of Working Effectively

    with Legacy Code) “Clean Code always looks like it was written by someone who cares.”
  6. Rigidity A rigid system is a system in which a

    single change or enhancement forces you to make dozens of other changes throughout the body of the system
  7. Inseparability An inseparable system is a system composed of modules

    that can’t be separated and reused for other purposes
  8. Opacity Opaque code is code that do not communicate the

    author’s intent, is hard to read, hard to understand, hard to change
  9. Ottinger’s Rules for Variables and Class Naming Every time you

    choose a name, it should reveal the intent of the thing you’re trying to name; if a comment is needed, then the name does not sufficiently reveal the intent.
  10. Reveal your intent Names that describe implementation details do not

    reveal intent; they talk more about the code as it is and why it was written that way , than they talk about the problem being solved.
  11. Reveal your intent Whenever you need to read the code

    in order to understand the name, the name has pretty much failed to communicate. Names are not for your convenience, but rather the primary tool for communicating your intent.
  12. Naming rules • Classes and variables are nouns, methods are

    verbs • Avoid noise words like Manager, Processor, Data or Info; they are just synonyms for “I don’t know how to call this” • Boolean should be written as predicates • If a method returns a Boolean, should be written as predicate
  13. The Scope Length Rule • For variables: THE LONGER THE

    SCOPE OF THE VARIABLE, THE LONGER THE NAME • For classes and functions: THE LONGER THE SCOPE, THE SHORTER THE CLASS OR FUNCTION NAME SHOULD BE. THE SHORTER THE SCOPE, THE LONGER THE CLASS OR FUNCTION NAME SHOULD BE.
  14. The first rule of functions Blaise Pascal: “I’m sorry this

    letter was so long, but I didn’t have time to make it smaller”
  15. The first rule of functions How long should a function

    be? 30 lines? 20 lines? Surprise, surprise… 5 Lines!
  16. Extract till you drop! Classes tend to hide inside big

    functions. To make sure a function does one thing, refactor big functions using Extract Function method, until you cannot extract anymore.
  17. Arguments Constructors should follow the same rule. If more than

    two, maximum three arguments are needed, it might mean that the arguments are cohesive enough to become an object.
  18. Switches and cases Switch statements are usually a missed opportunity

    for polymorphism, and they also tend not to be OOP.
  19. Q&A