Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Basics of writing clean code

Writing clean, understandable, and maintainable code is a skill that is crucial for every developer to master.

In this, we will look at the most important principles to improve code quality and I will give you code examples for each of them.

  • Sé el primero en comentar

  • Sé el primero en recomendar esto

Basics of writing clean code

  1. 1. Presented By: Mansi Babbar Basics of Writing Clean Code
  2. 2. Lack of etiquette and manners is a huge turn off. KnolX Etiquettes Punctuality Respect Knolx session timings, you are requested not to join sessions after a 5 minutes threshold post the session start time. Feedback Make sure to submit a constructive feedback for all sessions as it is very helpful for the presenter. Mute Please keep your window on mute Avoid Disturbance Avoid leaving your window unmuted after asking a question
  3. 3. Agenda 01 Introduction & Overview 02 Clean Naming Convention 04 Clean Classes 05 Clean Error Handling 06 Clean Unit Tests 05 Clean Comments 07 7 Clean Functions 03 05 Clean Formatting Skills 08
  4. 4. What is clean code?
  5. 5. Clean code
  6. 6. Clean code ● easy to understand the execution flow of the entire application ● easy to understand how the different objects collaborate with each other ● easy to understand the role and responsibility of each class ● easy to understand what each method does ● easy to understand what is the purpose of each expression and variable ● easy to extend and refactor
  7. 7. Clean code ● Classes and methods are small and only have single responsibility ● Classes and methods are predictable and work as expected ● The code is easily testable and has unit tests (or it is easy to write the tests) ● Tests are easy to understand and easy to change
  8. 8. What is the cost of bad code?
  9. 9. Cost of bad code
  10. 10. Cost of bad code
  11. 11. Cost of bad code
  12. 12. For whom do we write code ?
  13. 13. ● For yourself ? ● For computer ? ● For other programmers ?
  14. 14. How to write clean code ?
  15. 15. Universal Principles to follow ● Follow SOLID principles ● Follow a Design Pattern if it fits the problem space that you are trying to solve. ● Follow Law of Demeter ● Follow TDD (Test Driven Development)
  16. 16. What to clean ?
  17. 17. What should we clean? ● Naming convention ● Functions ● Classes ● Error Handling ● Tests ● Comments ● Formatting Skills
  18. 18. Naming Convention
  19. 19. Cleaning Naming Convention ● Name should be simple ● Names should reveal intent ● Use pronounceable name ● Use one word per concept ● Use verbs for function names and nouns for classes and objects
  20. 20. Functions
  21. 21. Cleaning Functions ● Functions should be short as possible ● It should follow SRP (Single responsibility principle) ● No side effects ● No nested control structure ● Prefer less number of arguments ● Must only do what the name suggests and nothing else ● Should either do something or answer something, but not both
  22. 22. Classes
  23. 23. Cleaning Classes ● Classes should contain variables and functions that do one thing (SRP) ● The name of a class should describe what responsibilities it fulfills ● Do one thing (SRP) : It is forbidden to create classes that have many responsibilities ● Variables should be private so that we can change their type or implementation when required ● Classes should be small ● Classes should provide abstraction
  24. 24. Error Handling
  25. 25. Cleaning Error Handling ● Code should be both clean and robust, it should handle errors with grace and style. ● Catch everything. Better to be safe than sorry. ● Be informative. Return Exceptions instead of Error Codes. Each exception thrown should provide enough context to detect the error root cause and location.
  26. 26. Unit Tests
  27. 27. Cleaning Unit Tests Clean tests should follow F.I.R.S.T principles: ● Fast: Tests should be fast. They should run quickly. ● Independent: Tests should not depend on each other. One test should not set up the conditions for the next test. ● Repeatable: Tests should be repeatable in any environment. They should run in the production environment, the QA environment, and even on your laptop while riding home on the train without a network. ● Self-Validating: Tests should have a boolean output. Either they pass or fail. ● Timely: Tests need to be written in a timely fashion.
  28. 28. Comments
  29. 29. Cleaning Comments ● The only truly good comment is the comment you found a way not to write. Don’t comment bad code, rewrite it ● Don’t comment bad code, rewrite it ● Explain your intention in comments ● Warn of consequences in comments ● Never leave code commented
  30. 30. Formatting Skills
  31. 31. Cleaning Formatting Skills ● When people look into your code, you want them to be impressed with the neatness, consistency, orderliness and attention to detail that they perceive ● You should strive to keep your lines short. You should never have to scroll to the right. ● A function that is called should be below a function that does the calling. ● Each line represents an expression or a clause, and each group of lines represents a complete thought.
  32. 32. Conclusion
  33. 33. Conclusion We can simple remember these points while writing code... ● KIS : Keep It Simple ● DRY : Don’t Repeat Yourself ● YAGNI : You Aren’t Gonna Need It ● Composition over Inheritance ● Favor Readability ● Practice Consistency
  34. 34. Thank You !!!!