yegor256 / qulice

Quality Police for Java projects: aggregator of Checkstyle and PMD
https://www.qulice.com
Other
299 stars 110 forks source link

Please explain why ++ and -- operators are prohibited #942

Open golszewski86 opened 5 years ago

golszewski86 commented 5 years ago

Please see qulice-checkstyle/src/main/resources/com/qulice/checkstyle/checks.xml

<module name="IllegalToken">
   <property name="tokens" value="POST_INC, POST_DEC"/>
</module>

It prohibits usage of the post increment and decrement operators(like idx++ or idx--), however there is no justification for it. I wasn't able to find one on the intenet except people saying it's more efficient in C/C++.

Could you please provide justification, and maybe include it in the checks.xml file?

0crat commented 5 years ago

@krzyk/z please, pay attention to this issue

0crat commented 5 years ago

@golszewski86/z this project will fix the problem faster if you donate a few dollars to it; just click here and pay via Stripe, it's very fast, convenient and appreciated; thanks a lot!

krzyk commented 5 years ago

@golszewski86 Yeah, you are right. And in most cases many devs hack it by using ++idx and --idx.

@yegor256 Can this rule go away? Or is there a justification for it?

llorllale commented 5 years ago

@krzyk a possible reason is to avoid this mistake:

    int x = 1;
    int y = x++;
    System.out.println(x);  // prints 2
    System.out.println(y);  // prints 1

Someone may mistakenly expect y == 2.

golszewski86 commented 5 years ago

I'm sorry @llorllale but this reason doesn't make sense to me. I understand some people may not know how do the post-increment and pre-increment work, but this is not the reason to disable these constructions for everyone.

yegor256 commented 5 years ago

@krzyk just use ++i and that's it. I don't see why the rule should go away.

krzyk commented 5 years ago

@yegor256 yeah, but both postfix and prefix increment /decrement in my opinion is equally bad. So why discourage just one? (or at all?). I think we should either prohibit both or none.

llorllale commented 5 years ago

@krzyk why do you think the preincrement is bad?

krzyk commented 5 years ago

@llorllale it 8s in the same group as postincrement, so if we discourage one, we should do the same with the other.

Personally I don't have anything against either one if the result of the expression is not used.

yegor256 commented 5 years ago

@krzyk I don't think that pre-increment is bad. Post-increment is bad for a very specific reason -- it returns the value before the increment, which makes code less readable. Pre-increment is side-effect free, so to speak. So, I vote for ++i but against i++.

golszewski86 commented 5 years ago

Post-increment is bad for a very specific reason -- it returns the value before the increment, which makes code less readable

That's just an opinion @yegor256, maybe it's less readable for you but not for everyone. I'd expect code quality rules to be based on generally acknowledged rules, not personal opinions about readability.

krzyk commented 5 years ago

@golszewski86 rules in qulice are generally decided by generally acknowledged rules but in some cases our PO (Yegor) decides about them in more personal way, look at http://yegor256.com/ for explanations why some of the rules are configured the way they are. You can see some good arguments against i++ from at least two people.

golszewski86 commented 5 years ago

@krzyk yep there are two arguments:

So there's actually only one argument, and it's not a good one.