this post was submitted on 26 Nov 2023
707 points (89.2% liked)

Programmer Humor

19817 readers
556 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] magic_lobster_party@kbin.social 11 points 1 year ago* (last edited 1 year ago) (2 children)

I’ve seen code that look like this:

int delay = 15 * 60; // 10 minutes

Even if the comment was on the same line someone forgot to update it. People just ignore comments.

Better solution is to write (in C#):

TimeSpan delay = TimeSpan.FromMinutes(15)

Much more obvious what the code actually means.

[–] 18107@aussie.zone 7 points 1 year ago (1 children)

A better comment would be delay in seconds as that is the one thing not obvious from glancing at the code.

[–] magic_lobster_party@kbin.social 13 points 1 year ago

Or just name the variable delaySeconds if you really want to store it as an int. Bonus is that every use of the variable perfectly communicates what it is.

[–] CCatMan@lemmy.one 1 points 1 year ago (1 children)

Is the better way is a runtime performance hit. Does the compiler optimize this?

[–] magic_lobster_party@kbin.social 1 points 1 year ago (1 children)

It’s probably a little bit slower, but there are other things more worth to optimize than to shave off a few microseconds from a 15 minute delay.

[–] CCatMan@lemmy.one 1 points 1 year ago (1 children)

Yeah, it adds up eventually when working with embedded platforms, but for PC stuff I agree.

If you’re working in embedded I guess you can probably make an inline function or a macro so it’s taken care of at compile time.