23

Language shortcuts can often be used to make code more concise.

For example, ternary and null coalescing operators can reduce the amount of code, but arguably to the detriment of readability:

In C#:

Person newGuy = new Person();
if (boss == null) {
    newGuy.Boss = GetDefaultBoss();
} else {
    newGuy.Boss = boss;
}

is functionally equivalent to:

Person newGuy = new Person();
newGuy.Boss = boss ?? GetDefaultBoss();

but obviously a lot more verbose.

Where do you draw the line when it comes to conciseness vs readability?

6
  • 3
    This is a subjective question, someone who implemented something would say his way makes sense, someone reading it would say it makes no sense and another person reading it would say I get it but I prefer another way. This is merely preference.
    – Chris
    Sep 6, 2010 at 17:38
  • 23
    I think the second version is more readable.
    – Vaibhav
    Sep 21, 2010 at 14:57
  • 2
    You are confusing conciseness with terseness. Conciseness improves readability. Terseness detracts from it.
    – Ferruccio
    Nov 15, 2011 at 13:56
  • 1
    @ThorbjørnRavnAndersen: The audience for C# code is usually "developers with a C# background". From what I've seen, the consensus here on programmers.se seems to be that you should not avoid using useful language features just because someone might not be familiar with them. (See also: programmers.stackexchange.com/q/101513/33843.)
    – Heinzi
    Nov 15, 2011 at 14:06
  • 1
    duplicate?: programmers.stackexchange.com/q/58630/15464 .. Oh, this one is older. :) Nov 15, 2011 at 14:14

8 Answers 8

66

Both.

Your first example is certainly more verbose, and arguably more explicit... but it also requires me to scan five lines instead of one. Worse, it deemphasizes its purpose - assigning a value to newGuy.Boss.

Your second example may cost me a second if I'm unfamiliar with the null coalescing operator, but there can be no doubt as to its purpose, and if I'm scanning through a larger routine looking for the source of a value, it will be much easier for me to pick this one out.

Now, contrast this:

if (boss == null) {
    newGuy.Boss = GetDefaultBoss();
    newGuy.IsTemp = true;
    newGuy.AddTask("orientation");
} else {
    newGuy.Boss = boss;
    newGuy.IsTemp = false;
}

...with:

newGuy.Boss = boss ?? GetDefaultBoss();
newGuy.IsTemp = boss == null;
if ( boss == null ) newGuy.AddTask("orientation");

The latter example is again much shorter, but now it obscures its purpose by making tasks triggered by the same test appear to be distinct. Here, I feel the verbosity of the former is justified.

8
  • 4
    Excellent answer! - It's not about verbosity as much as it's about the purpose.
    – Damovisa
    Sep 6, 2010 at 5:39
  • 2
    @Damovisa: exactly - the goal of code is communication, and there's no general reason why this shouldn't be done as concisely as possible (but no more).
    – Shog9
    Sep 6, 2010 at 6:07
  • 1
    Learning the null coalescing operator will take more than a second - but if you don't know it, then you should learn it!
    – Casebash
    Sep 7, 2010 at 0:35
  • 2
    Ah "??" would be nice in Java.
    – user1249
    Oct 17, 2010 at 10:02
  • Even though the last example is shorter in terms of code, it is actually evaluating boss 3 times as opposed to just once in the verbose example. This is just another reason why shorter code just for sake of being shorter is a bad idea. I think conciseness should be second to the primary objective, whether it is readability/maintainability (for complex code) or performance (for critical sections of inner loops, etc.). In other words, never optimize purely for conciseness -- unless you plan to send your code over a 1bps connection ;)
    – user193130
    Mar 5, 2014 at 23:24
17

While both are good goals, I'll always side with Readability when forced to choose one.

I would argue that your example improves both readability and brevity. Consider, however:

if( a > b )
{
    foo = bar
}
else
{
    if( c.isThing() ){
        foo = somethingElse;
    }
    else{
        foo = someFurtherThing.GetFoo();
    }
}

as opposed to

foo = a > b ? bar ?? whatever.something : c.isThing() ? somethingElse : someFurtherThing.GetFoo();

The latter is concise, but is difficult to read through. The former is verbose, but the flow of logic is clear.

Ultimately, brevity doesn't serve much of a purpose, other than the ability to fit more on the screen. Readability makes it easier to debug, and so should generally be preferred.

5
  • 12
    Proper formatting could easily improve readability of the second example. This is an unfair comparison. When formatted like this, I'm not at all sure it is that bad. Nov 15, 2011 at 14:17
  • 1
    The code samples aren't equivalent: The first one is missing ?? whatever.something . Oct 20, 2017 at 19:20
  • @StevenJeuris It's still bad, since a rather novel structure like that would have to be cognitively parsed, rather than visually scanned. (I could think of one or two other ways to format the code you linked to). I think it's better to leverage people's intuition based on reaching for generalised familiarity.
    – Magne
    Jan 20, 2021 at 14:20
  • @Magne Rather novel? These, or similar, operators are now part of pretty much any mainstream language. And the point you make the code can be formatted differently is moot; of course, the same point can be made about good old braces, and has been made since the start of software development. Jan 21, 2021 at 15:08
  • @StevenJeuris Yes. The operators are mainstream. But the way you nested them was rather novel. I've programmed for over 10 years and haven' seen ternary operators nested precisely like that. It took me a while to parse. Esp. that the single ? was on the line below the boolean expression in question. The good old formatting using braces keep code more distinct/separated, and thus leaves less room for various interpretations. I suggest trying the various approaches on a junior developer and see which style of formatting would take him/her longer to understand.
    – Magne
    Jan 25, 2021 at 15:26
11

I would say as a general rule never sacrifice readability because of conciseness, but never judge readability based on another programmers lack of knowledge on that subject.

Conciseness and readability are not opposites. Like this answer, sometimes shorter is more readable.

1
  • 5
    +1 for not assuming another programmer's lack of knowledge. In the example given, the second option is only less readable if you aren't familiar with the null coalescing operator. I would never write my code based on the assumption that my co-workers don't know the language syntax. Even if they didn't know what ?? means, if I use it and then they learn it, we've both benefited. And it's not like it's hard to type in "?? operator" on msdn.com Sep 6, 2010 at 13:55
4

I would say I prefer readability, though that sometimes means using concise code. (I.e. ternary for relatively simple conditionals inside a larger conditional block.)

Basically, if it is unnecessarily difficult to understand, don't do it.

3

Readability comes first where it conflicts with conciseness, because code is modified more often than it is initially written. On the other hand:

  1. Syntactic noise and boilerplate code often obscure intentions and thus hurt readability. Sometimes the more concise code is also more readable. For example, think lambda functions or delegates/first-class functions versus single-method classes that implement a single-method interface.

  2. Readability should be assessed based on how easy the code is to read for a reasonably experienced programmer who knows the language and its unique/advanced features fairly well, not some barely competent code monkey who only knows the lowest common denominator.

2

One aspect that I don't think has been mentioned yet: what are your goals?

If all you care about is job security, go for conciseness & compactness over everything else. Skip commenting your code, too.

If you want to be able to easily hand your code off to someone else while you go work on a cool new project, go for readability, clarity, and lots of solid comments.

Note: the above isn't about you personally, @Damovisa; it's for anyone choosing between the two positions.

1
  • That sounds like a job security but if you want to be a programmer, it's a job security at the wrong company... ;) Sep 30, 2015 at 1:47
2

There is one thing that the verbose version has as an advantage.

It has more lines, and most debuggers are line-oriented! It is very hard to set a break point in the middle of an expression, but it is usually trivially simple to set it inside a block statement.

In other words, which one would you want to see in your editor if you want your debugger to kick in when boss == null ?

(That said I like the ??-operator)

0
0

Readability should come first, long-term most people spend most time modifying or extending existing code - readability is a big part of maintainability.

That said, conciseness is something which can contribute towards readability. For example, in your question the second snippet is both more readable and more concise.

Not the answer you're looking for? Browse other questions tagged or ask your own question.