r/badcode • u/SueedBeyg Good code makes sense. Bad code just works. Yours does neither. • Feb 08 '21
java Browsing through my company's codebase; talk about over-engineering
379
u/Sonotsugipaa Feb 08 '21
"isEmptyIgnoreCase"
247
Feb 08 '21
Capital nothing, my favorite character
58
u/Golden_Flame0 Feb 08 '21
I don't know, lowercase nothing is pretty cool.
3
u/MegaBatchGames Feb 09 '21
I prefer lowercase emptiness too. It looks more visually pleasing.
1
u/branditodesigns Feb 11 '21
I add .equalsIgnoreCase when I don't want to do a toLower then string comparison for a lazy case insensitive :p
37
15
6
u/Igoory Feb 08 '21
For a second I didn't understand the problem with that... I think my brain needs more sleep
2
Feb 08 '21
Hope your boss or HR people are not on Reddit, my guy
1
u/SueedBeyg Good code makes sense. Bad code just works. Yours does neither. Feb 08 '21
So long as they’re not on this 1 particular sub, I’m probably good
5
Feb 08 '21
maybe the engineer is, doesn't recognize the code, blissfully laughing and saying *lol, what garbage!*
5
u/SueedBeyg Good code makes sense. Bad code just works. Yours does neither. Feb 08 '21
Lol if they find out I’ll just say it’s “open-source code review”
1
2
162
u/MetallixBrother Feb 08 '21
Are we all just ignoring the fact that "getLocalDateTimeFromXMLGregorianCalendar" requires an non-null instance of the XMLGregorianCalendar class, and then just ignores it and calls a static method instead? That seems like the real horror here.
45
u/QueenVogonBee Feb 08 '21
Yeah this is completely mental! Clearly a mistake easily made because the name of the variable is the same as the class name.
I’m also curious as to what xmlgregoriancalendar is. My first thought is that we could probably separate concerns a little more (file storage vs calendarism) but it’s hard to say without knowing more.
15
u/carfniex Feb 08 '21
xmlgregoriancalendar is a shitty date class that you often get in jaxb beans, especially if theyre made with cxf etc
17
u/coding_stoned Feb 08 '21
The method name alone is awful enough. I know this is common practice in Java, but still. XMLGregorianCalendar shows up three times in the signature. Surely
getLocalDateTime(XMLGregorianCalendar calendar)is just as clear, without being redundantly verbose.7
u/editor_of_the_beast Feb 08 '21
I think the variable name is the same as the class name, so it just looks like that. But it’s an actual instance method call.
7
u/MetallixBrother Feb 08 '21
The syntax highlighting suggests that it's a static method call. Even if it's just a case of the highlighter being incorrect, the ambiguity is definitely not helpful, and could be fixed by using camel case on the instance.
6
u/editor_of_the_beast Feb 08 '21
Oh 100% this is horrible code on almost infinite dimensions. But I think the syntax highlighting is just busted here, which should alert the person that what they’re doing is heinous.
1
u/Shadow_Being Feb 09 '21
most people who are the kind to make a class file called "CommonUtils" tend to lack common sense.
26
u/reaper-is-happy Feb 08 '21
It's useful when using Stream<T>.filter, but java.lang.Objects already exists for that
22
u/JacobiCarter Feb 08 '21
Objects.isNull(obj) and Objects.nonNull(obj) and Strings.isNullOrEmpty(str)
2
u/Kambz22 Feb 08 '21
Yeah I was going to say isNullOrEmpty is quicker for me to type and I use it all the time but it already exists. There's also a slight chance they do not have the dependency that has that method though.
2
u/JacobiCarter Feb 08 '21
True,
Strings.isNullOrEmptyis Guava, butObjects.isNull/Objects.nonNullare plain Java.
25
u/lsinitramfs Feb 08 '21
idk if its that bad but more "why?"
36
u/weakling24 Feb 08 '21
There is "isEmpty" and "isEmptyIgnoreCase". How often do empty strings differ in case?
-5
u/fakeplasticdroid Feb 08 '21
Besides that, it's trying equate to it to "" which doesn't have casing to begin with.
10
41
u/Golden_Flame0 Feb 08 '21
IsNullOrEmpty is at least useful... why would you ever need the rest?
20
Feb 08 '21
in Java streams for one, generic predicates for another. Variants of these functions exist in the standard libraries of both Java + C#.
The only thing that's a horror is the ignore case one.
7
12
u/Killed_Mufasa Feb 08 '21
Idk, I could see plenty of use-cases for this. Like in primitives or complex functions; isNull(example) is a bit easier to read and less errorprone than example !== null (for which you gotta type the right comparators each time). So I'm kinda twisted about this..
2
u/Raniconduh Feb 08 '21
Just so you know, in line code can be done with single backticks instead of triple ones. E.g. `code` as opposed to ```code```
1
u/AutoModerator Feb 08 '21
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/Killed_Mufasa, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
4
1
u/currykid94 Feb 08 '21
Let me guess Finance?
1
u/SueedBeyg Good code makes sense. Bad code just works. Yours does neither. Feb 09 '21
What gave it away?
1
9
Feb 08 '21
I mean, writing wrappers on primitives is a common programming practice, not exactly a bad code example.
3
u/Poddster Feb 08 '21
The only use would be in passing them as callbacks, e.g. to a map or predicate function.
3
u/_drink_water Feb 08 '21 edited Feb 08 '21
In JS, lodash provides something very close to that, but we're talking about a functional paradigm language that asserts that null == undefined and that any non-declared variable == undefined throws exception.
0
u/AutoModerator Feb 08 '21
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/_drink_water, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/torvold Feb 08 '21
This could be over-engineering, but sometimes it's useful to have predicates that can be passed to higher order functions.. for example psuedocode: filter.on(isNull). Hard to type out a full example on my phone but I can elaborate if that's not clear.
1
1
u/habitats Feb 08 '21
I support this one.
Coming from Scala, code such as ...
val myNewList = myList.filter(isBar).map(toFoo)... is very common, and more readable than ...
val myNewList = myList.filter(x => isBar(x)).map(x => toFoo(x))... which is more verbose and has lower information-density. It also forces you to come up with a name, which often is unnecessary given the type of the list you're working on.
So I applaud this, actually.
5
2
2
2
u/Nice_Reference5388 Feb 08 '21
It would make sense if the null checks were throwing custom null exceptions
2
u/haigha-earwicket Feb 08 '21
Not really. Most of these are best practices found in a lot of codebase to improve readability and extensibility. Particularly in environments where you have junior or rotating staff who want to do these things in 20 different ways.
1
2
u/ColdSnickersBar Feb 08 '21
Oh awesome, and now everything gets to depend on this CommonUtils class forever and ever. Want to distribute your components separately? Better send along this CommonUtils class with it, buddy. Oh did CommonUtils update? Guess what now everything has to update.
Utils classes are terrible. A bunch of stuff grouped together for no other reason than that people can't think of where else to put them, causing multiple Component Principle violations. Then, as it gets worse, it grows into a crappy "utils" or "common" module that will hang around the neck of every module you make forever like a millstone.
2
u/fzammetti Feb 08 '21
Things like this can make a lot of sense in some languages. I've had an isEmpty() utility function in my Javascript code for years. Want to know if a string is empty, or null, or undefined, or only contains space characters? Or if an object contains no properties? Or if an array has no elements? Because functionally, in MOST cases, those are all the same basic condition at the logical level, so a common function that checks ALL those conditions for ALL those types and can't result in any sort of error if anything is missing is quite useful.
In Java though? Not so much.
2
u/AndrewOfBraavos Feb 08 '21
The isNullOrEmpty method is super useful. Especially when processing streams and wanting to pass in a method reference to a filter method. That’s why the ApacheCommons library has the StringUtils class; to provide some of these. Another thing to note is that Java provides an Objects.isNull method for this purpose as well.
2
u/Shadow_Being Feb 09 '21
hey man this sort of thing sis super useful, you never know if you plan to rewrite all the common programming language base functionality later on in the project.
2
1
1
u/thevernabean Feb 08 '21
They may be intended to be used as static imports? Otherwise... Probably just copy pasta.
1
u/RavynousHunter Feb 08 '21
Did we work at the same place? Because that shit's lookin' awful familiar...
1
u/GoddammitDontShootMe Feb 08 '21
Why doesn't isNullOrEmpty() take an Object? That way it will always return true if null, and if it can be converted to a string and that string is empty.
1
u/0vercoded Feb 08 '21
Just playing devil's advocate here, for a larger system where there's plenty of edge cases I can see the merit of running everything through something centralized like this.
I usually reserve judgment on the particulars of this unless I know the system. Shit like that usually doesn't just accidentally get put in. Sometimes it does though.
1
1
u/Gammabyte Feb 08 '21
But how will you check if a string is null or empty while ignoring casing?
You would think that that would be more commonly used than a case sensitive null or empty
1
u/lefthandednipple Feb 15 '21
Oh god. I've seen this code. I know who wrote it. That is from 2015. I had hoped the company had died
1
u/twoBreaksAreBetter Mar 19 '21
All of the useful ones here already exist in standard libs.
But seriously, why the ef is the parameter type Object?
0
Feb 08 '21
[deleted]
8
6
u/circlebust Feb 08 '21
I really don't think primitive concerns like checking for null should be subject to implementation evolution (outside the very language itself deciding to alter it).
TypeScript does allow "native" overloading. And of course, JS can do it too, you just have to handle the
argumentsmore manually.1
184
u/SueedBeyg Good code makes sense. Bad code just works. Yours does neither. Feb 08 '21
And yes, the dev who wrote it encourages me to use `isNull()` whenever I need to do a simple null check.