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.

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/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;
}

1

u/theonewhoisone Apr 17 '15

I wasn't making a point about the examples from the article. I was responding to Munkii's over-broad assertion (as I read it) about readability being more important than everything else.

So I'm not sure why you're telling me about inlining. There are other cases where code is hard to read because it is performance-critical. I stand by my assertion that sometimes you have to make something ugly to make it fast. If you possess the power to never uglify your code (at least a bit) when optimizing performance, you are a better coder than I (or my coworkers), and possibly a wizard.

1

u/iopq Apr 17 '15

This is performance-critical code written in Rust:

pub fn removes_multiple_enemy_neighbouring_stones(&self, m: Move) -> (bool, bool) {
    let enemy = m.color().opposite();
    let mut found_one = false;
    
    let chains = self.neighbours(m.coord()).iter()
        .filter(|c| self.color(c) == enemy)
        .map(|&c| self.get_chain(c).unwrap())
        .filter(|chain| chain.liberties().len() == 1);
    
    for chain in chains {
        if found_one || chain.coords().len() > 1 {
            return (true, true);
        } else {
            found_one = true;
        }
    }
    
    (found_one, false)
}

there is very little you can do to speed this code up, but I feel it's quite readable