this post was submitted on 28 Mar 2024
611 points (98.1% liked)

Programmer Humor

32568 readers
148 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] cookie_sabotage 160 points 8 months ago (4 children)
public class GameManager : MonoBehaviour
{
    public bool EnableHighContrast;
    public bool PlayerWon;
    public float PlayerUnitsMoved;
    public int PlayerDeathCount;
    public float PlayerHealth;

    public void PlayerTakeDamage(float damage)
    {
        PlayerHealth -= damage;
        if (PlayerHealth < 0)
        {
            PlayerDieAndRespawn();
        }
    }

    public void PlayerDieAndRespawn()
    {
        return;
    }
}

I couldn't contain myself.

[–] [email protected] 57 points 8 months ago (8 children)

Should it be

PlayerHealth <= 0

?

Otherwise the player could have 0 health and not die? I’m sleep deprived so forgive me if I’m wrong

[–] [email protected] 27 points 8 months ago (1 children)
[–] [email protected] 54 points 8 months ago (1 children)

Counting this meme as my first FOSS contribution

[–] [email protected] 12 points 8 months ago

Holy shit I was there with you sir! With the zeros and stuff

[–] [email protected] 26 points 8 months ago

Open up ticket first, please. Thanks Codemonkey.

[–] [email protected] 12 points 8 months ago* (last edited 8 months ago)

You are correct about it allowing you to have zero health and not die, but whether or not that's the correct behavior will depend on the game. Off the top of my head I know that Street Fighter, some versions at least, let you cling to life at zero.

load more comments (5 replies)
[–] [email protected] 34 points 8 months ago (2 children)

Yay, escaped the fight with 0 health!

[–] [email protected] 15 points 8 months ago

Well if you have a "down but not dead" condition then yes, you could escape a fight with 0 health (assuming you have teammates/pawns that can save you).

load more comments (1 replies)
[–] [email protected] 10 points 8 months ago (1 children)

I called the takeDamage function and my player disappeared: send 'elp everything foobar

[–] cookie_sabotage 15 points 8 months ago (1 children)

Don't worry! this issue will be fixed in the next patch. In the meantime just try not getting hit.

load more comments (1 replies)
load more comments (1 replies)
[–] [email protected] 112 points 8 months ago (1 children)

Great. Now that my code is self-documenting it is somehow also not legible?

Make up your damn minds, peeps!

[–] [email protected] 22 points 8 months ago

Is this... clean code ? 💁‍♂️ 🦋

[–] [email protected] 101 points 8 months ago (1 children)

gestures at ~~butterfly~~ this code

Is this self-documenting code?

[–] [email protected] 39 points 8 months ago (1 children)

I genuinely believe something like this is what some of my professors wanted me to submit back in school. I once got a couple points off a project for not having a clarifying comment on every single line of code. I got points off once for not comment-clarifying a fucking iterator variable. I wish I could see what they would have said if I turned in something like this. I have a weird feeling that this file would have received full marks.

[–] [email protected] 12 points 8 months ago (1 children)

Did you have my professor for intro to C? This guy was well known for failing people for plagiarism on projects where the task was basically "hello world". And he disallowed using if/else for the first month of class.

[–] [email protected] 6 points 8 months ago (4 children)

Reminds me of an early Uni project where we had to operate on data in an array of 5 elements, but because "I didn't teach it to everyone yet" we couldn't use loops. It was going to be a tedious amount of copy-paste.

I think I got around it by making a function called "not_loop" that applied a functor argument to each element of the array in serial. Professor forgot to ban that.

load more comments (4 replies)
[–] [email protected] 71 points 8 months ago (2 children)

This is something that can easily get refactored, because the purpose of alia the variables is right there in the name. This is way better that spending three days to try to figure out what the purpose of var1 is.

[–] jballs 7 points 8 months ago (1 children)

Nah, refactoring this would be a bitch. Your function name contains everything that happens in the function. Which means if you add something to it, you also have to change the name of the function. So CallThisWhenThePlayerTakesDamageAndIfThePlayerHealthIsLessThanZeroThenAlsoTheyDie would have to go to something like CallThisWhenThePlayerTakesDamageAndIfThePlayerHealthIsLessThanZeroThenAlsoTheyDieAndIncrementTheTotalDamageTakenCounter if you added something else.

[–] [email protected] 11 points 8 months ago

IDE renames all references, no issue

load more comments (1 replies)
[–] [email protected] 71 points 8 months ago (2 children)

I would take this over int a; anyday

[–] [email protected] 31 points 8 months ago (1 children)

Amateur! Ints should be called i, j or k.

[–] [email protected] 27 points 8 months ago (5 children)

Cmon now its illegal to use them anywhere other than iterators

load more comments (5 replies)
[–] [email protected] 18 points 8 months ago

int IntegerThatWillBeUsedToIterateOverAListOfItemsAndModifyThem2

[–] [email protected] 48 points 8 months ago* (last edited 8 months ago) (1 children)

I mean, this is overdoing it a bit and the "thisVarMakesItSoThat" part is redundant, but other than that those are very descriptive property- and method names, which is not a bad thing.

[–] [email protected] 11 points 8 months ago (3 children)

It wouldn't need to say HighContrastForAccessibilityPurposes though, it would ideally just be HighContrast, and the "for accessibility purposes" would be a comment, right?

[–] [email protected] 11 points 8 months ago* (last edited 8 months ago) (2 children)

Variable names shouldn't need comments, period. You don't want to look it up every time this variable is used in code, just to understand what it holds. Of course there are always exceptions, but generally names should be descriptive enough to not need additional explanation.

And context can also come from names of other things, e.g. name of a class / namespace that holds this variable. For example AccessibilitySettings.HighContrast, where AccessibilitySettings holds all options related to accessibility.

load more comments (2 replies)
load more comments (2 replies)
[–] [email protected] 31 points 8 months ago (1 children)
[–] [email protected] 12 points 8 months ago

Always a relevant xkcd.

[–] [email protected] 30 points 8 months ago

Deobfuscated code

[–] [email protected] 22 points 8 months ago

yeah, PascalCase is the worst

[–] [email protected] 21 points 8 months ago (1 children)

I'll take this over the more "classic" styles, when people seed to believe they were paying the compiler by the character.

[–] [email protected] 7 points 8 months ago (1 children)

I respect code golfers the same way I respect a cobra, from a distance. Don't bring that single character naming to the codebase please.

load more comments (1 replies)
[–] Socsa 17 points 8 months ago

Self documenting!

[–] [email protected] 16 points 8 months ago

Starting my day off with this absolutely cursed image, thank you OP.

[–] [email protected] 14 points 8 months ago (2 children)

Looks ugly until you need to implement something and realize you've been blessed with a description of business logic.

[–] [email protected] 17 points 8 months ago

Strong names are great, but (sometimes) mentioning the type of variable in the name is redundant.

load more comments (1 replies)
[–] [email protected] 14 points 8 months ago* (last edited 8 months ago) (6 children)

The real naming fail is calling the class "GameManager", still my number one pet peeve. With a class name as vague as that you would have to add tons of information into the variable name. (Also the class name begs for unorganized code. I mean name one function or variable that you could not justify putting into the "GameManager" class. After all if it's managing the game it could justifiably perform any process in the game and access any state in it.)

Once you put the first bool into a class with a name like AccessibilitySettings, calling it something like HighContrast is completely sufficient.

[–] [email protected] 6 points 8 months ago

We've all been guilty of these mistakes, naming stuff is hard, structuring your project is hard, learning the grains of a language takes time. But comments like these are golden nugets, some might read this and think "oh yeah, this makes sense" and rethink their whole methodology of naming and structure. You might have pushed someone reading your comment, to think more about these things.

load more comments (5 replies)
[–] [email protected] 12 points 8 months ago
[–] [email protected] 11 points 8 months ago (1 children)

Bruh didn't know how to use comments to describe a function or variable...

[–] [email protected] 15 points 8 months ago

Clearly this is what we call "self documenting code".

[–] [email protected] 10 points 8 months ago

"Commenting is for n00bs"

[–] [email protected] 9 points 8 months ago

You forgot to declare custom primitive types. You cannot create a bool you gotta declare a DoubleYouDoubleYouDoubleYouDotLemmyGradDotML_Bool

[–] [email protected] 8 points 8 months ago

Self-explanatory code doesn't need comments!

[–] [email protected] 8 points 8 months ago

I see. So that’s why people put comments before variable declarations.

[–] [email protected] 7 points 8 months ago

Like... there's such a thing as comments my dude.

load more comments
view more: next ›