r/badcode • • May 12 '23

java Code written by project lead. A comment was added by my team member that the entire method can be replaced with a single line: getErrorsList().contains(message). Project lead responded that we should focus on readability and performance and not on such code review comments.

Post image
796 Upvotes

152 comments sorted by

View all comments

Show parent comment

132

u/halfanothersdozen May 12 '23

Yes if you change code so it breaks then you can break the code. Very astute.

I agree it should be simplified but contriving examples of how to ruin a simple if statement is an extremely dumb point to make.

-43

u/Agilitis May 12 '23

I was proving the point that more code equals more ways to break something. If it is unnecessarily complex, you have more ways to break a simple code. If there is only one line of code, you only need to take care of that piece of code.

Also, I've seen many occasions where exactly this was the base for the errors that happened. Too bloated code, and someone changes parts of it.

24

u/halfanothersdozen May 12 '23

The code has literally the same complexity as the one-liner. I'm not positive but I suspect this compiles to the same java byte code. If you're worried that the devs around are going to break this you need to get better devs around you.

The lead here committed the sin of wasting everyone's time both in reading it and in causing a discussion both in his code review and now in a Reddit thread. The argument that the literal number of lines of code is the enemy is completely stupid and people need to chill out and look for actual bugs instead of made up contrived non-existent threats.

-14

u/Agilitis May 12 '23

I'm not positive but I suspect this compiles to the same java byte code.

Probably, but we are not discussing that, that's a different topic. Anyways I'm not arguing that this particular code will be a problem, but the pattern that it creates -> It is okay to create bloated functions.

All I'm saying is: more code equals more opportunity to break things. If a code can be simplified without hurting readability then it should be simplified because it creates less room for error. It is hard to argue on very few lines of code because on itself these small functions are not problems. If you have 10 thousand of lines of codes it becomes a bigger problem. I think we should always think about the patterns that we create because it might not pose a problem now, but it will down the road. I cannot explain this any better, if you only worked on small projects this does not apply.

10

u/halfanothersdozen May 12 '23

It is hard to argue on very few lines of code because on itself these small functions are not problems.

Yep.

So don't. Go argue about something important.

The mistake was writing code that leads to such distractions. The rest of this opining is just noise.

4

u/pakfur May 12 '23

You are both right. But, there is a 0% chance that there is a bug in code that isn’t written.

So, in general, write less code when you can. That is the point that u/agilitis was making.

2

u/halfanothersdozen May 12 '23

We're not both right. I said this code isn't bad, they said it is. Both can't be true. And I disagree with the premise that more lines, literally just by line count, is bad. The logic is the same. A one liner would be better, but this function itself is fine. The lead should have just made the suggested change and moved on and no drama would have followed.

5

u/Agilitis May 12 '23

With each response you give us you leave out important bits in our arguments to change it and because of that:

  1. You are arguing about something else
  2. You try to make me (us?) look like we do not understand the problem

You are right. Mroe code is not inherently worse. More code when you could have done it in less lines of code WITHOUT hurting readability is bad. This argument has multiple point and all of them needs to be included in the argument to make sense.

  1. More line of code
  2. Can be done in less line of code
  3. The shorter version is just as readable as the long version

If these are true then yes, the longer version is worse. Which is true for the example above, so this is a worse code than the shorter version.

4

u/halfanothersdozen May 12 '23

I understand you just fine. And yes there is a better version of the code. But I disagree with your argument, not your conclusion.

This is a simple "if" statement. There is no opportunity to make a mistake in reading and understanding this block of code. It can be made more concise but it cannot be made any more simple. We should be fighting against complexity and maintain a preference for brevity. Making something less complex generally reduces the possibility of error, however refactoring code to be more succinct often does introduce a mistake.

But most important in my mind is spending time quibbling over things like this distracts us from finding errors in the logic that don't matter to code readers but will matter to users. So yes this code can be better written, but not by much, and it is not something that truly violates the principal of "less is more" in code.

1

u/Agilitis May 12 '23

I feel like my point still did not come across, probably my poor writing skills. I am arguing that this kind of code is dangerous because it shows others a pattern which creates a bloated code base which will have problems.