Aaargh....my eyes......my eyes......
Aaargh....my eyes......my eyes......
Aaargh....my eyes......my eyes......
undefined
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.
Should it be
undefined
PlayerHealth <= 0
?
Otherwise the player could have 0 health and not die? I’m sleep deprived so forgive me if I’m wrong
You are correct.
Open up ticket first, please. Thanks Codemonkey.
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.
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?
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.
Yay, escaped the fight with 0 health!
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).
This is floating point. We also need to know what happens when you escape with -0.
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.
Too readable, please make each name a paragraph describing its function and how it relates to the other variables/functions around it
Great. Now that my code is self-documenting it is somehow also not legible?
Make up your damn minds, peeps!
Is this... clean code ? 💁♂️ 🦋
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.
I would take this over int a; anyday
Amateur! Ints should be called i, j or k.
Cmon now its illegal to use them anywhere other than iterators
int IntegerThatWillBeUsedToIterateOverAListOfItemsAndModifyThem2
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.
Nah, refactoring this would be a removed. 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
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
Then they change what the function does without updating the name and you misunderstand the code completely.
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.
It wouldn't need to say HighContrastForAccessibilityPurposes though, it would ideally just be HighContrast, and the "for accessibility purposes" would be a comment, right?
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.
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.
Noone yet?
Always a relevant xkcd.
Deobfuscated code
yeah, PascalCase is the worst
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
Self documenting!
Starting my day off with this absolutely cursed image, thank you OP.
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.
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.
With a class name as vague as that you would have to add tons of information into the variable name.
Technically they did exactly that.
Unity actually gives any class with the name GameManager a special gear icon. You cant just forgo the cool gear icon!
(Its not too terrible from an organizational standpoint because most of the scripts are attached to game objects. MonoBehavior is a component of GameObject. For instance, you’d never have player movement in the GameManager class, you would put it in the component class attached to the player character GameObject.)
you’d never have player movement in the GameManager class
You want to bet? (Source: I teach game programming on a college level.)
But yeah, your comment about the gear icon is sadly more true than people may realize. Game developers do questionable things. => Engine developers cater to people. => Students argue that if something is supported it can't be that bad. Sometimes it feels like fighting windmills.
"Manager" classes often end up like "God classes", just like how "Utils" classes end up with a bunch of random stuff in them.
At my first job I was working on an MMO and we had a DatabaseManager class with 10k+ lines of code. Less than the first 200 lines actually used any of the members of that class.
Looks ugly until you need to implement something and realize you've been blessed with a description of business logic.
Strong names are great, but (sometimes) mentioning the type of variable in the name is redundant.
Until they find out that the way to descriptive variables or functions needs to be extended with new business logic requiring renaming of functions again and again.
I think maintaining code with this level of verbose naming, will be a pain over time. If they don't let the naming slip, and then they could as well use cryptic 3 letter names.
Bruh didn't know how to use comments to describe a function or variable...
Clearly this is what we call "self documenting code".
Reminds me of this https://lemmy.world/post/12487512
"Commenting is for n00bs"
Self-explanatory code doesn't need comments!
You forgot to declare custom primitive types. You cannot create a bool you gotta declare a DoubleYouDoubleYouDoubleYouDotLemmyGradDotML_Bool
I see. So that’s why people put comments before variable declarations.
Like... there's such a thing as comments my dude.
String if_you_must_please_at_least_separate_the_words
That's just camel case vs snake case (though in this case, it also has the first character capitalised)
PascalCase is standard in C#
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?
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.
snake_case_might_be_better_for_that
Wtf. It's not even center aligned.
The curse of camel case!
youCannotImagineTheLookOfDisgustOnMyFace
PascalCase
I don't (brain processes the photons bouncing off the object and colliding with the cones and rods in the retina) a problem with this.
(See what I did there?)
(I think I met my dad joke quota for the month.)
this is a crime against humanity
I’m already annoyed by the end of public static final string
that the variable name doesn’t even bother me in these sorts of langauages. All things should be public, static, & final, by default so they aren’t necessary to write. You should only have to write what unsafeties you are opting into.
You may enjoy having a look at F#. It says that it's "functional first", but I think a better description would be "an opinionated version of C#".
For example it doesn't have a "const"-keyword. Instead it has a "mutable"-keyword, because everything is const by default.
Being attached to the CLR isn’t my cup of tea, but I have been learning its older cousin OCaml off & on the last 1½ years
Aah.... Like reading a novel. But with a lot of weird punctuation.... Or maybe just like a novel then.
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.
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.
I use C++ and in certain projects, I am already halfway there.
🤮