10 ms·
What does code readability mean? (2018)
- eric4smith 5y agoIt means !Perl
- NegativeLatency 5y agoLike everything else there’s a tendency to lean towards categorizing it into one binary category or another, I think this article makes some great points however about the topic of simplicity I think rich hickeys talk about “simple made easy” is really informative for thinking about design and building systems (it also presents a interesting definition of the two categories) https://www.infoq.com/presentations/Simple-Made-Easy/ https://www.infoq.com/presentations/Simple-Made-Easy/
- vrnvu 5y agoThis is has been a popular topic for decades and I feel like most people get two fundamentals always twisted. Syntax and semantics. Syntax != Semantics. Code readability = syntax that make semantics obvious. If a block of code is written in an imperative style or functional style doesn't matter. Ask yourself instead, are the semantics clear? Are we coping or moving the values? Are we cloning the references? Are we iterating over mutable references or copies of the value? A good language, with good code readability, makes semantics obvious. A good programmer, encourages good readability. With good readability we don't have doubts about what a piece of code is doing.
- ge96 5y ago> imperative style or functional style doesn't matter. Semi-related, can you have something like Haskell that seems hard to "read" eg. () :: -> but written in something easier to read like C++. Idk maybe I'll get used to it like the transition from old JS functions to arrow funcs
- anko 5y agoThis is a really good comment, and I hadn't really thought of it in those terms. So it's insightful to me. It encapsulates a lot of things I hadn't really directly tried to answer before. My previous answer is that good code makes it easy to discover intent. But I think the semantics are important too - for example in scala we use monad transformers to treat futures (async code) much like lists. But the problem with this approach is it's not clear without inspecting the type signature if you're doing something that has a high algorithmic complexity eg. spawning threads or not. I think the best language i've ever read for semantics is elixir. But i think the lack of static typing makes it harder to code review, especially on github. Maybe Gleam is the answer? What language do you like?
- 29athrowaway 5y agoImagine I tell you that story, but before that let me tell you something else. Nevermind, let's go on with the story. One moment, what was I saying? Oh sure, yes, the story. The story goes as follows... The ending, preceded by most of the story, preceded by its beginning. Some people write code that reads like that. Readability counts.
- narag 5y agoI recommend Code Complete for characteristics that make code objectively better and more readable. This article seems to make the case that it's all about taste or lack of familiarity. I'm not convinced.
- 0xCAP 5y agoFor me, readable code is smart code wrote in a way that makes it readable by dummies. I've seen both 1000locXfile imperative spaghetti messes and super modular dogmatic Clean Code/Hexagonal Architecture/DDD stuff, and I can tell you that both can be nightmare inducing. I'm no big brainer, but I've always been praised for being the guy who produces legible code which other devs, even juniors, don't fear messing with. How? I simply don't push my code to extremes like it's gonna prove I'm smarter than I need to be for my job. And I model pretty damn intricated logic for a job.
- croo 5y agoThe article is missing a very important part of the topic what I call "reasoning". Clean code is easier to reason about. The easier to reason about the code the cleaner it is and the less context one need to understand the problem the better. State is the real enemy - field variables, mutable objects, lots of incoming parameters, context holding objects all makes it incredibly hard to understand the edge cases and general mechanism of the code. Thankfully goto statements, global variables and singletons already has stigma attached to them...
- es7 5y agoOn the other hand, singletons and global variables can be incredibly useful for writing clear and simple code. As a web and mobile app developer, I find that globals can be useful. Developers will go through extraordinary lengths to avoid making things global, but the truth is that for end-user applications a great deal of the relevant product requirements are essentially singletons. There’s only one active profile and one user and one catalog and one persistence layer. There’s only one DOM and one window. The result is that code which endeavors to make state which is truly shared and global actually shared and global will often be much simpler and less bug-prone, provided that reasonable abstractions are chosen for any mutation and event APIs related to global state.
- jupp0r 5y agoThere’s only one DOM and one window. And then you start using web workers, service workers, audio weblets, … Or you want to write tests for your code, or you want to use the same code in a backend node service or with react-native.
- chriswarbo 5y ago> Developers will go through extraordinary lengths to avoid making things global, but the truth is that for end-user applications a great deal of the relevant product requirements are essentially singletons. Globals are appropriate when "there can be only one"; however, I tend to find that's rarely the case. Much more common is "there can be only one at a time"; in which case, dynamic variables are far more useful than global variables. The most obvious example is in tests.
- wruza 5y agoNot going to criticize the article much, because the author is right - sometimes I just don’t like the code/style, so it’s just harder to relate to, not that it is actually hard to understand. But there is a single “case” I’d never agree on: the code whose complexity and/or composition ends with nothing. The more experience you get, the more high-level structure you are able to pick on, but sometimes structures are just there, serving nothing at all. Overzealous (or compelled) decomposition into 5-line chunks over tens of files. Pointless renaming, re-exporting and encapsulation. Higher-order code golf. All this done to a finished non-extensible project makes no sense and makes it harder to read, for no reason. This is a lenghty article, but it is readable in one chunk, a good piece of read. Now imagine the author split it into a number of submodules, then in each they’d give a new name to every phenomenon. Then instead of using English they’d construct a new sub-language (also in modules) to express meaning in a shorter way. E.g. this: I can’t read the code because I don’t have sufficient experience or expertise (with the language or domain). I haven’t spent enough time trying to read and understand the code (“it’s not obvious” or “it’s not intuitive”). I don’t have much interest in understanding this code, I prefer to rewrite it in my own style. … turns into this: With code as object, let me as you negating: frobnicate causality of read object over sufficient experience or expertise (with the language or domain). frobnicate relation of enough time and attempt to read and understand object (“it’s obvious” or “it’s intuitive”). have much interest in understanding object, no prefer to rewrite it in my own style. // “no” negates negation include more items from [here|https://…]. See frobnicate in my frobnication article.
- xnorswap 5y agoThis is why I hate plug-ins that claim to produce readability or code-cleanliness metrics, because they very typically have the opposite effect. A 1000 line function broken down into 100 different 10 line functions which are then each called in turn (and need lots of parameters to pass around the inevitable shared state) is actually often less readable than just a 1000 line function. The way to deal with such a function to ask why there needed to be a 1000 line function. Why does so much apparently need to be done here and now and so much state shared across so many lines. Code metrics tools would rather you break everything down into micro-functions which if you're blindly trying to follow the tooling recommendations to get it past a check-in gate will probably end up badly named and with little thinking into actual abstraction. If you have two functions but whenever you call function foo = Bar() you have to then call function Baz(foo) then you don't really have two functions at all.
- progx 5y agoThe important thing is "why?". Every code is readable, more or less, but to understand why the code exists is the important thing.
- kaon123 5y agoThe article needs more quotes.
- lucideer 5y ago> When we say that some code “is unreadable” we actually mean one or more of: > 1. I can’t read the code because I don’t have sufficient experience or expertise (with the language or domain). > 2. I haven’t spent enough time trying to read and understand the code (“it’s not obvious” or “it’s not intuitive”). > 3. I don’t have much interest in understanding this code, I prefer to rewrite it in my own style. > 4. The code offends my sense of aesthetics; I would write it differently. > 5. The original programmer didn’t know how to write code. > 6. The code appears to violate some principles or patterns I believe in. The rest of the article mainly deals with 1-4 here, and only selectively tackles 6 as a "performative" repeat of 4. It does mention vaguely toward the end that there may be some objective sense of readability, but doesn't go into any technical analysis. Which is a shame as I don't think it's that complex. A few sibling commenters have alluded to this using various terms: ultimately I think objective code readability is about context and locality. Consider a static analysis security tool that produces a control flow analysis tree: often a long tree is a very good sign of bad readability (in practice a long tree can be caused by many things: excessive boilerplate, frequent variable mutation or reassignment, etc.) These same tools often fall down analysing control flow of applications with weird state patterns: global state, "magic" model loading, etc. Things that further hamper a reader's ability to hold a complete picture of any given file they're reading in their head at once. I think this is interesting because not only is it a fairly simple idea to reason about (context & locality) but it's even potentially automatable as a metric.
- WillAdams 5y agoAren't 1--4 and 6 addressed by the concept of "Literate Programming"? http://literateprogramming.com http://literateprogramming.com (at least the programmer would explain the choices made) and I've found that the need to explain the code in a literate mode has resulted in my better understanding the problem and possible approaches, resulting in a successful coding solution.
- math-dev 5y agoMy suggestion is to find some programmers in your selected language who you look up to and learn from them how best to write code in that language I may be generalising but I find older (40+) programmers more likely to write good code - they have all the war zone experience and battle scars For example, in Common Lisp I am a big fan of https://github.com/informatimago https://github.com/informatimago and also Allegro Common Lisp’s open source code I must admit, the _art_ of writing readable code really appeals to me and is one of my finest joys in programming
- yodsanklai 5y ago> I may be generalising but I find older (40+) programmers more likely to write good code I think it's also because they don't deal with complexity as well as younger programmers so they strive for simplicity (which is good). > I must admit, the _art_ of writing readable code really appeals to me and is one of my finest joys in programming Totally agree. Unfortunately, this isn't rewarded as much in a professional settings were you're expected to write features. Refactoring is much less visible, unless you're refactoring a piece of code which everyone was struggling with.
- andi999 5y agoDo you mean that in the sense that they have the experience, that unnecessary complexity will later bite you?
- fatnoah 5y ago>I think it's also because they don't deal with complexity as well as younger programmers so they strive for simplicity (which is good). Yeah, I'm going to have to ask for details here. When I was a youngster, it was the older folks who were my guides through the forests of complexity, especially when it came to interconnections between things, unintended side effects, and far-ranging consequences of design choices. When I got to that ripe old state of being 40+, that was my role as well.
- faho 5y ago>I may be generalising but I find older (40+) programmers more likely to write good code [...] >For example, in Common Lisp Technology choice can correlate with age. In Lisp's case I would expect that it's long past "cool", i.e. that it's attracting fewer people than it used to, and so I would expect it to skew older. Just like perl and tcl and awk. Also you would have to take survivor bias into account - if you only see good lisp projects, maybe that's because the bad lisp projects died out? Maybe the bad old lisp programmers left?
- es7 5y agoBased on the way the article is written, I wouldn’t trust the author to write readable code either. It seems unfocused and lacks a coherent point… or did I just fail to understand it? Readability is a very real concern and something every programmer should think about. It’s a form of communication. It all starts with naming. Naming the entities, classes, methods and variables is probably 70% of making really readable code. In my experience, the difference between ‘spaghetti mess’ and ‘nice, clean code’ can often be achieved with an identical AST structure but with well-thought-out and consistently applied names. The other 30% probably deserves it’s own book. It involves breaking complex portions of code up into well-named segments (ex: functions or variables), keeping related segments near each other (not spreading functionality out over dozens of files), using consistent patterns and much more. There is a such thing as unreadable code. The author here has some nuggets of wisdom buried inside a lot of chaos. Maybe his style serves a good purpose for prose, but it isn’t an example of efficient and clear communication. Readable code should be efficient and clear communication.
- lexicality 5y ago> Does that mean all programmers should dumb their code down so even beginners with no domain expertise can understand it at a glance? Should we strive to satisfy the Shakespeare for Dummies demographic? Having been on call for other people's code I can tell you that the idiom "Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live" is a very good idea and you should stick to it. If I get woken up at 2am due to an outage you caused and open your file to find you've written War and Peace code instead of The Hungry Caterpillar code I will be very very very upset with you.
- ycuser2 5y agoThe most readable code is mine.
- ki_ 5y agowhat makes code totally unreadable for me, is when people think adding more smaller classes and more smaller functions is actually simplifying the code. NO! NO! NO! You are splitting up the code and spreading it around so i cant see the whole picture and make it very difficult for me to track which function calls what function. After 20 goto definitions... i mean. common. abstracting your code is making it more complex. or. maybe im just working with terrible developers that dont know how to abstract in a simple way. Anyway, i dont like it. Procedural programming is best in my opinion. And if you are going to abstract, dont use too many layers. e.g. A calls B calls C and D, and D calls also C and C sometimes calls E and E calls B. etc... TOTAL NIGHTMARE.
- dstick 5y agoYeah I never got that whole "a function can never be longer than X lines". Screw that. Put everything that's relevant in that context in the function and only start breaking things up once it has to be re-used. Which happens a lot less often than many books and guides make out to be.
- toto444 5y agoI once read a comment or post on HN that was saying something around the line : 'good code is code that you can throw away easily'. A long function that can be broken down easily meets that criterion.
- Akronymus 5y agoAlso, internal functions/lambdas first before exposing it externally makes the most sense in many cases. Reduces namespace pollution and makes it easier to rewrite those inner ones.
- xupybd 5y agoThis depends. If you don't mix levels of abstraction you can use your functions to build something close to a DSL. I would have agreed with you when I was 5 years in but now I like small functions and lots of the. Each function have one narrow easy to test task.
- 5y ago
- kristov 5y agoThere is a definition of "unreadable" missing from the bullet point list at the top. This is code that is known to be correctly functioning, but the implementation of that functionality is objectively obscure (this is not equivilant to the second bullet point). In my experience you see it most in code that is old and has had many programmers work on it. For example, your reading a function trying to figure out how it could ever return a correct value - and yet good tests show it does every time. You realize 100 lines can be replaced by a hash lookup such-n-such, so you make the change expecting the tests to fail because you didn't understand something, but nope, you were right - simple lookup was all it needed. How does code like this come to be? It grows organically. The latest minor tweak is a tiny blip in the long twisted history of the functions existence. This might seem like a minor addition to the list, but I think it's an important one. And it has a deeper philosophical implication: if you end up with a "Rube Goldberg machine" chunk of code it is not simply stylistic preference, even if the code functions correctly. In other words: you should not make the claim that just because it functions correctly that it is "correctly written". You certainly can make that claim (and I have seen many do, and I have done so myself). There is code that is complex because it needs to be. There is code that is complex even though it doesn't need to be. If you don't actively prune the second type, it will grow all by itself and take over your garden.
- troelsSteegin 5y agoIs code reading a use case for GPT-2 text summarization? The examples I see are text to text or code to code. I am wondering what code to text could be like.
- yanis_t 5y ago
- GnarfGnarf 5y agoYes, Macbeth has become unreadable. English has evolved. Shakespeare (whoever he was) was a great author, but he spoke a different language.
- photochemsyn 5y agoTopological maps of a code base sound like a good way to explain what's going on in the code in a visual manner.