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

Naming Things (with notes)

Avatar for Pete Pete
April 12, 2018

Naming Things (with notes)

There's an old joke that goes, “The two hardest things in programming are cache invalidation, naming things, and off-by-one errors.” In this talk, we'll discuss the subtle art of naming things – a practice we do every day but rarely talk about.

Avatar for Pete

Pete

April 12, 2018

More Decks by Pete

Other Decks in Programming

Transcript

  1. @aupajo .co.nz Hi, I’m Pete. I’ve been writing Ruby for

    about 11 years, I work for a company called Trineo, and I live in New Zealand. That’s me squinting in the sun at RubyConf AU back in March.
  2. What is code? Ostensibly for the computer. In reality, it’s

    a recipe that has to be digested by two audiences: the computer and the developers. You have to encode behaviour and intent.
  3. What is good code? Good code is efficient. By efficient,

    I don’t mean efficient for the computer, I also mean efficient for the human mind. To understand that, I want you to focus on an idea: cognitive overhead.
  4. Why is naming things important? It’s our primary tool for

    communication. It is the artefact left behind of someone’s thoughts and intentions. A good name conveys how to use something and give you a clear sense of when it should change.
  5. Why is naming things hard? Often because when we’re forced

    to pick a name, we have to articulate a concept. Often it is hard to express a concept in a simple word or two. Ideas can be subtle. Struggling to name something is an important part of of design. Difficulty comprehending indicates more clarification needs to be done.
  6. Some developers solve this problem with comments. But why is

    it you have to explain the code? Why can’t the code explain itself?
  7. Nothing has changed but for your ability to understand this

    code. To the computer, these expressions are identical. But everything important to you, the developer, has changed.
  8. If it isn’t meant to be an array, avoid numbers.

    This is a common pattern seen in tests.
  9. This is better. Now you’re articulating why you have two

    users: one is earlier and one is later.
  10. Type suffixes usually add more context than is necessary, and

    cause greater churn when you replay one type for another (bigger concern in a dynamic language).
  11. Be careful of words like “info” and “data”. What do

    they mean? Are these all equivalent. It’s a bit like saying “thing” in English.
  12. Prefixes: not the worst thing, but consider why this happens.

    It’s because the author wanted to use the name “File” twice. Consider what would happen if they were forced to pick another name.
  13. What does a “page manager” do? Any guesses? Well, it

    manages a page. So anything that could plausibly be considered “managing a page” could easily get added to this class. These classes quickly balloon out of control.
  14. By turning this into a formatted page, you are now

    talking about the end result of what this class does – what you’re trying to achieve. This allows you to think more clearly about what is and isn’t needed.
  15. Specificity is good, but use your judgement. For instance, it’s

    probably fine to call a postal address simply an “address”.
  16. However, if your application deals with more than one type

    of address, you may want to consider increasing its specificity.
  17. These methods invite confusion, because you constantly have to ask

    yourself “is this object add, insert, or append”?
  18. Avoid patterns in names. Design patterns are implementation details, and

    encoding the pattern into the name prevents altering that class in the future to not use that pattern. They’re unnecessary information that usually only adds cognitive overhead.
  19. Directories and their structure are just as important as the

    file name. Both can add context and lower overhead for finding files or figuring out where to place files. A well- named directory can be more powerful than a well-named class.
  20. Consider the ergonomics. Folders with too many items take longer

    to read. A folder with one item is like having a category with one item, which is not a useful category to have.
  21. Long names are bad, because they often contain too many

    concepts, indicating an object that has a high cognitive overhead.
  22. If necessary, make the name longer. Brevity is good, but

    the ultimate goal is clarity. Here, the key concept is that we have vehicle types.
  23. When the inevitable shortening happens, it’s easier to do things

    like this. Not perfect, but far better than “bpvt”.
  24. What is it you do? You are a translator for

    two audiences. You are the bridge between the human and the computer. You need to know how computers work and how humans work – both the humans which your software serves, and the humans which your software will by changed by. Your primary weapon in your toolbox against ambiguity and cognitive overhead – the forces that corrode a software’s maintainability – is to name. You name things every day. Be mindful about the words you choose. Software should be written to be read by another developer.
  25. Programs must be written for people to read, and only

    incidentally for machines to execute. “ Harold Abelson Structure and Interpretation of Computer Programs
  26. The ratio of time spent reading versus writing is well

    over 10 to 1. We are constantly reading old code as part of the effort to write new code. Making it easy to read makes it easier to write. Robert C. Martin “Uncle Bob”
  27. The ratio of time spent reading versus writing is well

    over 10 to 1. We are constantly reading old code as part of the effort to write new code. Making it easy to read makes it easier to write. Robert C. Martin “Uncle Bob”
  28. The ratio of time spent reading versus writing is well

    over 10 to 1. We are constantly reading old code as part of the effort to write new code. Making it easy to read makes it easier to write. Robert C. Martin “Uncle Bob”
  29. A parable about stones Marco Polo describes a bridge, stone

    by stone. "But which is the stone that supports the bridge?" Kublai Khan asks. "The bridge is not supported by one stone or another," Marco answers, "but by the line of the arch that they form.” Kublai Khan remains silent, reflecting. Then he adds: "Why do you speak to me of the stones? It is only the arch that matters to me.” Polo answers: "Without stones there is no arch."
  30. On Exactitude To my mind exactitude means three things above

    all: (1) a well-defined and well-calculated plan for the work in question; (2) an evocation of clear, incisive, memorable images; (3) a language as precise as possible both in choice of words and in expression of the subtleties of thought and imagination. - Italo Calvino