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

Readable code - available without prescription

Readable code - available without prescription

This talk tells the story of a revolution in the world of linguistics and how we can apply it to software development. Everyone agrees on the importance of readable code, but no-one can agree on how to write it. There are many conflicting rules out there, and they are all opinionated and arbitrary. Grammar is also full of rules, and not all rules are the same. “Prescriptive” rules are hard to follow. “Descriptive” rules are intuitive. With the help of Shakespeare, Noam Chomsky and some little green men, we will learn the dangers of prescriptive rules, and discover howreadable.com, an online scientific experiment that can help us write readable code.

Presented at FrontCon 2020
https://2020.frontcon.com/sessions/readable-code-available-without-prescription/

Daniel van Berzon

August 14, 2020
Tweet

More Decks by Daniel van Berzon

Other Decks in Technology

Transcript

  1. A B C const sorted = input .split('') .sort() .join('');

    const chars = input.split(''); const sortedChars = chars.sort(); const sorted = sortedChars.join('');
  2. const postcodeValid = (params) => { return strPresent(params.postcode); } const

    validateParams = (params) => { return firstNameValid(params) && lastNameValid(params) && emailValid(params) && postcodeValid(params); } Whitespace Indentation Break up long lines Legible Code ≠ Readable Code Easier on the eye?
  3. B: Rachel and I went to the cinema 2 words

    for referring to oneself “I” (subject): “I went to the cinema” “Me” (object): "Rachel noticed me” “Rachel” and “I” are both subjects “Rachel and I went to the cinema” So there!!
  4. 1. Opinion 2. Size 3. Age 4. Shape 5. Colour

    6. Origin 7. Material 8. Purpose English has complex grammar rules … Adjective order Green little men Little green men
  5. 1. Opinion 2. Size 3. Age 4. Shape 5. Colour

    6. Origin 7. Material 8. Purpose English has complex grammar rules … Adjective order Green little men Little green men lovely little old long green French silver serving spoon
  6. No one was ever taught this rule in school! Little

    green men Rachel and me went to the cinema Between you and I All debts are cleared between you and I Merchant of Venice Act 3 scene 2
  7. Noam Chomsky Universal Grammar Humans are born with an instinctive

    understanding of grammar. Prescriptive grammar Descriptive grammar
  8. Prescriptive grammar rule Arbitrary rule that prescribes how people should

    speak Robert Lowth 1710 - 1787 Rachel and I went to the cinema
  9. Prescriptive Descriptive Vs. Rachel and I went to the cinema

    How people Should speak How people speak naturally
  10. Learning code readability Psychology of code readability Make your code

    easier to read with Functional Programming Google JavaScript Style Guide Idiomatic.js Rules Code Readability Literature
  11. // bad function sayHi(name) { return 'How are you, '

    + name + '?'; } // good function sayHi(name) { return `How are you, ${name}?`; } Airbnb JavaScript Style Guide: Template strings give you a readable, concise syntax ... “ ”
  12. No native speakers of: JavaScript Python C / C++ Ruby

    Java Universal grammar applies to native language speakers Flaw in the metaphor Baṣa Jawa
  13. Descriptive rules? • Based on empirical evidence • Describe how

    developers read / write code • Based on behaviour rather than opinion Ask a developer how readable they find code Measure how easily they can read code
  14. Online experiment to find descriptive rules for code readability Measures

    readability of coding constructs Showing code snippets to developers Measuring how easily they read them .com
  15. Metric for readability? Can a developer predict output of code?

    Time it takes them to understand the code
  16. A B C const sorted = input .split('') .sort() .join('');

    const chars = input.split(''); const sortedChars = chars.sort(); const sorted = sortedChars.join('');
  17. function sort(input) { var lowerCase = input.toLowerCase(); var lettersArray =

    lowerCase.split(''); var sorted = lettersArray.sort(); return sorted.join(''); } var result = sort('bag'); function sort(input) { return input .toLowerCase() .split('') .sort() .join(''); } var result = sort('bag'); Participant asked to predict value of result ES5? Chaining vs Intermediate
  18. 545 participants 10+ 6-10 5 4 3 2 1 <1

    Years programming Main language Other Go Clojure Kotlin Swift HTML/CSS Python Typescript Ruby C# Java PHP JavaScript
  19. function sort(input) { var lowerCase = input.toLowerCase(); var lettersArray =

    lowerCase.split(''); var sorted = lettersArray.sort(); return sorted.join(''); } var result = sort('bag'); function sort(input) { return input .toLowerCase() .split('') .sort() .join(''); } var result = sort('bag'); Chaining vs Intermediate
  20. Constructs: Chaining methods Operator precedence If statements Pure functions Abstraction

    Boolean algebra Naming Ternary operators Inline functions Early return Comments P value too high
  21. High P value? • No effect to measure • Effect

    too small • Sample size is too low • Flaw in methodology
  22. Precedence vs Brackets var fixed = 200; var monthly =

    20; var result = fixed + (monthly * 12); var fixed = 200; var monthly = 20; var result = fixed + monthly * 12;
  23. var fixed = 200; var monthly = 20; var result

    = fixed + (monthly * 12); var fixed = 200; var monthly = 20; var result = fixed + monthly * 12; Precedence vs Brackets
  24. var result = fixed + (monthly * 12); var result

    = fixed + monthly * 12; An opinionated code formatter
  25. Order of if statements Prefer dealing with the positive case

    first instead of the negative. if (yes) { // foo } else { // bar } if (no) { // bar } else { // foo }
  26. Positive vs Negative branch first function canLegallyDrink(age) { var legal

    = age >= 18; if (legal) { return 'yes'; } else { return 'no'; } } var result = canLegallyDrink(14); function canLegallyDrink(age) { var legal = age >= 18; if (!legal) { return 'no'; } else { return 'yes'; } } var result = canLegallyDrink(14);
  27. Limitations of experiment • Code snippets have to be self

    contained and small • Exercises are not realistic • Larger scale factors impossible to test Consistency - Structure - Approach • Metrics are too simplistic
  28. Improvements • Explore different methodologies - Ask developers change code

    • Increase participation: 1000+ • Alternative approach?
  29. Takeaways • Descriptive > Prescriptive • Possible to objectively measure

    readability • Question arbitrary rules • Science is hard! • Help us! [email protected]
  30. Thank Jake 'Sid' Smith Niall Coleman Clarke Narani van Laarhoven

    Freyja Nash Phil Teare Cian Campbell Oskar Holm Daniel van Berzon @dvberzon vanberzon.com howreadable.com you @ocastahq ocasta.com