r/programminghorror • • Mar 08 '26
I've refactored the leap year checker to conform to OOP standards and use descriptive names
/img/xaj06d5zjwng1.png
596
Upvotes
r/programminghorror • • Mar 08 '26
51
u/CrasseMaximum Mar 08 '26
You could have made the year variable const /s
59
u/Infinite_Self_5782 Mar 08 '26
15
u/ings0c Mar 08 '26 edited Mar 08 '26
*shudder*
You just reminded me of the time where we had tables that were “sharded” on year, so we had:
All with the same schema. There was a lot of data that was updated each week, and we’d overwrite the current year, leaving the historical data.
I had to get it to work with Entity Framework and found no alternative at the time other than to make a bunch of entities like:
public class MyThing2025 : ThingBase {}
public class MyThing2026 : ThingBase {}
public class MyThing2027 : ThingBase {}
Someone should have told me about partitioned tables
13
u/cookingforengineers Mar 08 '26
Just load a date library, create the Feb 29 date for that year and check if the date matches Feb 29. If it is, then it is a leap year!
/s
12
u/brotatowolf Mar 08 '26
I’ve seen shit like this in production
9
u/matrayzz Mar 09 '26
1. Domain Object (Value Object)
2. Leap Year Specification (Specification Pattern)
3. Gregorian Leap Year Implementation
4. Strategy Interface
5. Strategy Implementation
6. Strategy Factory
Because enterprise systems never instantiate things directly.
7. Singleton Service
Naturally this must be globally accessible.
8. Facade Layer
Clients must not interact with services directly.
9. Builder for Request DTO (Completely unnecessary)
10. Controller (Enterprise Entry Point)
11. Usage
7
u/No_Pollution9224 Mar 08 '26
This would be best exposed as a SOAP endpoint for the whole enterprise to leverage. You can call it from your Java applets everywhere.
9
u/lucidbadger Mar 08 '26
Use recursive templates ffs!!!11
3
u/HildartheDorf Mar 09 '26
The default in Year should return false and only be overridden in derived classes (where it returns true).
Hell, why not a LeapYear class that derives from Year and returns true, then Year2028 etc. can inherit from LeapYear!
3
u/StraightGuy1108 Mar 09 '26
Acktually you should have followed the strategy pattern and encapsulate the leap-ness into a
IsLeapBehaviorinterface, then make individualYearXXXXbe composed of and delegate to a specific leap behavior 🤓2
u/utolso_villamos Mar 09 '26
Year should be an enum
1
u/c0nfleis95 Mar 09 '26
I know this is not the focal point, but what syntax theme is this 😂
1
u/Droggl Mar 09 '26
Bad code. This needs a singleton factory class (with an abstract base) and a builder class with a separe interface.
1
u/Steinrikur Mar 09 '26
For the love of God, do not turn this sub into a place for deshittifying isLeapYear() like it was for isOdd() last year.
This isn't funny. Intentionally writing bad code isn't horror. Take this to /r/ProgrammingHorrorCircleJerk
1
u/nobody0163 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Mar 09 '26
Where is the
YearFactory1
u/SirFoomy Mar 09 '26
To some people indentation is obviously just a suggestion.
1
u/amarao_san Mar 09 '26
I believe, they have generics for that.
... Can we put a year into a lifetime?
1
u/Dark-W0LF Mar 09 '26
IsLeap=Year%4 Right?
1
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Mar 10 '26
I guess this is our new
isEven().1
u/northerncodemky Mar 10 '26
Please do template specialisation and offset the cost of that vtable lookup to compile time
1
u/dreamingforward Mar 10 '26
Tip: Never use a class when you can use a simple function. A normal function carries nor remembers no state between calls.
6
u/[deleted] Mar 08 '26
[removed] — view removed comment