문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

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.

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.

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.

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.

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)

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 softwareengineering.stackexchange
scroll top