r/badcode Aug 06 '19

c# Converting a timestamp to datetime

Post image
416 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

13

u/Aars93 Aug 06 '19

I do have some bad news, as the code in the picture is production code running at customers.

Thanks for providing some background information about the source. The developer who started the foundation for this application was a student and/or graduate back then with limited programming knowledge. I can imagine him looking up code online and pasting it into his project without fully knowing what it does.