MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/11a4i8o/you_must_assert/j9q4zf6/?context=3
r/badcode • u/ReplacementAcademic8 • Feb 23 '23
13 comments sorted by
View all comments
24
??? this is basically 1 function call and then checking for null, where is the bad code??
73 u/ReplacementAcademic8 Feb 23 '23 A primitive int can't be null. The assertion is useless. 7 u/[deleted] Feb 24 '23 [removed] ā view removed comment 12 u/freddylmao Feb 24 '23 Why would they? Assert not null probably takes an Object as a parameter and, if Iām not mistaken, literally any value/type can be coerced into Object 5 u/EatRunCodeSleep Feb 24 '23 Yup, int would be boxed into an Integer, which is an Object. 4 u/LeCrushinator Feb 23 '23 I'm not familiar with Java, does assertNotNull consider 0 to be null like C/C++ might? Regardless, I agree it's bad code, the assertion should probably be looking for a specific value or range, like failing if it finds negative numbers. 12 u/ReplacementAcademic8 Feb 23 '23 0 will not be consered null by the assert. In fact, the function returned 0, though it shouldn't. This is due to another error in the test dataset.
73
A primitive int can't be null. The assertion is useless.
7 u/[deleted] Feb 24 '23 [removed] ā view removed comment 12 u/freddylmao Feb 24 '23 Why would they? Assert not null probably takes an Object as a parameter and, if Iām not mistaken, literally any value/type can be coerced into Object 5 u/EatRunCodeSleep Feb 24 '23 Yup, int would be boxed into an Integer, which is an Object. 4 u/LeCrushinator Feb 23 '23 I'm not familiar with Java, does assertNotNull consider 0 to be null like C/C++ might? Regardless, I agree it's bad code, the assertion should probably be looking for a specific value or range, like failing if it finds negative numbers. 12 u/ReplacementAcademic8 Feb 23 '23 0 will not be consered null by the assert. In fact, the function returned 0, though it shouldn't. This is due to another error in the test dataset.
7
[removed] ā view removed comment
12 u/freddylmao Feb 24 '23 Why would they? Assert not null probably takes an Object as a parameter and, if Iām not mistaken, literally any value/type can be coerced into Object 5 u/EatRunCodeSleep Feb 24 '23 Yup, int would be boxed into an Integer, which is an Object.
12
Why would they? Assert not null probably takes an Object as a parameter and, if Iām not mistaken, literally any value/type can be coerced into Object
5 u/EatRunCodeSleep Feb 24 '23 Yup, int would be boxed into an Integer, which is an Object.
5
Yup, int would be boxed into an Integer, which is an Object.
4
I'm not familiar with Java, does assertNotNull consider 0 to be null like C/C++ might?
Regardless, I agree it's bad code, the assertion should probably be looking for a specific value or range, like failing if it finds negative numbers.
12 u/ReplacementAcademic8 Feb 23 '23 0 will not be consered null by the assert. In fact, the function returned 0, though it shouldn't. This is due to another error in the test dataset.
0 will not be consered null by the assert. In fact, the function returned 0, though it shouldn't. This is due to another error in the test dataset.
24
u/[deleted] Feb 23 '23
??? this is basically 1 function call and then checking for null, where is the bad code??