Top 5 best ways to improve your code pyxis v2 - en

912 views
759 views

Published on

This is my top 5 of best ways to improve your code base on many code reviews and some good practices

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
912
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Pourquoi?Quand?Comment?
  • Only one level of abstraction per methodSwitch case? Polymorphism?
  • The nameshouldasnwer all big questionsThe code shouldbereadablelike a storyOne word per concept (i.e. State -> AddressState != SystemState)
  • ** Use Intention-Revealing names (d -> elapsedInDays)** Avoid disinformation (AccountList -> Accounts)** Make meaningful distinction (moneyAmountvs money vs amount -> InvoiceTotal)** Use pronounceable names (genymdhms -> generationTimestamp** Avoid encoding (phoneString -> phone)** Avoid mental mapping (a -> account)** Class names -> noun not verbs** Method names -> verbs (SaveAccount)** Don't be cute. Use standard names (Destroy, kill, obliterate -> Delete)** Solution Domain Names (Interface) vs Problem Domain Names (Implémentation)
  • Reduce number of parameters - Clean code chapter 3 (p. 40-43)
  • Réduisez la visibilitéUtilisez les protectedUtilisez les privateUtilisez les InternalRéduisez le scopeDéplacez un field vers une méthodeScindez une classeDéplacer une variable près de son utilisationRéduisez la durée de vieInitialisation tardiveRéduisez les référencesLibérer tôt
  • Dead procedure / function / methodDead variableDead parameterDead return valueDead event declarationDead enumeration / constantDead typeDead class / structDead interface
  • Top 5 best ways to improve your code pyxis v2 - en

    1. 1. TOP 5 WAYS TO IMPROVE YOUR CODE Top 5 best ways to improve your code Write once read many.
    2. 2. we are Expertise Quality Respect /studio Agile Big Data custom CQRS Desktop Event Sourcing Microsoft .NET C# Scrum Software development Training © Pyxis Technologies inc. Web
    3. 3. INTRODUCTION Legacy code is code without tests  Michael Feather in Working effectively with legacy code Without maintenance, code degrade rapidly We must detect and get rid of code smell © Pyxis Technologies inc. Top 5 best ways to improve your code 3
    4. 4. MY 5 IMPROVEMENTS 1. Simplify conditionals 2. Remove comments 3. Clarify contracts 4. Control scope 5. Flush dead code © Pyxis Technologies inc. Top 5 best ways to improve your code 4
    5. 5. SIMPLIFY CONDITIONALS WHY? 1 2 3 4 5     Reduce complexity Improve readability Improve maintainability Improve Reusability © Pyxis Technologies inc. Top 5 best ways to improve your code 5
    6. 6. SIMPLIFY CONDITIONALS WHEN? 1 2 3 4 5      There is more than one conditions (and / or) There is too many code in the body Condition is based on type There are nested conditions There are many outcome from the same information (if / elseif / switch case) © Pyxis Technologies inc. Top 5 best ways to improve your code 6
    7. 7. SIMPLIFY CONDITIONALS HOW? 1 2 3 4 5  Refactor conditional statement        Decompose conditional Consolidate conditional expression Consolidate duplicate conditional fragments Introduce null object Flatten nested if Don't use negative Keep conditional statement lean  Avoid conditional statement  Replace conditional with polymorphism  Replace conditional logic with strategy  Replace conditional dispatcher with command © Pyxis Technologies inc. Top 5 best ways to improve your code 7
    8. 8. 2 3 4 5 SIMPLIFY CONDITIONALS 1 DECOMPOSE CONDITIONAL © Pyxis Technologies inc. Top 5 best ways to improve your code 8
    9. 9. 2 3 4 5 SIMPLIFY CONDITIONALS 1 CONSOLIDATE CONDITIONAL EXPRESSION © Pyxis Technologies inc. Top 5 best ways to improve your code 9
    10. 10. 2 3 4 5 SIMPLIFY CONDITIONALS 1 CONSOLIDATE DUPLICATE CONDITIONAL FRAGMENTS © Pyxis Technologies inc. Top 5 best ways to improve your code 10
    11. 11. 2 3 4 5 SIMPLIFY CONDITIONALS 1 INTRODUCE NULL OBJECT © Pyxis Technologies inc. Top 5 best ways to improve your code 11
    12. 12. 2 3 4 5 SIMPLIFY CONDITIONALS 1 FLATTEN NESTED IF © Pyxis Technologies inc. Top 5 best ways to improve your code 12
    13. 13. 2 3 4 5 SIMPLIFY CONDITIONALS 1 DON'T USE NEGATIVE © Pyxis Technologies inc. Top 5 best ways to improve your code 13
    14. 14. 2 3 4 5 SIMPLIFY CONDITIONALS 1 KEEP CONDITIONAL STATEMENT LEAN  As much as possible, try to use only one condition per if statement  Use a method to combine many conditions  Inverse if statement if most of the code is inside the true branch  Avoid double negation © Pyxis Technologies inc. Top 5 best ways to improve your code 14
    15. 15. 2 3 4 5 SIMPLIFY CONDITIONALS 1 REPLACE CONDITIONAL WITH POLYMORPHISM © Pyxis Technologies inc. Top 5 best ways to improve your code 15
    16. 16. 2 3 4 5 SIMPLIFY CONDITIONALS 1 REPLACE CONDITIONAL LOGIC WITH STRATEGY © Pyxis Technologies inc. Top 5 best ways to improve your code 16
    17. 17. 2 3 4 5 SIMPLIFY CONDITIONALS 1 REPLACE CONDITIONAL DISPATCHER WITH COMMAND © Pyxis Technologies inc. Top 5 best ways to improve your code 17
    18. 18. 1 REMOVE COMMENTS WHY? 2 3 4  Improve readability  Improve maintainability  Avoid obsolete comments 5 © Pyxis Technologies inc. Top 5 best ways to improve your code 18
    19. 19. REMOVE COMMENTS WHEN? 1 2 3 4 5  Each time a comment add anything else than useful information, intention, clarification, warning, todo or amplification  Comment for empty block of code  Empty catch  Describe code line by line  Example:     // // // // Getting connection string from configuration Opening connection Retrieving data Closing connection © Pyxis Technologies inc. Top 5 best ways to improve your code 19
    20. 20. REMOVE COMMENTS HOW? 1 2 3 4 5  Replace comment with good naming  Extract method  Use meaningful words  Write useful comments  Respect standard naming (MSDN: Guidelines for names)     Properties Enums Events Methods © Pyxis Technologies inc. Top 5 best ways to improve your code 20
    21. 21. EXTRACT METHOD 2 3 4 5 REMOVE COMMENTS 1 © Pyxis Technologies inc. Top 5 best ways to improve your code 21
    22. 22. USE MEANINGFUL NAMES 2 3 4 5 REMOVE COMMENTS 1  Use Intention-Revealing names  d  elapsedInDays  Avoid disinformation  AccountList  Accounts  Make meaningful distinction  moneyAmount vs money vs amount  InvoiceTotal  Use pronounceable names  genymdhms  generationTimestamp  Avoid mental mapping  a  account  Class names  noun not verbs  Method names  verbs  Don't be cute. Use standard names  Destroy, kill, obliterate  Delete  Solution Domain Names vs Problem Domain Names © Pyxis Technologies inc. Top 5 best ways to improve your code 22
    23. 23. PROPERTIES 2 3 4 5 REMOVE COMMENTS 1  Use PascalCase naming  Use noun or adjective  Don't use names that might be confused with a Get  Prefix boolean with Can, Is, or Has © Pyxis Technologies inc. Top 5 best ways to improve your code 23
    24. 24. ENUMS 2 3 4 5 REMOVE COMMENTS 1  First element should be default value  Use PascalCasing naming  Single value enum should be singular  Bit field enum should be plural and has a Flag attribute  Bit field enum values must be coherent (Read & Write == ReadWrite) © Pyxis Technologies inc. Top 5 best ways to improve your code 24
    25. 25. EVENTS 2 3 4 5 REMOVE COMMENTS 1  Use PascalCase naming  Use past to describe post -event and present progressive for pre-event  Provide a virtual method  Provide a cancel behavior for a pre -event © Pyxis Technologies inc. Top 5 best ways to improve your code 25
    26. 26. EVENTS 2 3 4 5 REMOVE COMMENTS 1 © Pyxis Technologies inc. Top 5 best ways to improve your code 26
    27. 27. METHODS 2 3 4 5 REMOVE COMMENTS 1  Use verbs as method name  ProcessPayment  Explicitly express return type in method name  CreateCustomer  GetInvoice  Use coherent naming (Get, Fetch or Retrieve but not all for the same usage context) © Pyxis Technologies inc. Top 5 best ways to improve your code 27
    28. 28. 1 CLARIFY CONTRACTS WHY? 2 3 4  Improve performance  Improve readability  Improve reusability 5 © Pyxis Technologies inc. Top 5 best ways to improve your code 28
    29. 29. CLARIFY CONTRACTS QUAND? 1 2 3 4 5  The is too many paramters (how many is too much?)  A method has more than one responsability  A method use out paramters  Except for:  TryPattern  Service contracts  You need a default value © Pyxis Technologies inc. Top 5 best ways to improve your code 29
    30. 30. CLARIFY CONTRACTS HOW? 1 2 3 4 5  Reduce the number of paramters  Introduce parameter object  Create overload with less parameters  Use default value  Outputs  Use complex return type  Use out parameters only when necessary  Maintain parameters order for overloads © Pyxis Technologies inc. Top 5 best ways to improve your code 30
    31. 31. INTRODUCE PARAMETER OBJECT 2 3 4 5 CLARIFY CONTRACTS 1 © Pyxis Technologies inc. Top 5 best ways to improve your code 31
    32. 32. CREATE OVERLOAD WITH FEWER PARAMETERS 2 3 4 5 CLARIFY CONTRACTS 1 © Pyxis Technologies inc. Top 5 best ways to improve your code 32
    33. 33. USE DEFAULT VALUE 2 3 4 5 CLARIFY CONTRACTS 1 © Pyxis Technologies inc. Top 5 best ways to improve your code 33
    34. 34. CONTROL SCOPE WHY? 1 2 3 4  Avoid side effects  Improve reusability  Improve maintainability 5 © Pyxis Technologies inc. Top 5 best ways to improve your code 34
    35. 35. CONTROL SCOPE WHEN? 1 2 3  A field is used only n a few methods  Internal implementation is exposed by public members 4 5 © Pyxis Technologies inc. Top 5 best ways to improve your code 35
    36. 36. CONTROL SCOPE HOW? 1 2 3 4 5  Visibility  protected  private  internal  Responsibility  Move a field inside a method  Split class  Move variable closer to usage  Lifetime  Create instances as needed  Release reference as early as possible © Pyxis Technologies inc. Top 5 best ways to improve your code 36
    37. 37. FLUSH DEAD CODE WHY? 1 2 3 4 5      Because we must Improve maintainability Improve performance Improve readability 100% test coverage © Pyxis Technologies inc. Top 5 best ways to improve your code 37
    38. 38. 1 FLUSH DEAD CODE WHEN? 2 3 4  You know the code is dead  You think the code is dead  You want the code to be dead 5 © Pyxis Technologies inc. Top 5 best ways to improve your code 38
    39. 39. FLUSH DEAD CODE HOW? 1 2 3 4 5  Identify and remove the code  Delete le code  Compile  Run unit tests  What is dead code?  Code in comments  Code not covered by unit test  Tools  There are tools that delete any line of not covered by at least one unit test © Pyxis Technologies inc. Top 5 best ways to improve your code 39
    40. 40. REFERENCES  Refactoring – Improving the design of existing code  Auteur: Martin Fowler  Edition: Addison Wesley  ISBN: 978-0-201-48567-7  Refactoring to patterns (Martin Fowler signature)  Auteur: Joshua Kerievsky  Edition: Adison Wesley  ISBN: 978-0-321-21335-1  Clean code – a handbook of agile software craftsmanship  Auteur: Robert C. Martin  Edition: Prentice Hall  ISBN: 978-0-132-35088-4  Working effectively with legacy code  Auteur: Michael C. Feather  Edition: Prentice Hall  ISBN: 978-0-13-117705-5 © Pyxis Technologies inc. Top 5 best ways to improve your code 40
    41. 41. THE END  Questions?  Remember Simplify conditionals Remove comments Clarify contracts Control scope Flush dead code  Eric De Carufel  eric@decarufel.net  http://blog.decarufel.net © Pyxis Technologies inc. Top 5 best ways to improve your code 41

    ×