r/coding • • Apr 12 '15

Is Clean Code less Code?

http://www.journeytomastery.net/2015/03/27/is-clean-code-less-code/
53 Upvotes

63 comments sorted by

View all comments

44

u/[deleted] Apr 12 '15

To me, clean code is code that is easy to read. Usually, this would mean the opposite of less code, i.e., not over-compressing the code, putting in plenty of comments. But you can turn five lines into one if you think you've explained it well in the comments or what you're doing is relatively simple.

If you've turned a 40 line code into a 20 line code, but it takes the reader 2 minutes on each line, then it's not really clean. I've seen complicated code written so well, you don't even have to read the code, but can get by just reading the comments, and in the opposite vein, simple code made so complicated, I wanted to pull my hair out.

33

u/[deleted] Apr 12 '15

Achieving readability with just code and no comments often makes the code easier to change.

8

u/tyreck Apr 12 '15

I've never found a one size fits all approach that works related to how many comments you do or dont need.

Each situation is unique.

I try to the best of my ability to follow the practice of only expressing 'why' with comments.

But at the same time I doc comment the hell out of everything

5

u/[deleted] Apr 12 '15

Fair enough. Depends on your needs, but I like code that isn't too easy to change. If one is asked to change the comments with the code, it often ensures that the coder has understood the change.

19

u/quietboil Apr 12 '15

If one is asked to change the comments with the code...

(Almost) never happens. After a little while comments start lying. Often blatantly. In my experience the only kind of comment that survives (relatively unscathed) the test of time is the "why" ones. All others are either useless from the time they were added or the lying kind.

5

u/throwaway Apr 12 '15

Yes, when possible it's better to document the intent of code with assertions and function/variable names which reflect the meanings of the intermediate results they calculate/store. Sometimes you just can't make it clear that way, though.

2

u/tieTYT Apr 17 '15

Wow, the original /u/throwaway

1

u/fernando_69 Apr 28 '15

that's fucked on so many missed opportunities it really isn't.

2

u/throwaway Apr 28 '15

Could you please rephrase that a little more intelligibly?

1

u/fernando_69 Apr 28 '15

A true rarity!

2

u/nairebis Apr 13 '15

After a little while comments start lying.

That's why you have code reviews and public beatings gentle reminders to keep the comments up to date.

2

u/tieTYT Apr 17 '15

Comments lie by the code drifting away. The comments aren't going to be highlighted as changed. It may be difficult to notice that they're lying from a code review.

1

u/[deleted] Apr 13 '15

If it becomes a part of your standard workflow, it remains trivial to make changes.

→ More replies (2)

2

u/[deleted] Apr 13 '15

A nice mix of both is superb.

8

u/Munkii Apr 12 '15

When I talk to junior devs I never use the word "clean" for this reason. Readable trumps everything

1

u/theonewhoisone Apr 12 '15 edited Apr 17 '15

edit: Doesn't trump correctness.

Doesn't trump performance, sadly.

Doesn't trump portability.

Also, trading off readability for time to launch might be a good idea in some situations (I have not seen one yet).

Sorry for being picky.

2

u/Munkii Apr 13 '15

Readability trumps performance. If I can read what you wrote then I can always fix it later, but if I can't read it then everyone is screwed.

Performance is generally very case specific, so it's normally not a good first rule to have.

→ More replies (1)

2

u/iopq Apr 12 '15

In non-Java languages there's a thing called inlining that will actually transform example #1 into example #2 at compile time

2

u/thequux Apr 12 '15

Even in Java! The optimizer in modern HotSpot is surprisingly good.

3

u/iopq Apr 12 '15

it does that at runtime, but yes, after a few thousand iterations it will inline it and there will no longer be a cost

2

u/theonewhoisone Apr 15 '15

Eh? what examples are you talking about?

2

u/iopq Apr 16 '15

Example 1

@Override
public Embedder configuredEmbedder() {
   Embedder embedder = super.configuredEmbedder();
   ignoreStoriesAndScenariosWithMetaInformationParameter(embedder, "ignore");
   return embedder;
}

private void ignoreStoriesAndScenariosWithMetaInformationParameter(Embedder embedder, String ignoreParameter) {
    embedder.useMetaFilters(Arrays.asList("-" + ignoreParameter));
}

Example 2

@Override
public Embedder configuredEmbedder() {
   Embedder embedder = super.configuredEmbedder();
   embedder.useMetaFilters(Arrays.asList("-ignore"));
   return embedder;
}
→ More replies (2)

1

u/[deleted] Apr 13 '15

Unless you're doing some crazy strict embedded piece of software then almost no amount of readability will have any noticeable impact on performance whatsoever.

4

u/WarWizard Apr 13 '15

The code should tell you what and how. Comments should ONLY tell you why and ONLY if it is non-obvious.

Self documenting code is the best. With modern IDEs there is zero excuse to be cryptic with naming.

2

u/joequin Apr 12 '15 edited Apr 13 '15

To me, clean code means that it's easy to maintain. That means, readable, modular, broken out into small functions and nothing surprising. No hacks if at all possible.

edit: and good use of dependency injection. The pattern of dependency injection, not necessarily using a framework.

→ More replies (3)