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

Stripecon - Writing maintainable code

kay
September 21, 2018

Stripecon - Writing maintainable code

Presentation for Stripecon 2018 @ Enschede
Writing maintainable code in an ever growing environment

kay

September 21, 2018
Tweet

Other Decks in Technology

Transcript

  1. WRITING WRITING MAINTAINABLE CODE MAINTAINABLE CODE IN AN EVER GROWING

    ENVIRONMENT IN AN EVER GROWING ENVIRONMENT Stripecon 2018 - Enschede 1
  2. WHAT BAD CODE BASES SHARE WHAT BAD CODE BASES SHARE

    No structure, whatsoever Different coding styles... even in the same line Lack of any form of architecture Unreadable 5
  3. DOCUMENTATION DOCUMENTATION Use README's for ease in project setup Write

    down the planned architecture Write down code styles / conventions / etc. 10
  4. COMMENTS IN CODE COMMENTS IN CODE ARE THE SOURCE OF

    ARE THE SOURCE OF GREAT EVIL GREAT EVIL ..EXCEPT WHEN IT COMES TO DOCS ..EXCEPT WHEN IT COMES TO DOCS ...OR RESOURCES, MAGIC AND OTHER STUFF LIKE THAT ...OR RESOURCES, MAGIC AND OTHER STUFF LIKE THAT 12
  5. UNIT TESTING UNIT TESTING Some form of documentation Proper unit

    tests (not just happy flow) Eases refactoring Use it in your C.I. 13
  6. Always code as if the guy who ends up maintaining

    your code will be a violent psychopath who knows where you live. — John F. Woods, 1991 15
  7. Non understandable (variable) naming According to: // A has no

    meaning, whatsoever $a = [ "Bella" ,"Lucy" , "Daisy" ]; // Variable is self explaing $top_3_most_popular_female_dog_names = [ "Bella" ,"Lucy" ,"Daisy" ]; wideopenpets.com 17
  8. Non understandable (variable) naming // Abbreviation is not clear //

    $thing doesn't make it any better either $trans_cost = get_trans_cost($thing); // Now we now it are transaction costs $transaction_cost = get_trans_cost($thing); 18
  9. UNSURE WHY SOME CODE WORKS UNSURE WHY SOME CODE WORKS

    function cartesian(arg) { var r = [], max = arg.length-1; function helper(arr, i) { for (var j=0, l=arg[i].length; j<l; j++) { var a = arr.slice(0); // clone arr a.push(arg[i][j]); if (i==max) r.push(a); else helper(a, i+1); } } helper([], 0); return r; } 19
  10. UNSURE WHY SOME CODE WORKS UNSURE WHY SOME CODE WORKS

    // Cartesian shuffle in order to generate all possible combina // see: https://stackoverflow.com/q/33524132 function cartesian(arg) { var r = [], max = arg.length-1; ... } helper([], 0); return r; } 20
  11. ABRA-CADABRA CODE ABRA-CADABRA CODE function cartesian(arg) { var r =

    [], max = arg.length-1; function helper(arr, i) { for (var j=0, l=arg[i].length; j<l; j++) { var a = arr.slice(0); // clone arr a.push(arg[i][j]); if (i==max) r.push(a); else helper(a, i+1); } } helper([], 0); return r; } 22
  12. SET VARIABLES WHERE THEY BELONG SET VARIABLES WHERE THEY BELONG

    function cartesian(arg) { var r = [], max = arg.length-1; function helper(arr, i) { for (var j=0, l=arg[i].length; j<l; j++) { var a = arr.slice(0); // clone arr a.push(arg[i][j]); if (i==max) ... } } ... } 25
  13. SET VARIABLES WHERE THEY BELONG SET VARIABLES WHERE THEY BELONG

    function cartesian(arg) { var result = []; function helper(arr, i) { var maxLength = args[i].length - 1; for (var helperI = 0, helperI<setLength; helperI++) { var a = arr.slice(0); // clone arr a.push(arg[helperI][j]); if (i===maxLength) ... } } ... } 26
  14. 31

  15. The Flying V for (var i = 0; i <

    array.lenght; i++) { for (var nestedArrI = 0; nestedArrI < array[i].length; i++ if (array[i][nestedArrI] === true) { while (1 == 1) { switch(array[i][nestedArrI]) { case X: if (...) { ... } } } } } } } 36
  16. CATCH/RETURN EARLY CATCH/RETURN EARLY OR AVOID HAVING TO RETURN EARLY

    AT ALL OR AVOID HAVING TO RETURN EARLY AT ALL 37
  17. BE DEFENSIVE IN YOUR BE DEFENSIVE IN YOUR PROGRAMMING PROGRAMMING

    Try / catch shows uncertainties in your code Use them wisely 38
  18. UNIT TESTING UNIT TESTING Don't just go for code coverage,

    go for case coverage Have a bug? Add a unit test Good code is testable 41
  19. 42

  20. THINK AHEAD! THINK AHEAD! Work out how you like to

    implement thing and discuss this with your team 44
  21. DON'T BE THE ROCKSTAR DON'T BE THE ROCKSTAR YOUR CODE

    IS ONLY AS MAINTAINABLE YOUR CODE IS ONLY AS MAINTAINABLE AS THE MOST JUNIOR DEV IN YOUR TEAM AS THE MOST JUNIOR DEV IN YOUR TEAM 45
  22. KEEP YOUR DOCS UP-TO DATE KEEP YOUR DOCS UP-TO DATE

    REVIEW YOUR STYLE GUIDE / REVIEW YOUR STYLE GUIDE / CONVENTIONS CONVENTIONS 47
  23. HELP.. MY CODE HELP.. MY CODE BASE IS !@#$ BASE

    IS !@#$ WHAT TO DO NOW? WHAT TO DO NOW? 48
  24. DON'T PANIC DON'T PANIC Rome wasn't buid in a day...

    maintainability comes with small steps Try to get your technical depth in scope 49
  25. USE BUG FIXES / SMALL FEATURES TO USE BUG FIXES

    / SMALL FEATURES TO IMPROVE YOUR CODE IMPROVE YOUR CODE Don't forget to add unit tests 50
  26. NEVER KEEP CONTRIBUTING TO NEVER KEEP CONTRIBUTING TO UNMAINTAINABLE CODE

    UNMAINTAINABLE CODE Use by-passes with maintainability in mind 51
  27. APPLY ALL THAT YOU'VE LEARNED APPLY ALL THAT YOU'VE LEARNED

    TODAY, AND SPREAD THE WORD TODAY, AND SPREAD THE WORD 52
  28. TAKE AWAYS TAKE AWAYS Setup code conventions and coding styles

    Document the planned code structure and architecture Avoid common mistakes 53
  29. TAKE AWAYS TAKE AWAYS Setup linters, both for Dev-enviroment and

    C.I. Ensure your code stays readable and comprehensible Keep evaluating your code maintainability and keep technical depth in scope 54
  30. FURTHER TIPS FURTHER TIPS Design Patterns (Gang of Four) Learn

    (some) functional programming Simple made Easy (Rich Hickey) 56
  31. f`