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

Go and write some readable code - Grigory Petrov - Evrone

GoDays
January 22, 2020

Go and write some readable code - Grigory Petrov - Evrone

How often do we say "this code is impossible to change, it should be rewritten"? Sadly, the code in question can be our creation from a few years ago. What can we do to make the code more readable? To address this painful question I will dig into neurophysiology, cognitive intuition and source code, the origin of software complexity problem and how we can deal with it. The talk will follow decomposition, different ways to set navigation beacons within our code, how useful identifiers are crafted and how types are used as traps for future errors. Finally, I will talk about a "gradual" code writing technique that is not limited to a well-known "gradual typing".

GoDays

January 22, 2020
Tweet

More Decks by GoDays

Other Decks in Technology

Transcript

  1. @grigoryvp Grigory Petrov bit.ly/rwrgoen Let's use social networks to communicate

    [email protected] t.me/grigoryvp fb.com/grigoryvp vk.com/grigoryvp github.com/grigoryvp twitter.com/grigoryvp instagram.com/grigoryvp Slides Handle Name 2
  2. @grigoryvp Grigory Petrov bit.ly/rwrgoen What's next? Speaker Grigory Petrov Specialization

    Generalist Role DevRel at Evrone Experience 20 years Talk time 25-30 minutes Questions At the end of the talk, 5-10 minutes 3
  3. @grigoryvp Grigory Petrov bit.ly/rwrgoen What's next? Speaker Grigory Petrov Specialization

    Generalist Role DevRel at Evrone Experience 20 years Talk time 25-30 minutes Questions At the end of the talk, 5-10 minutes 4
  4. @grigoryvp Grigory Petrov bit.ly/rwrgoen What about other fields? • Years

    of education from simple to complex things. • Best practices. 15
  5. @grigoryvp Grigory Petrov bit.ly/rwrgoen What about other fields? • Years

    of education from simple to complex things. • Best practices. • Practice makes perfect. 16
  6. @grigoryvp Grigory Petrov bit.ly/rwrgoen What about other fields? • Years

    of education from simple to complex things. • Best practices. • Practice makes perfect. 17
  7. @grigoryvp Grigory Petrov bit.ly/rwrgoen Software development is different • Zero

    copy cost. • No best practices. • No "software development" education. 21
  8. @grigoryvp Grigory Petrov bit.ly/rwrgoen Software development is different • Zero

    copy cost. • No best practices. • No "software development" education. • Hard to "learn by example". 22
  9. @grigoryvp Grigory Petrov bit.ly/rwrgoen Other people's code is "bad" •

    No intuitive "good" or "bad" code feeling. • Hard to "learn from masters". 26
  10. @grigoryvp Grigory Petrov bit.ly/rwrgoen Other people's code is "bad" •

    No intuitive "good" or "bad" code feeling. • Hard to "learn from masters". • No intuition for "architecture" or "tech debt" for customers. 27
  11. @grigoryvp Grigory Petrov bit.ly/rwrgoen Software complexity challenge • Complexity accumulation

    makes code unreadable. • We can't remove complexity, it's our code value. 30
  12. @grigoryvp Grigory Petrov bit.ly/rwrgoen Software complexity challenge • Complexity accumulation

    makes code unreadable. • We can't remove complexity, it's our code value. • But we can redistribute it! 31
  13. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. •

    "Miller's purse", 7±2. • Neurophysiology suggests around 5. 34
  14. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. •

    "Miller's purse", 7±2. • Neurophysiology suggests around 5. • Long-term memory: limits unknown. 35
  15. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. It's much easier for a developer to read source code if they are familiar with libraries and frameworks in use. 37
  16. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Shared coding standard helps common patterns redistribution into long-term memory. 38
  17. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Shared coding standard helps common patterns redistribution into long-term memory. • gofmt • golint 39
  18. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. 42
  19. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. • A program usually starts with a single file. 43
  20. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. • A program usually starts with a single file. • Number of files increases along with complexity. 44
  21. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. • A program usually starts with a single file. • Number of files increases along with complexity. • Files are grouped into packages. 45
  22. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. • A program usually starts with a single file. • Number of files increases along with complexity. • Files are grouped into packages. • Language abstractions for more decomposition: ◦ struct ◦ interface ◦ channel 46
  23. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. • A program usually starts with a single file. • Number of files increases along with complexity. • Files are grouped into packages. • Language abstractions for more decomposition: ◦ struct ◦ interface ◦ channel • DSL: XML, JSON, YAML etc. 47
  24. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • A program usually starts with a single file. • Number of files increases along with complexity. • Files are grouped into packages. • Language abstractions for more decomposition: ◦ struct ◦ interface ◦ channel • DSL: XML, JSON, YAML etc. • Microservices. 48
  25. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. 49
  26. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. 50
  27. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. 51
  28. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. • A composite identifier is "one thing". Verkehrsinfrastrukturfinanzierungsgesellschaft 52
  29. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. • A composite identifier is "one thing". • Answers the "what is this?" question. 53
  30. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. • A composite identifier is "one thing". • Answers the "what is this?" question. • MixedCase saves a bit of horizontal space. 54
  31. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. • A composite identifier is "one thing". • Answers the "what is this?" question. • MixedCase saves a bit of horizontal space. • Word count increases with complexity. i => users => adminUsers 55
  32. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. 56
  33. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. • Answers the "for what purpose?" question. 57
  34. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. • Answers the "for what purpose?" question. • In the code: 58 Common comments: Remove the user Sort the list Magic bytes Rare (useful) comments: Page no longer linked with a user Access old users RFC12345
  35. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. • Answers the "for what purpose?" question. • In the code. • In commits (due to blame): 59 Common comments: Remove files Small refactoring Few lines change Bugfix Rare (useful) comments: Added by accident, not needed. Better container names #ref 1963: crash research #fix 1963
  36. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. • Answers the "for what purpose?" question. • In the code. • In commits (due to blame). • In tickets (due to commit references): 60 Common comments: Nothing works! Rare (useful) comments: Server answers 500 for /users endpoint GET request, headers and body are empty.
  37. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. 61
  38. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. • If info does not fit into code and comments. 62
  39. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. • If info does not fit into code and comments. • Auto-generation is a special case. 63
  40. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. 64
  41. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. • Can double as a documentation. 65
  42. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. • Can double as a documentation. • Can demonstrate execution path. 66
  43. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. ◦ Types. 67
  44. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. ◦ Types. • "Error traps" in dynamic languages. 68
  45. @grigoryvp Grigory Petrov bit.ly/rwrgoen Complexity redistribution • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. ◦ Types. • "Error traps" in dynamic languages.. • No Gradual Typing in Go. 69
  46. @grigoryvp Grigory Petrov bit.ly/rwrgoen "Gradual" approach • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. ◦ Types. 70
  47. @grigoryvp Grigory Petrov bit.ly/rwrgoen "Gradual" approach • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. ◦ Types. • Gradual decomposition. 71
  48. @grigoryvp Grigory Petrov bit.ly/rwrgoen "Gradual" approach • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. ◦ Types. • Gradual decomposition. • Gradual meta information. 72
  49. @grigoryvp Grigory Petrov bit.ly/rwrgoen "Gradual" approach • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. ◦ Types. • Gradual decomposition. • Gradual meta information. • Gradual typing (not for Go). 73
  50. @grigoryvp Grigory Petrov bit.ly/rwrgoen "Gradual" approach • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. ◦ Types. • Gradual decomposition. • Gradual meta information. • Gradual typing (not for Go). 74
  51. @grigoryvp Grigory Petrov bit.ly/rwrgoen "Gradual" approach • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. ◦ Types. • Gradual decomposition. • Gradual meta information. • Gradual typing (not for Go). 75
  52. @grigoryvp Grigory Petrov bit.ly/rwrgoen "Gradual" approach • Human Memory. ◦

    Libraries. ◦ Frameworks. ◦ Coding standards. • Decomposition. ◦ Files. ◦ Modules. ◦ Abstractions. ◦ DSL. ◦ Applications. • Metainformation. ◦ Identifiers. ◦ Comments. ◦ Documentation. ◦ Tests. ◦ Types. • Gradual decomposition. • Gradual meta information. • Gradual typing (not for Go). 76
  53. @grigoryvp Grigory Petrov bit.ly/rwrgoen It's hard to write a readable

    source code And I don't know what to write on this slide. 77
  54. @grigoryvp Grigory Petrov bit.ly/rwrgoen It's hard to write a readable

    source code And I don't know what to write on this slide. 78