{"id":1901,"date":"2011-03-03T14:13:35","date_gmt":"2011-03-03T17:13:35","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=1901"},"modified":"2011-03-03T20:52:49","modified_gmt":"2011-03-03T23:52:49","slug":"cognitive-load-and-the-importance-of-naming","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2011\/03\/cognitive-load-and-the-importance-of-naming\/","title":{"rendered":"Cognitive load and the importance of naming"},"content":{"rendered":"
\nThere are only two hard things in Computer Science: cache invalidation and naming things. — Phil Karlton\n<\/p><\/blockquote>\n
What is cognitive load?<\/h2>\n
When I had Human-Computer Interaction classes in college, I got acquainted with the concepts of Cognition and Cognitive Load. My teacher showed us a very simple example to explain what are those concepts, compare the following examples:<\/p>\n
\n
- 551153219421<\/li>\n
- 55 11 5321 9421<\/li>\n<\/ol>\n
The second example is obviously more simpler to comprehend and also may lead us to think that they are maybe telephone numbers. That means the second example has much less cognitive load, i.e., you need less mental work to give those numbers context and meaning.<\/p>\n
What does it mean in code writing?<\/h2>\n
That also applies to naming things in our projects, and heavily. Imagine a new co-worker in your project having to learn all the names and concepts. If you can ease the burden by naming things well, your fellow colleague will be a lot more productive.<\/p>\n
I’ve been working on my Outpost gem <\/a> and there’s a part on it that verifies your system’s output in order to tell whether the system is failing or not. I called it “hooks”. This is a nice example on how bad naming can affect comprehension of a project.<\/p>\n
Let’s give it a little bit of thought. What does “hooks” mean? It means, in computing, something that will be called back eventually. Ok, we got that part right. But what does it mean in Outpost? In order to fully understand the concepts, I have to:<\/p>\n
\n
- Understand the meaning of hooks in computing (not troublesome, since it is pre-acquired knowledge and it has a broad meaning)<\/li>\n
- Understand what Outpost does<\/li>\n
- Understand what a Scout does<\/li>\n
- Apply the concept of hooks in the context of Outpost since the “hook” concept does not add much value to me<\/li>\n
- Understand what hooks do in the context of Outpost.<\/li>\n<\/ol>\n
Five “cognitive steps” in order to fully understand what the name “hook” mean in Outpost. But, after a while, during my work at Ruby Mendicant University<\/a>, Gregory Brown suggested me to rename from “hook” to “expectation”. Expectation is also a common knowledge in computing, but even more, it has a very strong meaning for people used to TDD. So, now, to understand how “expectations” fit in Outpost, I have to:<\/p>\n
\n
- Understand the meaning of the word “expectation” in TDD<\/li>\n
- Understand what Outpost does<\/li>\n
- Understand what a Scout does<\/li>\n
- Understand what an expectation do in the context of Outpost.<\/li>\n<\/ol>\n
One less step. It is very hard to quantify how much did we gain in this simple name change, but we can feel there’s much less cognitive load involved. If you’re thinking the name you’ve chosen is good, try to think about the steps involved to understand the concept you’re trying to elucidate.<\/p>\n
If you need more practical advices, Uncle Bob’s written a whole chapter about naming in his “Clean Code” book. In there lies a bunch of rules and tips on how to improve your naming by reducing the cognitive load involved in understanding code, getting closer to the path of having cleaner code.<\/p>\n
Do you have this kind of philosophical thought about code as well? How do you make it practical and what are the improvements you’ve seen in your projects? Please share with us!<\/p>\n","protected":false},"excerpt":{"rendered":"
There are only two hard things in Computer Science: cache invalidation and naming things. — Phil Karlton What is cognitive load? When I had Human-Computer Interaction classes in college, I got acquainted with the concepts of Cognition and Cognitive Load. My teacher showed us a very simple example to explain what are those concepts, compare … \u00bb<\/a><\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[142],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/1901"}],"collection":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=1901"}],"version-history":[{"count":18,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/1901\/revisions"}],"predecessor-version":[{"id":1925,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/1901\/revisions\/1925"}],"wp:attachment":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=1901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=1901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=1901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}