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

Programmer Humor

32410 readers
1019 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
[–] [email protected] 155 points 7 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] 55 points 7 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] 26 points 7 months ago

Open up ticket first, please. Thanks Codemonkey.

[–] bassomitron 25 points 7 months ago (1 children)
[–] [email protected] 54 points 7 months ago (1 children)

Counting this meme as my first FOSS contribution

[–] SidewaysHighways 12 points 7 months ago

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

[–] [email protected] 12 points 7 months ago* (last edited 7 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 7 months ago (2 children)

Yay, escaped the fight with 0 health!

[–] [email protected] 15 points 7 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)
[–] Sakychu 10 points 7 months ago (1 children)

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

[–] [email protected] 15 points 7 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] 111 points 7 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 7 months ago

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

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

gestures at ~~butterfly~~ this code

Is this self-documenting code?

[–] [email protected] 39 points 7 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.

[–] maniclucky 12 points 7 months ago (5 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.

load more comments (5 replies)
[–] [email protected] 70 points 7 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.

[–] [email protected] 7 points 7 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 7 months ago

IDE renames all references, no issue

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

oh such hope

in a week IntegerThatTracksOneThingForOnePurpose will be an object tracking 30% of the game state and mutated in 15 places without ever being renamed

[–] [email protected] 69 points 7 months ago (2 children)

I would take this over int a; anyday

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

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

[–] [email protected] 26 points 7 months ago (5 children)

Cmon now its illegal to use them anywhere other than iterators

load more comments (5 replies)
[–] Ironfacebuster 18 points 7 months ago

int IntegerThatWillBeUsedToIterateOverAListOfItemsAndModifyThem2

[–] [email protected] 47 points 7 months ago* (last edited 7 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 7 months ago (2 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 7 months ago* (last edited 7 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)
[–] [email protected] 6 points 7 months ago* (last edited 7 months ago) (1 children)

Well the "Purposes" can definitly be dropped. I guess "HighContrast" would be enough if there is only a single high contrast setting, but if there are multiple then I think "HighContrastForAccessibility" would be totally fine.

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

Always a relevant xkcd.

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

Deobfuscated code

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

yeah, PascalCase is the worst

[–] [email protected] 21 points 7 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.

[–] Theharpyeagle 7 points 7 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)
[–] [email protected] 16 points 7 months ago

Self documenting!

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

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

[–] vacuumoftalent 14 points 7 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 7 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 7 months ago* (last edited 7 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.

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

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

[–] waz 15 points 7 months ago

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

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

"Commenting is for n00bs"

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

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

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

Self-explanatory code doesn't need comments!

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

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

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

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

load more comments
view more: next ›