Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Giving Code a Good Name

1,513 views

Published on

Presented at .NET South West (2017-07-25)

Code is basically made up of three things: names, spacing and punctuation. With these three tools a programmer needs to communicate intent, and not simply instruct. But if we look at most approaches to naming, they are based on the idea that names are merely labels, so that discussion of identifier naming becomes little more than a discussion of good labelling.

A good name is more than a label; a good name should change the way the reader thinks. A good name should describe structure with intention, as opposed to the affix-heavy approach common to many naming conventions in current use, where the addition of more prefixes and suffixes becomes homeopathic, diluting the meaning. Good naming is part of good design. This session looks at why and what it takes to get a good name.

Published in: Software

Giving Code a Good Name

  1. 1. Giving Code a Good Name @KevlinHenney
  2. 2. It's just naming.
  3. 3. It's just semantics.
  4. 4. It's just meaning.
  5. 5. / WordFriday
  6. 6. code, noun ▪ a set of instructions for a computer ▪ a computer program, or a portion thereof ▪ a system of words, figures or symbols used to represent others, especially for the purposes of secrecy ▪ a set of conventions or principles governing behaviour or activity in a particular domain Concise Oxford English Dictionary ∙ Oxford English Dictionary ∙ Merriam-Webster's Collegiate Dictionary
  7. 7. As a I want So that $Role $Feature $Benefit
  8. 8. As a I want So that programmer $Feature $Benefit
  9. 9. As a I want So that programmer good identifier names $Benefit
  10. 10. As a I want So that programmer good identifier names I can read code
  11. 11. As a I want So that programmer good identifier names I can read and understand code
  12. 12. As a I want So that programmer good identifier names I can spend less time determining the meaning of code and more time coding meaningfully
  13. 13. As a I want So that programmer ??? I can spend less time determining the meaning of code and more time coding meaningfully
  14. 14. Signal-to-noise ratio (often abbreviated SNR or S/N) is a measure used in science and engineering that compares the level of a desired signal to the level of background noise. Signal-to-noise ratio is sometimes used informally to refer to the ratio of useful information to false or irrelevant data in a conversation or exchange. http://en.wikipedia.org/wiki/Signal_to_noise_ratio
  15. 15. To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? William Shakespeare Hamlet
  16. 16. Continuing existence or cessation of existence: those are the scenarios. Is it more empowering mentally to work towards an accommodation of the downsizings and negative outcomes of adversarial circumstance, or would it be a greater enhancement of the bottom line to move forwards to a challenge to our current difficulties, and, by making a commitment to opposition, to effect their demise? Tom Burton Long Words Bother Me
  17. 17. People will be using the words you choose in their conversation for the next 20 years. You want to be sure you do it right. Unfortunately, many people get all formal [...]. Just calling it what it is isn't enough.
  18. 18. public class ConfigurationManager { ... }
  19. 19. public class Configuration { ... }
  20. 20. They have to tack on a flowery, computer science-y, impressive sounding, but ultimately meaningless word, like Object, Thing, Component, Part, Manager, Entity, or Item.
  21. 21. 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"
  22. 22. 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"
  23. 23. 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
  24. 24. http://eightyeightgames.com/2011/03/14/rollrover-source-analysis/
  25. 25. if (portfolioIdsByTraderId.get(trader.getId()) .containsKey(portfolio.getId())) { ... } Dan North "Code in the Language of the Domain"
  26. 26. if (trader.canView(portfolio)) { ... } Dan North "Code in the Language of the Domain"
  27. 27. Agglutination is a process in linguistic morphology derivation in which complex words are formed by stringing together morphemes, each with a single grammatical or semantic meaning. Languages that use agglutination widely are called agglutinative languages. http://en.wikipedia.org/wiki/Agglutination
  28. 28. pneumonoultramicroscopicsilicovolcanoconiosis Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz fylkestrafikksikkerhetsutvalgssekretariatslederfunksjonene muvaffakiyetsizleştiricileştiriveremeyebileceklerimizdenmişsinizcesine hippopotomonstrosesquipedaliophobia
  29. 29. http://www.bonkersworld.net/object-world/
  30. 30. http://www.bonkersworld.net/object-world/ OBJECT-ORIENTED VenetianBlind Door Television Picture Glass Sofa TelevisionRemoteControl Peephole
  31. 31. AccessViolationException ArgumentOutOfRangeException ArrayTypeMismatchException BadImageFormatException CannotUnloadAppDomainException EntryPointNotFoundException IndexOutOfRangeException InvalidOperationException OverflowException
  32. 32. AccessViolation ArgumentOutOfRange ArrayTypeMismatch BadImageFormat CannotUnloadAppDomain EntryPointNotFound IndexOutOfRange InvalidOperation Overflow
  33. 33. ArgumentException ArithmeticException ContextMarshalException FieldAccessException FormatException NullReferenceException ObjectDisposedException RankException TypeAccessException
  34. 34. Argument Arithmetic ContextMarshal FieldAccess Format NullReference ObjectDisposed Rank TypeAccess
  35. 35. InvalidArgument InvalidArithmeticOperation FailedContextMarshal InvalidFieldAccess InvalidFormat NullDereferenced OperationOnDisposedObject ArrayRankMismatch InvalidTypeAccess
  36. 36. Omit needless words. William Strunk and E B White The Elements of Style
  37. 37. Naomi Epel The Observation Deck
  38. 38. LoanMember BookCopy
  39. 39. LoanIMember IBookCopy Member BookCopy
  40. 40. Loan Member BookCopy
  41. 41. role, noun ▪ an actor's part in a play, film, etc. ▪ a person or thing's function in a particular situation ▪ a function, part or expected behaviour performed in a particular operation or process Concise Oxford English Dictionary ∙ Merriam-Webster's Collegiate Dictionary
  42. 42. LoanBorrower LoanItem Member BookCopy
  43. 43. interface
  44. 44. TreeBuilderParser
  45. 45. TreeBuilderParser BuilderImpl
  46. 46. TreeParserListenerParser Builder
  47. 47. Tree ParserListenerParser Builder
  48. 48. Connection Connection Factory Connection Impl
  49. 49. Connection Connection Pool Pooled Connection
  50. 50. Everybody knows that TDD stands for Test Driven Development. However, people too often concentrate on the words "Test" and "Development" and don't consider what the word "Driven" really implies. For tests to drive development they must do more than just test that code performs its required functionality: they must clearly express that required functionality to the reader. That is, they must be clear specifications of the required functionality. Tests that are not written with their role as specifications in mind can be very confusing to read. Nat Pryce and Steve Freeman "Are Your Tests Really Driving Your Development?"
  51. 51. public class Stack<T> { private  public Stack()  public int Depth  public T Top  public void Push(T newTop)  public void Pop()  }
  52. 52. [TestFixture] public class StackTests { [Test] public void TestConstructor()  [Test] public void TestDepth()  [Test] public void TestTop()  [Test] public void TestPush()  [Test] public void TestPop()  }
  53. 53. [TestFixture] public class StackTests { [Test] public void Constructor()  [Test] public void Depth()  [Test] public void Top()  [Test] public void Push()  [Test] public void Pop()  }
  54. 54. public class Stack<T> { private  public Stack()  public int Depth  public T Top  public void Push(T newTop)  public void Pop()  }
  55. 55. public class Stack<T> { private  public int Depth  public T Top  public void Push(T newTop)  public void Pop()  }
  56. 56. [TestFixture] public class StackTests { [Test] public void Depth()  [Test] public void Top()  [Test] public void Push()  [Test] public void Pop()  }
  57. 57. methodtest test test method method test test
  58. 58. namespace Stack_spec { [TestFixture] public class A_new_stack { [Test] public void has_no_depth()  } [TestFixture] public class An_empty_stack { [Test] public void throws_when_queried_for_its_top_item()  [Test] public void throws_when_popped()  [Test] public void acquires_depth_by_retaining_a_pushed_item_as_its_top()  } [TestFixture] public class A_non_empty_stack { [Test] public void becomes_deeper_by_retaining_a_pushed_item_as_its_top()  [Test] public void on_popping_reveals_tops_in_reverse_order_of_pushing()  } }
  59. 59. namespace Stack_spec { [TestFixture] public class A_new_stack { [Test] public void has_no_depth()  } [TestFixture] public class An_empty_stack { [Test] public void throws_when_queried_for_its_top_item()  [Test] public void throws_when_popped()  [Test] public void acquires_depth_by_retaining_a_pushed_item_as_its_top()  } [TestFixture] public class A_non_empty_stack { [Test] public void becomes_deeper_by_retaining_a_pushed_item_as_its_top()  [Test] public void on_popping_reveals_tops_in_reverse_order_of_pushing()  } }
  60. 60. As a I want So that programmer ??? I can spend less time determining the meaning of code and more time coding meaningfully
  61. 61. As a I want So that programmer identifiers to communicate directly and with intent I can spend less time determining the meaning of code and more time coding meaningfully
  62. 62. At some level the style becomes the substance.

×