Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Prochain SlideShare
The Error of Our Ways
The Error of Our Ways
Chargement dans…3
×

Consultez-les par la suite

1 sur 49 Publicité

Good Code

Télécharger pour lire hors ligne

Presented at the European Bioinformatics Institute (17th March 2017)

We often talk about good code — that we would like to write it, that there isn't enough of it, that it should not be considered an optional attribute of a codebase. We often talk about it but, when it comes to being precise, we don't always agree what constitutes good code, nor do we necessarily share a common view on its value.

Presented at the European Bioinformatics Institute (17th March 2017)

We often talk about good code — that we would like to write it, that there isn't enough of it, that it should not be considered an optional attribute of a codebase. We often talk about it but, when it comes to being precise, we don't always agree what constitutes good code, nor do we necessarily share a common view on its value.

Publicité
Publicité

Plus De Contenu Connexe

Les utilisateurs ont également aimé (20)

Publicité

Similaire à Good Code (20)

Plus par Kevlin Henney (20)

Publicité

Plus récents (20)

Good Code

  1. 1. Good Code @KevlinHenney
  2. 2. Four basic premises of writing: clarity, brevity, simplicity, and humanity. William Zinsser
  3. 3. Let us examine [software's] difficulties. Following Aristotle, I divide them into essence — the difficulties inherent in the nature of software — and accidents — those difficulties that today attend its production but that are not inherent.
  4. 4. How much of what software engineers now do is still devoted to the accidental, as opposed to the essential?
  5. 5. http://cacm.acm.org/magazines/2014/2/171689-mars-code
  6. 6. There are standard precautions that can help reduce risk in complex software systems. This includes the definition of a good software architecture based on a clean separation of concerns, data hiding, modularity, well-defined interfaces, and strong fault-protection mechanisms. Gerard J Holzmann "Mars Code", CACM 57(2) http://cacm.acm.org/magazines/2014/2/171689-mars-code/fulltext
  7. 7. Firmitas Utilitas Venustas
  8. 8. Habitability is the characteristic of source code that enables programmers, coders, bug-fixers, and people coming to the code later in its life to understand its construction and intentions and to change it comfortably and confidently.
  9. 9. Habitability makes a place livable, like home. And this is what we want in software — that developers feel at home, can place their hands on any item without having to think deeply about where it is.
  10. 10. Comments A delicate matter, requiring taste and judgement. I tend to err on the side of eliminating comments, for several reasons. First, if the code is clear, and uses good type names and variable names, it should explain itself. Second, comments aren't checked by the compiler, so there is no guarantee they're right, especially after the code is modified. A misleading comment can be very confusing. Third, the issue of typography: comments clutter code. Rob Pike, "Notes on Programming in C"
  11. 11. There is a famously bad comment style: i=i+1; /* Add one to i */ and there are worse ways to do it: /********************************** * * * Add one to i * * * **********************************/ i=i+1; Don't laugh now, wait until you see it in real life. Rob Pike, "Notes on Programming in C"
  12. 12. A common fallacy is to assume authors of incomprehensible code will somehow be able to express themselves lucidly and clearly in comments. Kevlin Henney https://twitter.com/KevlinHenney/status/381021802941906944
  13. 13. http://www.bonkersworld.net/object-world/
  14. 14. http://www.bonkersworld.net/object-world/ OBJECT-ORIENTED VenetianBlind Door Television Picture Glass Sofa TelevisionRemoteControl Peephole
  15. 15. Naomi Epel The Observation Deck
  16. 16. if (portfolioIdsByTraderId.get(trader.getId()) .containsKey(portfolio.getId())) { ... } Dan North, "Code in the Language of the Domain" 97 Things Every Programmer Should Know
  17. 17. if (trader.canView(portfolio)) { ... } Dan North, "Code in the Language of the Domain" 97 Things Every Programmer Should Know
  18. 18. Details count. Peter Weinberger
  19. 19. Architecture represents the significant design decisions that shape a system, where significant is measured by cost of change. Grady Booch
  20. 20. http://www.theregister.co.uk/2016/03/23/npm_left_pad_chaos/
  21. 21. function leftpad (str, len, ch) { str = String(str); var i = -1; if (!ch && ch !== 0) ch = ' '; len = len - str.length; while (++i < len) { str = ch + str; } return str; }
  22. 22. function leftpad(content, length, pad) { content = String(content) pad = String(pad || pad === 0 ? pad : ' ')[0] var left = Math.max(length - content.length, 0) return pad.repeat(left) + content }
  23. 23. var cache = [ '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ]; function leftPad (str, len, ch) { // convert `str` to `string` str = str + ''; // `len` is the `pad`'s length now len = len - str.length; // doesn't need to pad if (len <= 0) return str; // `ch` defaults to `' '` if (!ch && ch !== 0) ch = ' '; // convert `ch` to `string` ch = ch + ''; // cache common use cases if (ch === ' ' && len < 10) return cache[len] + str; // `pad` starts with an empty string var pad = ''; // loop while (true) { // add `ch` to `pad` if `len` is odd if (len & 1) pad += ch; // divide `len` by 2, ditch the remainder len >>= 1; // "double" the `ch` so this operation count grows logarithmically on `len` // each time `ch` is "doubled", the `len` would need to be "doubled" too // similar to finding a value in binary search tree, hence O(log(n)) if (len) ch += ch; // `len` is 0, exit the loop else break; } // pad `str`! return pad + str; }
  24. 24. I have yet to see any problem, however complicated, which, when you looked at it in the right way, did not become still more complicated. Anderson's Law
  25. 25. truths = { "Padding an empty string to a length of 0 results in an empty string": leftpad("", 0, "X") === "", "Padding a non-empty string to a shorter length results in the same string": leftpad("foobar", 3, "X") === "foobar", "Padding a non-empty string to a negative length results in the same string": leftpad("foobar", -3, "X") === "foobar", "Padding a non-empty string to its length results in the same string": leftpad("foobar", 6, "X") === "foobar", "Padding to a longer length with a single character fills to the left": leftpad("foobar", 8, "X") === "XXfoobar", "Padding to a longer length with surplus characters fills using only first": leftpad("foobar", 10, "XY") === "XXXXfoobar", "Padding to a longer length with an empty string fills with space": leftpad("foobar", 8, "") === " foobar", "Padding to a longer length with no specified fill fills with space": leftpad("foobar", 9) === " foobar", "Padding to a longer length with integer 0 fills with 0": leftpad("foobar", 7, 0) === "0foobar", "Padding to a longer length with single-digit integer fills with digit": leftpad("foobar", 10, 1) === "1111foobar", "Padding to a longer length with multiple-digit integer fills with first digit": leftpad("foobar", 10, 42) === "4444foobar", "Padding to a longer length with negative integer fills with -": leftpad("foobar", 8, -42) === "--foobar", "Padding a non-string uses string representation": leftpad(4.2, 5, 0) === "004.2", }
  26. 26. truths = { "Padding an empty string to a length of 0 results in an empty string": leftpad("", 0, "X") === "", "Padding a non-empty string to a shorter length results in the same string": leftpad("foobar", 3, "X") === "foobar", "Padding a non-empty string to a negative length results in the same string": leftpad("foobar", -3, "X") === "foobar", "Padding a non-empty string to its length results in the same string": leftpad("foobar", 6, "X") === "foobar", "Padding to a longer length with a single character fills to the left": leftpad("foobar", 8, "X") === "XXfoobar", "Padding to a longer length with surplus characters fills using only first": leftpad("foobar", 10, "XY") === "XXXXfoobar", "Padding to a longer length with an empty string fills with space": leftpad("foobar", 8, "") === " foobar", "Padding to a longer length with no specified fill fills with space": leftpad("foobar", 9) === " foobar", "Padding to a longer length with integer 0 fills with 0": leftpad("foobar", 7, 0) === "0foobar", "Padding to a longer length with single-digit integer fills with digit": leftpad("foobar", 10, 1) === "1111foobar", "Padding to a longer length with multiple-digit integer fills with first digit": leftpad("foobar", 10, 42) === "4444foobar", "Padding to a longer length with negative integer fills with -": leftpad("foobar", 8, -42) === "--foobar", "Padding a non-string uses string representation": leftpad(4.2, 5, 0) === "004.2", }
  27. 27. truths = { "Padding an empty string to a length of 0 results in an empty string": leftpad("", 0, "X") === "", "Padding a non-empty string to a shorter length results in the same string": leftpad("foobar", 3, "X") === "foobar", "Padding a non-empty string to a negative length results in the same string": leftpad("foobar", -3, "X") === "foobar", "Padding a non-empty string to its length results in the same string": leftpad("foobar", 6, "X") === "foobar", "Padding to a longer length with a single character fills to the left": leftpad("foobar", 8, "X") === "XXfoobar", "Padding to a longer length with surplus characters fills using only first": leftpad("foobar", 10, "XY") === "XXXXfoobar", "Padding to a longer length with an empty string fills with space": leftpad("foobar", 8, "") === " foobar", "Padding to a longer length with no specified fill fills with space": leftpad("foobar", 9) === " foobar", "Padding to a longer length with integer 0 fills with 0": leftpad("foobar", 7, 0) === "0foobar", "Padding to a longer length with single-digit integer fills with digit": leftpad("foobar", 10, 1) === "1111foobar", "Padding to a longer length with multiple-digit integer fills with first digit": leftpad("foobar", 10, 42) === "4444foobar", "Padding to a longer length with negative integer fills with -": leftpad("foobar", 8, -42) === "--foobar", "Padding a non-string uses string representation": leftpad(4.2, 5, 0) === "004.2", }
  28. 28. toMap = object => new Map(Object.entries(object)) format = (proposition, ok) => proposition.fontcolor(ok ? "green" : "red") + "<br>" present = truths => toMap(truths).forEach( (ok, proposition) => write(format(proposition, ok))) present(truths)
  29. 29. Padding an empty string to a length of 0 results in an empty string Padding a non-empty string to a shorter length results in the same string Padding a non-empty string to a negative length results in the same string Padding a non-empty string to its length results in the same string Padding to a longer length with a single character fills to the left Padding to a longer length with surplus characters fills using only first Padding to a longer length with an empty string fills with space Padding to a longer length with no specified fill fills with space Padding to a longer length with integer 0 fills with 0 Padding to a longer length with single-digit integer fills with digit Padding to a longer length with multiple-digit integer fills with first digit Padding to a longer length with negative integer fills with - Padding a non-string uses string representation
  30. 30. Padding an empty string to a length of 0 results in an empty string Padding a non-empty string to a shorter length results in the same string Padding a non-empty string to a negative length results in the same string Padding a non-empty string to its length results in the same string Padding to a longer length with a single character fills to the left Padding to a longer length with surplus characters fills using only first Padding to a longer length with an empty string fills with space Padding to a longer length with no specified fill fills with space Padding to a longer length with integer 0 fills with 0 Padding to a longer length with single-digit integer fills with digit Padding to a longer length with multiple-digit integer fills with first digit Padding to a longer length with negative integer fills with - Padding a non-string uses string representation
  31. 31. For every activity there is a certain appropriate scale.
  32. 32. The Facebook iOS app has over 18,000 Objective-C classes, and in a single week 429 people contributing to it. Facebook's code quality problem Graham King http://www.darkcoding.net/software/facebooks-code-quality-problem/
  33. 33. Your customers do not buy your software by the line. David Evans
  34. 34. Quote from Kevlin Henney Graphic by Sebastian Hermida http://sbastn.com/2009/06/typing-is-not-the-bottleneck/

×