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

242

u/halfanothersdozen May 12 '23

This code isn't "bad", it's just verbose. You're just mad your lead's a prick

119

u/Romejanic May 12 '23

Overly verbose code is still bad. This is a fairly simple function so it's not too bad, but as someone who has worked on corporate codebases that are ridiculously over-complicated, it's definitely a bad thing.

7

u/RolyPoly1320 May 12 '23

Everyone thinks verbose code is a bad things, until you have to go back to a 20 year old codebase to fix things. Then you have to pray the previous engineers thought about maintainability when patching it so you can place breakpoints and step through broken spots.

This could have been simplified by removing the else, it's not really needed since Contains returning true will cause the return true to execute anyway.

That said, lead is a prick, but never be too eager to make things one liners. You'll thank yourself later or a future engineer will thank you.

4

u/Romejanic May 13 '23

Yes I agree, overly compact code is definitely bad too if it’s hard to understand what it does. But being too verbose/overly descriptive when it could be condensed into less space where it’s more easily readable is also important

3

u/RolyPoly1320 May 13 '23

There's always a trade off though. Sometimes you really need to trap something in debugging and the one liner makes it harder.

Personally, I think both here were pricks. Lead shouldn't have dismissed the critique in the way they did, but junior should have approached the suggestion better.

Junior: "Hey, this method right here is overly verbose and is causing some performance issues. Maybe we should look into improving it."

Though if junior was already making changes and this was getting in the way, they should have fixed it and moved on from there. Make a note like, "Method was breaking code and needed fixed."

That's where I am right now. In my process of fixing other issues in a 20 year old codebase I found other breaking issues. Since they are breaking issues and they are in functionality that will be tested by QA I'm fixing them and moving on.

Better to make notes of it and get it fixed so QA has an easier time regression testing than to leave broken stuff in because someone else left it that way.

26

u/reverendsteveii May 12 '23

It's a bunch of extra reading and a bunch of extra code that could be wrong. It's bad. Doubly so because coming from a team lead this lack of understanding of junior level concepts doesn't bode well for the rest of the code base.

10

u/[deleted] May 12 '23

Plus it’s likely unnecessary overhead to call getErrorsList every time you want to check if a message is an error. And the else block isn’t helping anything. And the function could just return the method call’s response. Agreed, this is some intern level stuff

8

u/Smellypuce2 May 12 '23

I don't use Java but I would certainly hope its optimizer is at least good enough that both versions would have the same performance.

14

u/karisigurd4444 May 12 '23

Wrong. There's a time and place for verbosity. This is just garbage.

3

u/Leipzig101 May 12 '23

This code is bad. It makes the program slower. Function calls add branching, which stall the CPU pipeline. This is probably getting inlined, but cmon. This is just so stupid.

-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.

→ More replies (9)
→ More replies (4)

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.