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

-78

u/Agilitis May 12 '23

The best code is the one you didn’t write. This code can be broken in multiple ways. So yeah, it is bad code.

67

u/halfanothersdozen May 12 '23

What? There's nothing wrong with it. It's not broken. There's probably negligible overhead from the variable assignment. It's just literally more characters than it needs to be. How can it be "broken"?

-104

u/Agilitis May 12 '23

I did not say it IS broken. I said it CAN BE broken easily. Put a `!` mark in front of the if and it is broken. Switch the return statements and it is broken. Change the type of the variable that you created and it might not even support contains.

These examples might sound strange but these happen from time to time. If you can write this code simpler, you need to do it simpler. Not everything needs to be dumbed down, but this is not more readable than the one liner.

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.

→ More replies
→ More replies (1)

18

u/WesternAffectionate1 May 12 '23

Dude. Whoa. Now I truly realize what I missed out on by not getting a CS degree before starting my first job as a developer. Holy shit. You see, sometimes I like to stick random ‘!’ characters in my code for emphasis… I mean, I know it inverts the logic, but it just always felt more important that certain lines of code really have an impact, you know?

But damn, man… you see, my company writes software for designing fire sprinkler systems. One day I noticed the following assignment:

fire_detected_on_floor=True

Naturally, I changed it to:

fire_detected_on_floor!=True

…cos I mean, duh, there’s a fucking fire bro! If only, just once, someone — ANYONE — had informed me that breaking my code would… break my code… well, let’s just say that the burn unit would have had a much easier day when that fire started in the Children’s hospital.

Those poor kids… aw well, live and learn I guess. 🤷‍♂️

6

u/OpenSourcePenguin May 12 '23

By that logic any code can be easily broken.

In just de-reference a null pointer, or even better add a character and cause a syntax error.

Changing the code changes it, who would have thought.

0

u/PsylentKnight May 13 '23

By that logic any code can be easily broken.

That's the point though. The more code there is, the more code there is to break. This guy is getting dog-piled but I think he's right. This example is trivial, but in general, being too verbose is bad.

12

u/lazyzefiris May 12 '23

This code can also be modified in one place and you would not have to change every getErrorsList().contains(message) across whole codebase. You can also easily plug some additional logic, elther for debugging or special case treatment (like fitering the list) in this. Which might have already been there at some stage and then removed. This kind of verbose code is not an example of bad code in my book. The intent is clear, the implementation is straightforward.

4

u/Agilitis May 12 '23

I did not say the the function should be replaced by the line above everywhere. That's a different topic.

4

u/Eggimix May 12 '23

Also where did you get this statement this is not true lol

3

u/Agilitis May 12 '23

No exact origin, but the saying is very common among developers. I think it is pretty straightforward. Code you don't write can't have: bugs, readability issues, bad tests, etc. So yeah I still think it is true.

→ More replies (3)

2

u/leftofzen May 12 '23

Tell us you learnt programming from YouTube without telling us

0

u/Eggimix May 13 '23

? I went to college and I've worked for faang fuckin tech bros

6

u/leftofzen May 12 '23

It's kind of sad so many people have downvoted this, because you're absolutely right. The less code you write, the less bugs you can have, purely from a statistical point of view.

I'm a senior dev and I get the most kicks out of deleting code and refactoring existing code to be more concise, without sacrificing readability of course. I guess this sub is on board with Elon and judging quality by lines of code written

3

u/TheDiplocrap May 12 '23

I think the downvotes are objecting to the idea that it's broken in multiple ways, especially with the defense of that comment down thread.

I do think that more concise code means less opportunity to break it in the future, which is what I assume /u/Agilitis was trying to say. There's less surface area for things to accidentally go wrong.

You have to strike a balance between compact-ness and readability, though.

2

u/Agilitis May 12 '23

Absolutely, this is exactly what I was trying to say, but I believe this is what I wrote. That the code *can be* broken. I was just using the code above as an example, I would probably object if I saw this on an MR, that this can be simplified without losing clarity so it should be simplified. If we are creating 4x the code than needed, then on the long run it means hundreds of lines of code.

2

u/Agilitis May 12 '23

I'm gonna be honest. I believe many of these subs are actually full of juniors or students. I think very small percentage of people on this sub or any other programming related are experienced developer. Even experienced developer sub is full of mid level engineers.

2

u/ings0c May 12 '23 edited May 12 '23

The less code you write, the less bugs you can have, purely from a statistical point of view.

No, the two are syntactically different, but functionally the same.

The more operations your code performs, the more bugs are possible, but I’m no less likely to introduce a bug if my language keyword is the 4 letter func versus function, and in the same way, the code OP posted is no more likely to have bugs than the more concise alternative suggested.

That said, I would still pull it up in a review because it looks like code someone unfamiliar with the language would write. It’s stylistically problematic, but not otherwise.