Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Caring About Code Quality (Clean Code, GRASP, Effective Java, Design Pattern)

151 vues

Publié le

Code quality is not an exact science and is rather subjective, which brings the need of well defined rules and principles to follow.
Clean code is all about readability, furthermore, principles like GRASP or techniques like fluent APIs makes the code even cleaner and maintainable.
Design Patterns on the other hand are typical solutions to common problems in software design.
You will walk away with a taste of quality principles and metrics for building quality software.
Also previews of some useful books like Martin Fowler's Clean Code and Kent Beck's Implementation Patterns are presented.

Publié dans : Logiciels
  • Soyez le premier à commenter

Caring About Code Quality (Clean Code, GRASP, Effective Java, Design Pattern)

  1. 1. 1Caring About Code Quality Caring About Code Quality EL MAHDI BENZEKRI | elmahdi.benzekri@gmail.com 2019
  2. 2. 2Caring About Code Quality Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin The Book
  3. 3. 3Caring About Code Quality Does it work? What is Clean Code? Does it scale? Is it esthetic? Is it maintainable? Is it testable? Is it short? Is it clever? Does it have explanatory comments? Is it easy to understand? Is it well-structured? Is it object-oriented?
  4. 4. 4Caring About Code Quality • Focused • Single-minded attitude • Undistracted and unpolluted • Readable, simple and direct • Compact and literate • Contains only what is necessary • Makes it easy for other developers to enhance it • Tests should be also clean • Looks like it’s author cares • FoContains no duplicates • undations are established on tiny abstractions What is Clean Code after all?
  5. 5. 5Caring About Code Quality • Subjective • If you can’t measure, you can’t improve • Identify what matters • Defend and justify decisions. The need for measurement
  6. 6. 6Caring About Code Quality Cyclomatic complexity public void foo() { if (c1) { f1(); } else { f2(); } if (c2) { f3(); } else { f4(); } } • Upper bound for the number of test cases that are necessary to achieve a complete branch coverage • Lower bound for the number of paths through the control flow graph • Desirable value: below 10 Cyclomatic complexity: 3
  7. 7. 7Caring About Code Quality Always leave the campground cleaner than you found it. The Boy Scout Rule
  8. 8. 8Caring About Code Quality • Came from city crime researchers • A broken window will trigger a building into a smashed and abandoned derelict • So does the software Broken Window Theory
  9. 9. 9Caring About Code Quality Composed method example public void add(Object element) { if (!readOnly) { int newSize = size + 1; if (newSize > elements.length) { Object[] newElements = new Object[elements.length + 10]; for (int index = 0; index < size; index++) { newElements[index] = elements[index]; elements = newElements; } } elements[size++] = element; } }
  10. 10. 10Caring About Code Quality Implementation patterns
  11. 11. 11Caring About Code Quality Composed method example public void add(Object element) { if (!isReadOnly()) { if (atCapacity()) { grow(); } addElement(element); } }
  12. 12. 12Caring About Code Quality • Instant feedback • Allows you to make changes to code quickly • Help you understand the design of the code you are working on • Writing testable code helps to achieve better code quality • Unit tests are a form of sample code • Test-first forces you to plan before you code Advantages of Unit tests
  13. 13. 13Caring About Code Quality • Whether the unit you're testing should be isolated from its collaborators • Mockist approach – Isolate from dependencies – More flexible in what you can test • Classic approach – No attempt to isolate unless communicating with the collaborator is awkward – Less brittle tests Unit tests collaborator isolation
  14. 14. 14Caring About Code Quality • Change Risk Analysis and Prediction • Help you identify code that might be particularly difficult to understand, test, or maintain • 𝐶𝑅𝐴𝑃 𝑚 = cycl_comp(m)2 ∗ 1 − 𝑐𝑜𝑣𝑒𝑟𝑎𝑔𝑒 3 + cycl_comp(m) • A method with a CRAP score over 30 is considered unacceptable C.R.A.P. metric Cyclomatic Complexity Coverage required 0 – 5 0% 10 42% 15 57% 20 71% 25 80% 30 100% 31+ Not possible
  15. 15. 15Caring About Code Quality • Don’t build complex machines • Don’t build frameworks • Make the software so simple that there are obviously no deficiencies • Keep it simple, stupid (KISS) Rube Goldberg Machines
  16. 16. 16Caring About Code Quality • Avoid Cargo cult programming • Question authority • Accept feedback and give feedback Angry monkeys experiment
  17. 17. 17Caring About Code Quality DESIGN PATTERNS IN PRACTICE 2019
  18. 18. 18Caring About Code Quality Transfer object pattern
  19. 19. 19Caring About Code Quality Front controller
  20. 20. 20Caring About Code Quality Chain of responsibility
  21. 21. 21Caring About Code Quality Proxy pattern
  22. 22. 22Caring About Code Quality • Thread safety • Valid state • Encapsulation • Simpler to test • More readable and maintainable code Immutable Object
  23. 23. 23Caring About Code Quality Null Object
  24. 24. 24Caring About Code Quality Flyweight pattern
  25. 25. 25Caring About Code Quality Template method
  26. 26. 26Caring About Code Quality • State Pattern’s intent is to manage states of the object along with object’s behavior which changes with its state. • State typically involves a getStatus method and a reference back to the context • state, behavior is autodeterminated, strategy, behavior is determinated by caller. • Strategy Pattern is to have a family of interchangeable algorithms • Relation between individual states/individual strategies State vs Strategy
  27. 27. 27Caring About Code Quality Observer Pattern
  28. 28. 28Caring About Code Quality Facade Pattern
  29. 29. 29Caring About Code Quality Decorator Pattern
  30. 30. 30Caring About Code Quality THINKING AND CODING EFFICIENTLY
  31. 31. 31Caring About Code Quality Effective Java • Level of access ? Minimum • Classpath ? • Composition vs inheritance • Avoid side effects • Minimize Mutability • Check methods/constructosr parameters for validity • Always override hashCode when you override equals • … • https://github.com/HugoMatilla/Effective-JAVA-Summary
  32. 32. 32Caring About Code Quality • “This” keyword • Chained • Popular java Fluent API’s: – Java Object Oriented Querying (jOOQ) API – Java 8’s Date Time API – Java 9’s Money and Currency API Fluent API
  33. 33. 33Caring About Code Quality • Handle UI interractions GRASP - The Controller
  34. 34. 34Caring About Code Quality • Information expert • Encapsulating information GRASP - The Expert
  35. 35. 35Caring About Code Quality • Creation responsibility • Have enough information to instantiate an object GRASP - The Creator
  36. 36. 36Caring About Code Quality • it is a measure of how strongly related each piece of functionality expressed by the source code of a software module is. GRASP - High cohesion
  37. 37. 37Caring About Code Quality • The smallest number of references between objects as possible • Reason of change GRASP - Low coupling