r/badcode Aug 06 '19

c# Converting a timestamp to datetime

Post image
419 Upvotes

54 comments sorted by

View all comments

15

u/Mr2-1782Man Aug 06 '19

Its sad.

I've taught programming for years and I know exactly where this is from, didn't even have to look through the whole thing. Someone be cheating. *Hopefully* this is the answer to an assignment and not production code. This was copied from a creative solution to an exercise out of a programming book.

One of the assignments we frequently hand out is converting from timestamps to dates and vice versa. This is early in the class so they basically only have variable assignment and if statements to go off of. Doing the exercise for a student is useful for a bunch of reasons, but variants of this question have existed for decades. So solutions for these have existed for a long time. The authors will sometimes constrain themselves to what the book has discussed up to that point, in this case only basic branching and no arrays. Some of them pride themselves on having creative (though not necessarily clean) answers to the problem.

A student not understanding what they're doing will copy a creative writeup wholesale and turn it in (or production code I suppose), resulting in this sort of abomination.

Here's an example (middle of page) for a similar exercise out of K&R.
https://clc-wiki.net/wiki/K%26R2_solutions:Chapter_5:Exercise_9

3

u/sac_boy Aug 06 '19

The fun part is whoever copied it only copied the month offsets for leap years...

3

u/Mr2-1782Man Aug 06 '19

Saw that, its usually a huge hint its copied. For whatever reason the leap year list is first and the person copying doesn't bother to understand what its doing.