Like… there’s such a thing as comments my dude.

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.

Yay, escaped the fight with 0 health!

andrew
link
fedilink
English
58M

This is floating point. We also need to know what happens when you escape with -0.

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).

Too readable, please make each name a paragraph describing its function and how it relates to the other variables/functions around it

Wise
link
fedilink
English
578M

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

This won’t work if you can ever take more than 1 damage. If you were at 1 and received 2 damage you would become invincible. You’d want to do less than or equal to.

I know this is /c/Progammerhumor, but I wanted to pull on this thread a little bit for my own edification. I’m a Python guy and have been a while, but I’ve dabbled in other languages. The screenshot says “MonoBehaviour” which makes me assume this is mono or a .Net-like language (you know what happens when you assume).

If your player health is a float, would mono or .Net have an issue comparing the float with integer zero “0”? I mean, it seems like floating point precision may make it impossible for it to ever “equal” integer zero, but it also seems like the code isn’t accounting for that precision error.

Am I overthinking this?

Floating point errors are a product of how floating points work as a mathematical concept. So they’re independent of the programming language and can happen everywhere.

In this case though, I doubt it’s a critical issue. So the player “died” when they actually had 0.000000000027 hp left or whatever. Who cares? Do you need to be that precise?

TipRing
link
fedilink
148M

Hanging on with 1.70E-31 health.

As a noob in unity and programming, my understanding is that MonoBehavior only means that this script has to be attached as a component to a game object to function. And has no other meaning - but correct me if I’m wrong please.

Open up ticket first, please. Thanks Codemonkey.

@vithigar@lemmy.ca
link
fedilink
12
edit-2
8M

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.

You are correct.

Wise
link
fedilink
English
548M

Counting this meme as my first FOSS contribution

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

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

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

mac
link
fedilink
68M

The doctor prescribed “getting good”

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.

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

@jballs@sh.itjust.works
link
fedilink
English
78M

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.

IDE renames all references, no issue

Then they change what the function does without updating the name and you misunderstand the code completely.

Self documenting!

gestures at butterfly this code

Is this self-documenting code?

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.

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.

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.

but because “I didn’t teach it to everyone yet” we couldn’t use loops.

That is aggravating. “I didn’t teach the class the proper way to do this task, so you have to use the tedious way.” What is the logic behind that other than wasting everyone’s time?

Teaching someone the wrong way to do something frequently makes the right way make way more sense. Someone who just copy/pasted 99 near identical if statements understands on a fundamental level when, why, and where you use a for loop much more than someone who just read in the textbook “a for loop is used to iterate elements in a collection”.

I Cast Fist
link
fedilink
38M

Reminds me of a dude that wrote the equivalent of this in Visualg (a brazilian pseudocode language and program, meant solely for teaching programming)

if
  if
    if
      if
        if (x < 10) then
          print(x)
        else
      else
    else
  else
else

That the thing ran and didn’t complain about the amount of loose/needless if’s checking fuck all baffles my mind to this day.

And if I know the right way of doing it I already understand why it’s better because I want to use it in this situation. Making the students who already understand the lesson do it the wrong way is just a waste of their time.

Hot take, but the main problem with this is that you have to type variables or use them in larger expressions. Otherwise I like it.

These would make a great mouseover text. I don’t know if there’s any standard way to support that. Actually, how come coding in non-plaintext formats never took off?

Einar
link
fedilink
English
28M

snake_case_might_be_better_for_that

In pretty much any language I’ve used there is some standard for doc comments that would show up as mouseover text in the IDE.

I need like a tutorial on IDEs, because clearly I’m using them wrong. Or I tend to work on things that are poorly documented and just haven’t been able to take advantage, I guess that’s possible.

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

When the variable name is the description that should be in the comments.

Idea: Comments that automattically populate the end of any line a given variable is invoked on, including spelling out formulas from that line. ie: float y=mx+b // (cartesian y value)=(slope)(cartesian x value)+(cartisian y-intercept)

“Duplicated” coments not actually in the file, but specified witt the creation of such variables and spread around by the code editor /IDE.

@paholg@lemm.ee
link
fedilink
English
48M

Then, you could take those comments, and have the compiler use them to ensure you’re using the right variable in the right place. Oh wait, we just invented a type system.

“Commenting is for n00bs”

LostXOR
link
fedilink
128M

Always a relevant xkcd.

Zuberi 👀
link
fedilink
08M

🤮

idunnololz
link
fedilink
48M

Wtf. It’s not even center aligned.

Deobfuscated code

Alien Nathan Edward
link
fedilink
English
228M

yeah, PascalCase is the worst

@JakenVeina@lemm.ee
link
fedilink
English
218M

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

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.

Exactly, use emojis or gtfo

Create a post

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

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
  • 1 user online
  • 146 users / day
  • 305 users / week
  • 696 users / month
  • 2.84K users / 6 months
  • 1 subscriber
  • 1.57K Posts
  • 34.8K Comments
  • Modlog