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

Clean Code Principle for Software Engineer

Clean Code Principle for Software Engineer

Clean Code Principle for Software Engineer

Didik Tri Susanto

October 27, 2022
Tweet

More Decks by Didik Tri Susanto

Other Decks in Programming

Transcript

  1. Contents • Why Clean Code? • Benefit & Goal •

    Principle • Approach • Dealing with Legacy Code
  2. What is Clean Code “Clean code always looks like it

    was written by someone who cares” ~ Michael Feathers
  3. • Increase Maintainability == Reduce maintenance cost • Reduce onboarding

    time for new developer • Reduce tech debt • Reduce time to add or modify existing features • Happy developer
  4. “Always code as if the person who ends up maintaining

    your code is a violent psychopath who knows where you live.”
  5. Follows Coding Standard • PHP: PHP Standard Recommendation (PSR) •

    Javascript: Airbnb coding standard • Go: Effective Go
  6. Code Review or Pair Programming • Sharing context • Finding

    insight and alternative solution • Knowledge transfer • Managing your ego
  7. Meaningful Names for Variables, Methods, Classes, Arguments • Should be

    sufficient enough to understand the purpose or context • One should not have to read the whole code to figure out what a function does or what a class represents or to understand why a variable exists • No need comments
  8. Other naming best practices • Name functions as verbs. Ex:

    save, create, deductBalance • Name classes as nouns. Ex: User, UserController • Use pronounceable names. Ex: modificationTimestamp, isActive, createAndSave
  9. Good Functions • Should be small as possible • Should

    do just one (or multiple by context) thing • Should have fewer arguments • No side effects
  10. Do we need comments? • No. Your code should be

    self explain • Only put comment if necessary ◦ Some trade off ◦ Deprecation warning ◦ What point or process needs to be aware
  11. DRY - Don’t Repeat Yourself “Every piece of knowledge must

    have a single, unambiguous, authoritative representation within a system” • Actually duplication is fine* • Extract part of code that can be reused to a class or function
  12. KISS - Keep It Simple Stupid • Code less •

    Easy to understand, debug, and modify • Complex code != smart
  13. YAGNI - You Aren't Gonna Need It • “Maybe we

    need use that in the future” • Only code what you need to build • Avoid adding unnecessary dependencies or complexity • Leads to over engineering
  14. Design Pattern & System Design • Understanding design patterns can

    help us to make decision with our code ◦ Creational pattern ◦ Structural pattern ◦ Behavioral pattern • Understanding system design to prevent tech debt or over engineering ◦ Monolith Design ◦ Domain Driven Design ◦ Microservices ◦ Architectural Pattern
  15. • Do not judge it • Understand how it runs

    • Understand the structure & pattern • Understand the business context • Raise your empathy • Leave better than you found it
  16. Do we need to refactor the bad codes? It depends!

    What is your organization’s prioritization?
  17. Working on new or modifying features • Follow current structure

    with better approach • Plan and communicate if you are proposing approach that might totally different with current structure • Add unit tests or automated tests if possible • Do pair programming or code review • Done is better than perfect
  18. Clean Code is a Journey • Remember, your first code

    must be awful. Keep practice • Learn design patterns and system designs • Get more insight, find mentor or developer fellows, contribute to open source
  19. QnA