r/badcode • u/sausage-x-donut • 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.
796
Upvotes
42
u/[deleted] May 12 '23
That does not even compile so it’s bullshit.
8
u/esmagik May 12 '23
What I’d recommend next time is talking to them before you publicly call them out. Regardless if even, that’s exactly what a code review is for. You know them better than us and it sounds like they’re lacking confidence.
You could also pull that branch, update it yourself, or (if using GH) recommend the change from the UI.
Edit: I’m a Principal Software Engineer with a fortune 200 in the states.
145
u/framsanon May 12 '23
Could be worse.
if (errorsList.contains(message) == true) …
202
u/GergiH May 12 '23
I’ve only seen two sides, either nobody forces these kind of people so they just ignore cr comments and then everyone else does too, or there are rigid guidelines everyone follows and if you repeatedly miss some parts of it or the lead dev doesn’t agree with your logic you might get fired. I think I hate both.
56
u/the96jesterrace May 12 '23
Well I believe that you can argue that for specific purposes it’s more readable to wrap single line statements into a method so that you rather want to write
isError(someString);thanerrorList.Contains(someString);But I can’t think of a reason why you would want to write
instead of
return condition;36
u/slaymaker1907 May 12 '23
There is a small advantage here is that you can add an (efficient) debug point here when for a particular result. Conditional breakpoints are a thing, but they are often extremely slow because they first stop executing the code and then seeing if the condition is satisfied.
30
u/WasserTyp69 May 12 '23
getErrorsList().contains(message)Personally not a fan of that either, I'd only replace the if-else return by a
return errorsList.contains(message). Or at least pull thereturn falseout of theelsestatement. Really doesn't matter though.242
u/halfanothersdozen May 12 '23
This code isn't "bad", it's just verbose. You're just mad your lead's a prick
17
u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 12 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
19
u/DJPokis May 12 '23
I think people need to get some CR trainings. If you get a comment in CR and it's not controversial or logic breaking = you just apply it. Single line in this instance imho is way better and more readable also. This does not make it neither more performant, nor more readable, this actually increases cyclomatic complexity which some of the code quality tools can even give out as a warning or error depending on the configuration. (ex. SonarQube configuration on good code quality)
4
u/PerformerNo9031 May 12 '23
I'd be curious to see how this is used, is message some sort of string constant, or "magic string".
It may seem a bit silly, but maybe it's planned in the future to expand this method and do some additional stuff within? But then it should be commented.
4
u/AmyMialee May 12 '23
Couldn't it be a set?
And even then why do you have a list of strings to know if something is an error.
15
u/SubhumanOxford May 12 '23
getErrorsList().contains(message)is much more readable than whatever this shit is3
u/probaddie42 May 12 '23
Forget the needless
booleanliteralreturns. Why in the world are they doing aStringequality check here? Just a misplaced punctuation mark or capitalization difference between the list contents and the message passed in breaks this.This is begging for a
Messageclass/interface, where you just set the type (error, etc.) at construction time (which you must know if you have that list already).3
u/AHeroicLlama May 12 '23
Agree with project lead, we should focus on readability. So make the change!
8
u/zmitic May 12 '23
There is also one thing here that really drives me crazy:
elseafterreturn. Ugh...But yeah, your lead is garbage. With response he made, you can be sure then when something goes wrong, and it will, he will put the blame on all of you.
2
u/chaws314 May 12 '23
How about "you are missing an `=` on the 2nd line? :P
2
u/[deleted] May 12 '23
This is not bad code. This is code that is easily debugged and/or changed. The only thing that needs to be removed is the else. Just need the return.
2
u/jangohutch May 13 '23
its likely the compiler will optimize this anyways
2
u/[deleted] May 13 '23
Sounds like everyone got a healthy dose of bikeshedding today!
I’ve worked with lots of people, and unless you’ve got the stamina or will to catch every little dumb thing then stick to finding bugs and offer helpful tips that actually save time. While annoying, it’s just petty at the end of the day.
3
u/WhatIsThisSevenNow May 12 '23
So, overly verbose, fully functional code is "bad" now? WTF?!
3
u/[deleted] May 12 '23
So that is crap code, but having an "isError" function is still beneficial because you're making a contract, 'this function can tell if a string is an error' and the implementation details can be changed, updated for additional cases or criteria, and unit tested as a function. If you just put the 'errorsList.contains()' in many places where you might have called isError() instead, then if down the road 'how do we detect if a string is an error' needs to change, that means finding all the direct and obsolete usages and replacing them.
It's crap code because you should just 'return errorsList.contains(message)' and get rid of the if-else.
2
u/cheezballs May 12 '23
No, its all bad. getErrorsList() could return a null, which would NPE your .contains method.
Personally I'd do this:
return getErrorsList() ? getErrorsList().contains(message)) : false;
1
u/_xonde_ May 12 '23
Shortening it definitely wouldn’t make it less readable imo, even a simple IDE would scream at you to simplify this
1
u/the-software-man May 12 '23
} else {
1
u/Marthurio May 12 '23
I don't believe this is more readable than the simplified version at all. I disagree with your lead. It'd be interesting to hear them back up their comment implying this is more readable.
1
u/ihave7testicles May 12 '23
Where is the '=' for errorsList? That's a syntax error
1
u/benton_bash May 12 '23
How does this even compile without the = ?
1
u/[deleted] May 12 '23
I knew it! The problem in the code is not the redundant method. These extra lines not cause any delayed or misunderstanding. But there is a error on the second line, there isn't an equal symbol.
I haven't programmed in java for a long time, but I think equal symbol still needed, right?
1
u/AdriaNn__ May 13 '23
Am I blind, or there is no equal sign at the declaration of the List? Because in that case, he/she just initialized a function with no body( idk if it is feasable in C# )
1
u/MellowM8 May 13 '23
Then change the name to isErrorString, it isnt checking if its an error, but if the string is used as an error message, just to fuck with your lead
1
u/officialpatterson May 13 '23
I’m not annoyed, I’m just disappointed
1
u/NiteShdw May 14 '23
My IDE highlights these and can replace it with the simpler version.
1
u/Cool-Strike-3328 May 14 '23
www.satyabatiphoolchokinepal.com
1
u/Cool-Strike-3328 May 14 '23
www.satyabatiphoolchokinepal.com
1
u/m_a_n_t_i_c_o_r_e May 14 '23
By far the worst thing about this is random lookup into a data structure that (almost certainly) doesn't support fast random lookup.
Also, stringly-typed error taxonomy... lovely -_____-
1
u/franwebofrito May 30 '23
Your project lead needs to git gud
1
u/[deleted] May 12 '23
I'm guessing there is a story behind the project lead's response.
1
u/Zambeezi May 12 '23
Also, checking for errors in a message list? Why not check for an error enum?
1
u/josephblade May 12 '23
when you do:
you should pretty much always replace it with 'return x;' there is no benefit to the if true return true way of writing code.
it is much more readable to have:
than it is to have this pointless drivel. Now if there is any amount of additional logging or sending events or anything like that happening then it would make sense to have this spaced out but it doesn't become readable code just because the code is spread out across multiple lines.
1
u/DizzySkin May 12 '23
Even aside from how poorly written this is, why is this a method in the first place? Looks like primitive obsession at best, possibly just total garbage that should be deleted wholesale at worst.
1
u/[deleted] May 13 '23
How is this bad code?
-5
u/KOREANPUBLICSCHOOL May 12 '23
I agree with the project lead tbh, readability is more important than clever looking code.
0
u/trutheality May 12 '23
Replace the contents of the function with
return getErrorsList().contains(message);and commit with the message "Improved readability."0
u/Ju29ro- May 12 '23
Project leaders/managers code so little that they often forget simple/best practices
Or they are just individuals with little technical knowledge but with good talkative skills
0
u/illsk1lls May 12 '23
sounds like he cant read 🙈
-1
u/thunderkrown May 12 '23
As a Lead I hate this. Fire him!
-1
u/TransCapybara May 12 '23
That person should be new team lead.
-1
u/JimObeamo May 12 '23
Even the manager two levels up has to be able to read the code. Maybe three levels. Javascript is cool in that it can do the optimizations on the fly. The code is ugly, ugly, ugly., but coders are not the readers.
0
u/[deleted] May 12 '23
[deleted] — view deleted comment