Is less code cleaner?

Is less code cleaner?

In my eyes, clean code is simple and easy to understand code. Not over-designed, with as few template files as possible and clear semantics. So, does this mean that the less code, the cleaner it is?

[[133965]]

I don't think so. Most of the time, less code tends to be more semantically ambiguous and harder to understand (and therefore harder to maintain).

When I was working and testing meta filtering with jBehave, I wrote code similar to the following:

  1. public Embedder configuredEmbedder() {
  2. Embedder embedder = super .configuredEmbedder();
  3. ignoreStoriesAndScenariosWithMetaInformationParameter(embedder, "ignore" );
  4. return embedder;
  5. }
  6. private   void ignoreStoriesAndScenariosWithMetaInformationParameter(Embedder embedder, String ignoreParameter) {
  7. embedder.useMetaFilters(Arrays.asList( "-" + ignoreParameter));
  8. }
  9.  
  10. In a subsequent discussion of the code, one of my colleagues said that he had just deleted some "unnecessary" private methods, so the code became like this:
  1. @Override  
  2. public Embedder configuredEmbedder() {
  3. Embedder embedder = super .configuredEmbedder();
  4. embedder.useMetaFilters(Arrays.asList( "-ignore" ));
  5. return embedder;
  6. }

Obviously, the method is shorter and the code is less. For us, using such a class may allow us to see at a glance what changes have taken place in this method while we work. But what if there is a new person joining the project, and this guy has never used jBehave before? For him, a longer code can actually get more information, even if he doesn't know how jBehave works, what a "meta filter" is, and what minus means - but at least he can understand what we want to achieve.

When I tried to explain my opinion, other developers agreed with me but said that the same effect can be achieved by adding comments. Yes, I totally agree, adding comments definitely works. It's just a matter of style. I personally don't like comments, but in this case, maybe comments are a better choice because we can use them to explain the connection between the meta filter code and the jBehave layer file.

So, the code becomes this:

  1. @Override  
  2. public Embedder configuredEmbedder() {
  3. Embedder embedder = super .configuredEmbedder();
  4. // ignore stories and scenarios with meta information parameter @ignore.  
  5. embedder.useMetaFilters(Arrays.asList( "-ignore" ));
  6. return embedder;
  7. }

Of course you can say that such a small example is not worth mentioning. However, I think the style of a project is very important. You can also find a common style by discussing specific examples. Maybe other developers will consider whether his code will confuse new colleagues and add comments instead of shortening the method to one line of code.

in conclusion

Clean code doesn't always mean less code. So, you need to weigh the trade-offs between writing more small methods and reducing the number of lines of code. I will discuss coding style in another post later.

Which solution do you prefer and why? Please leave your comments.

<<:  Microsoft and Touch Technology invest heavily in Windows Game Contest

>>:  Moore: Moore's Law will be valid for another 10 years

Recommend

10 classic brand marketing in 2020

Younger, It is always the main theme of the brand...

10 Misconceptions About Marketing Promotion!

In 2020, risks and opportunities coexist. From th...

Detailed explanation of Tik Tok information flow delivery strategy!

Nowadays, more and more companies and projects ar...

How to Use Push Notifications in iOS 10

Introduction Although often overused, push notifi...

Can you learn Pinduoduo-style fission?

The concept of fission actually existed a long ti...

12 tips to thoroughly explain Douyin operation and promotion

In this article, the author will start with the r...

US mobile game market survey: middle-aged men are most willing to spend money

On October 17, Everyplay, a mobile game company u...

Is space junk a serious problem? What would happen if we left it alone?

We collect the garbage from our daily work and li...

Four key links in O2O operation

Although everyone has been playing with O2O in re...

In-depth analysis of Android mainstream application market promotion in 2019

As of Q2 2019, the domestic Android market share ...