To me bad code here is PerformOperation interface with the check method. What operation? Check what? Are all operations working on a single integer? smh
That jumped out to me too, why does the PerformOperation interface exist? What's the purpose in implementing it? Is the "check" method meant to be created for each thing that implements this interface? In fact now that I think about it, it seems a strange pattern for IsOdd to be a class in its own right. What does it mean to have an instance of an IsOdd?
That way you don't have to worry about language specific negative modulo division and also it's (very marginally) faster and better optimised for pipelining/branch prediction
/u/EntertainerUpset3768 is a scammer! It is stealing comments to farm karma in an effort to "legitimize" its account for engaging in scams and spam elsewhere. Please downvote their comment and click the report button, selecting Spam then Harmful bots.
Seems clear to me that this is part of some larger framework where things like "Operation" and "check" have meaning, there's nothing inherently bad about something called PerformOperation with a check method.
Given how little meaning PerformOperation has, I'd prefer using the built in java.util.function.IntPredicate and taking the opportunity to explain the names of all the functional interfaces in that package.
This is just my opinion, but implementing an interface with a check method to see if a number is an odd number or not is a bit over engineered to me. The intent is also not clear.
It may not be that bad in larger context, when you have subclasses implementing frequently used, specialized operation without passing lambda functions everywhere, or pre-java-8 where there's no lambda functions.
But the interface is named PerformOperation? (Java 8 has a Predicate functional interface already defined, btw).
Aaaand it doesn't work for negative numbers either so not only is it horrible but also wrong. Modulo can return a negative result for negative numbers in java. Simplest way around this would be to use Math.floorMod(a, 2) or a % 2 != 0
Lot of levels of bad for such a short piece of code. Shows that less code is only less opportunity for mistakes, but some programmers are very efficient.
181
u/glebbash Feb 11 '23
To me bad code here is PerformOperation interface with the check method. What operation? Check what? Are all operations working on a single integer? smh
61
u/[deleted] Feb 11 '23
This is just my opinion, but implementing an interface with a check method to see if a number is an odd number or not is a bit over engineered to me. The intent is also not clear.
12
u/gipp Feb 11 '23
Can we talk about how the override annotation is in a different font from everything else
14
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 11 '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!
3
u/MmKaz Feb 11 '23
Aaaand it doesn't work for negative numbers either so not only is it horrible but also wrong. Modulo can return a negative result for negative numbers in java. Simplest way around this would be to use
Math.floorMod(a, 2)ora % 2 != 03
u/Final-Communication6 Feb 12 '23
They're getting paid by LOC.
3
u/CheekApprehensive961 Feb 12 '23
Lot of levels of bad for such a short piece of code. Shows that less code is only less opportunity for mistakes, but some programmers are very efficient.
2
u/Aperture_Executive2 Feb 12 '23
IntPredicate isEven = (x) -> x%2 == 0;IntPredicate isOdd = (x) -> !isEven(x);1
u/probaddie42 Feb 11 '23
How much of this is part of a reflective test harness designed to automatically grade code?
1
u/[deleted] Feb 12 '23
i have unironically come across this multiple times
1
u/default-username-2 Feb 14 '23
Can never be too sure - if you ask me, it needs another check, third time's the charm
1
u/UltimatePeace05 Feb 11 '23
HOW DEEP IS THIS!?
1
u/Nerketur Feb 11 '23
Could be faster by saying
a&1 == 1-1
u/Paul_Robert_ Feb 11 '23
return a&1 == 1;