It all depends on what else is happening in the code we don't see. This code looks like it's reading values from some input UI. If there are, for example, two date range inputs and I'm building and app that produces some output that depends on the union of those date ranges, it makes sense to update min and max based on the limits of those inputs and then produce an output for the min-to-max range.
My guess is that the bounds are actually defined in the inputs (I've seen this in a few frameworks where the UI would take care of input validation which includes bounds checking). This code is just extracting the values from there.
It's not clear whether you actually need minYear and maxYear since (given valid inputs) here they're effectively being set to startYear and endYear, but there could be some logic downstream that further restricts min and max based on other ranges.
That's how you determine a minimum or maximum without using a built in.
You define your output variable. Initialize to a really high value or the first value in the list if you're looking for the lowest value. Then when the value in the loop is smaller that your currently smallest known value you update your currently smallest value. And at the end of the loop you have the smallest value in the list you iterated over. Vice versa for maximum of course.
I'm still pretty skeptical that what you're describing is what this code is meant to do, but I appreciate what you're saying - especially reminding me how basic the algorithm is :)
3
u/HolisticHombre Mar 02 '22
... at a certain point, you gotta blame code review.
159
u/trutheality Mar 02 '22
If there's more than one way to do a thing, do it twice just to be sure!
23
u/ign1fy Mar 02 '22
Logic so nice you gotta apply it twice!
20
u/elchzuechter Mar 02 '22
should be in r/BadScreenshot
27
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 02 '22
Image Transcription: Code
[A photograph of code on a computer screen, with the ends of some lines cut off, reading as follows:]
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!
7
u/Gen_Zer0 Mar 02 '22
What's the point of having a minimum or maximum if you're just gonna change it if the data doesn't fall into the range
7
u/[deleted] Mar 02 '22
Is it just me who has more of an issue with the inputs changing the bounds instead of being bounded by them, as opposed to this also being done twice?
2
u/eaglebirdman Mar 02 '22
Idk where to start on this. It all hurts
2
u/thebritisharecome Mar 02 '22
I feel like taking user input, unsanitized and pushing it through ToInt32 is the real gore here
1
u/beric_64 Mar 02 '22
Not saying it's good practice but aren't there integer max and min constants to use if you want to do this sort of comparison
1
u/[deleted] Mar 05 '22
They should add
Just in case
0
u/[deleted] Mar 02 '22
[deleted] — view deleted comment