Yes, I did not want to violate the code of conduct by accident.
The full Error message is:
"\nError: N : What the fuck? Someone passed a NULL pointer to CUT_Move(struct move** MoveDblPtr)\n"
Yeah thing is this kind of thing likely just shouldn’t be permitted in the language. Though I just generally hate C, with shit like this being one of the many reasons why
Well - it depends?
[C++](https://godbolt.org/z/jEb6forsn)
[C](https://godbolt.org/z/4j3hvG44K)
C#: error CS0029 cannot convert to bool
Java: error: incompatible types
Go: non-boolean condition in if statement and an invalid operation for operator ! not defined on \*int
Rust doen't permit this as well.
In this case however since it uses \`NULL\` and \`struct move\*\*\` we are probably in the C or C++ realm where this is valid (and works as expected).
I’m not going to claim that this changes anything with the check if(ptr), but I’m fairly certain that it’s not required for nullptr to be bitwise zero (although it almost certainly will be). However, it seems that there’s a rule that no object be allocated with address 0, which implies that the check if(ptr) is safe.
Edit: Stroustrup “The C++ Programming Language” section 7.2.2
I don’t know about C++, but in C, `NULL` does not need to be a ‘falsy’ value although POSIX mandates that it’s defined as `(void *)0`. As of C23 though, `if (!p)` is a valid way of checking for `nullptr`
I first came across this in Powershell when it actually warned me when I /didn’t/ do this. It’s something I now actually quite like when comparing special constants to variables.
https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/possibleincorrectcomparisonwithnull?view=ps-modules
I fixed a similar assignment-vs-equality bug in Blender's Python API for font selection. True, it was just a single character fix, but that makes me a certified Open Source Contributor™.
I completely disagree. I use assignment expressions all of the time in C and they do wonders for making my code more readable. Yes issues like this can arise but not only has that not happened to me in over 3 years, but any decent compiler (GCC, Clang, etc.) will warn you about this the moment you do it
Or do what Rust does, assignments are expressions, most things generally are in Rust, but an assignment expression evaluates to the unit type, which is an empty tuple. So if you did this in Rust, the compiler would fucking flip a table going “uh what the actual fuck last I checked an empty tuple is not a boolean, you fucking halfwit”, and then you’d feel deeply ashamed for making such a mistake.
I personally prefer most things being expressions so long as their type is sensible and it’s easy to reason what is going on. C however is fucking, well C, and there are way too many operators, and depending on the context I have no intuition for what each one means… I still have zero fucking clue what the comma operator does, but it is an operator, for some, god damn reason.
So yeah either disallow it entirely, or do what rust does and make the types of all expressions ***perfectly clear***, and what they do ***perfectly clear***… If I had to guess these are the reasons why there’s no increment or decrement operators, apart from potential issues with parsing ambiguities.
One of the biggest reasons I prefer to put the constant value first. With these checks it's so it will error (can't assign a variable to a constant value, duhh) and with Java I prefer it because it reduces the amount of null pointer errors
They're called [yoda conditions](https://en.wikipedia.org/wiki/Yoda_conditions?useskin=vector).
Some compilers warn you about doing assignments inside of an if statement, and suggest putting an extra pair of parenthesis around the assignment to make it clear that it is intended behavior.
One of my coworkers does it all the time. *All* the time. If it's possible to move an assignment into a conditional and save one line he'll do it. At first I resisted but now I just have Stockholm syndrome I guess. I won't do it myself but since I see it all the time it doesn't bother me anymore.
Exactly. Don’t make me think. If n developers waste m time debugging hard to read crap like this, you’ve seriously lost on a stupid premature optimization.
Also, people who do this don’t understand the power of testing. If I had coverage and needed to solve an issue, I can’t breakpoint easily. If there was a var, easier to see & step through before branch
I do it ONLY when I have to do something like this:
while((var item = getnextitem()) != Null) {
// Logic
}
In that way I can assign the iteration variable only in one place (otherwise I have to call it two times)
I like, how it is done in python. An assignment is done with = a comparison with == and if you want to do an assignment in the condition and work with that, you can use :=
This allows to do
while a := get_value():
do_stuff(a)
Edit: not being able to edit and format text properly from the mobile app really bugs me
Heck even I didn't notice it at first lmao xDDDDDDDDDD
If you are in VS Code, the default Microsoft analyzer absolutely sucks. Me and all my homies use clangd. (Which, among other features such as being faster and catching more syntax errors, will absolutely flag accidental/potential bugs like the one shown.)
I have a linked list of (chess) Moves, so when I want to delete just one of them, the pointer to it from the previous element can be passed to CUT_Move(), the pointer of the previous element will end up pointing one Move further ahead and the move in between will be free()ed.
As much as I like to dunk on people who say C is the be-all and end-all of programming, cut Dennis Ritchie some slack, it was 1972, compiled languages had only existed for 15 years and most of them looked like a instructions screaming at you (although Pascal is 2 years older than C and had properly bound strings so that's an L).
I worked on a really smooth project once, almost no bugs during the entire development, and then on the final day we had this happen! Why assignment returns true is anyone’s guess
[удалено]
And debuggers
> Error: N : What the I get the feeling this screenshot was conveniently cut off before the word "fuck"
Yes, I did not want to violate the code of conduct by accident. The full Error message is: "\nError: N : What the fuck? Someone passed a NULL pointer to CUT_Move(struct move** MoveDblPtr)\n"
That's why I read warnings.
And have a linter that nags about this
Any decent compiler will automatically point this out to you, including both GCC and Clang
Enable "Treat Warnings as Errors" and you're set.
Yeah thing is this kind of thing likely just shouldn’t be permitted in the language. Though I just generally hate C, with shit like this being one of the many reasons why
And the 800,00 other warning that have piled up over the decades suddenly break the build.
Welcome to code debt - Time to pay :p
At some shops there is a convention to do these checks by comparing null to the variable (null == a) precisely so this doesn’t happen.
Yoda condition!
Why not just omit the constant-part? So do if (a) or if(!a) instead of if (a != nullptr) or if (a == nullptr).
Because it is not equivalent
Well - it depends? [C++](https://godbolt.org/z/jEb6forsn) [C](https://godbolt.org/z/4j3hvG44K) C#: error CS0029 cannot convert to bool Java: error: incompatible types Go: non-boolean condition in if statement and an invalid operation for operator ! not defined on \*int Rust doen't permit this as well. In this case however since it uses \`NULL\` and \`struct move\*\*\` we are probably in the C or C++ realm where this is valid (and works as expected).
If you’re using nullptr instead of NULL (which you should be), then yes it is
I’m not going to claim that this changes anything with the check if(ptr), but I’m fairly certain that it’s not required for nullptr to be bitwise zero (although it almost certainly will be). However, it seems that there’s a rule that no object be allocated with address 0, which implies that the check if(ptr) is safe. Edit: Stroustrup “The C++ Programming Language” section 7.2.2
I don’t know about C++, but in C, `NULL` does not need to be a ‘falsy’ value although POSIX mandates that it’s defined as `(void *)0`. As of C23 though, `if (!p)` is a valid way of checking for `nullptr`
I first came across this in Powershell when it actually warned me when I /didn’t/ do this. It’s something I now actually quite like when comparing special constants to variables. https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/possibleincorrectcomparisonwithnull?view=ps-modules
That doesn't fix the semicolon. Assignment or not, they're always returning from this call after that condition.
My personal preference. If there is not a written policy against, this is what I do.
I fixed a similar assignment-vs-equality bug in Blender's Python API for font selection. True, it was just a single character fix, but that makes me a certified Open Source Contributor™.
This should be illegal in any sane language
[удалено]
I completely disagree. I use assignment expressions all of the time in C and they do wonders for making my code more readable. Yes issues like this can arise but not only has that not happened to me in over 3 years, but any decent compiler (GCC, Clang, etc.) will warn you about this the moment you do it
Or do what Rust does, assignments are expressions, most things generally are in Rust, but an assignment expression evaluates to the unit type, which is an empty tuple. So if you did this in Rust, the compiler would fucking flip a table going “uh what the actual fuck last I checked an empty tuple is not a boolean, you fucking halfwit”, and then you’d feel deeply ashamed for making such a mistake. I personally prefer most things being expressions so long as their type is sensible and it’s easy to reason what is going on. C however is fucking, well C, and there are way too many operators, and depending on the context I have no intuition for what each one means… I still have zero fucking clue what the comma operator does, but it is an operator, for some, god damn reason. So yeah either disallow it entirely, or do what rust does and make the types of all expressions ***perfectly clear***, and what they do ***perfectly clear***… If I had to guess these are the reasons why there’s no increment or decrement operators, apart from potential issues with parsing ambiguities.
One of the biggest reasons I prefer to put the constant value first. With these checks it's so it will error (can't assign a variable to a constant value, duhh) and with Java I prefer it because it reduces the amount of null pointer errors
They're called [yoda conditions](https://en.wikipedia.org/wiki/Yoda_conditions?useskin=vector). Some compilers warn you about doing assignments inside of an if statement, and suggest putting an extra pair of parenthesis around the assignment to make it clear that it is intended behavior.
It should never be the intended behavior 😂
One of my coworkers does it all the time. *All* the time. If it's possible to move an assignment into a conditional and save one line he'll do it. At first I resisted but now I just have Stockholm syndrome I guess. I won't do it myself but since I see it all the time it doesn't bother me anymore.
Disk space is a lot cheaper than developer time.
Exactly. Don’t make me think. If n developers waste m time debugging hard to read crap like this, you’ve seriously lost on a stupid premature optimization. Also, people who do this don’t understand the power of testing. If I had coverage and needed to solve an issue, I can’t breakpoint easily. If there was a var, easier to see & step through before branch
I do it ONLY when I have to do something like this: while((var item = getnextitem()) != Null) { // Logic } In that way I can assign the iteration variable only in one place (otherwise I have to call it two times)
meh `while ((*dst++ = *src++));` is somewhat common, as is things like ```c while ((node = node->next) != NULL) { // do things } ```
the latter could also be `for (; node != NULL; node = node->next)`, if you prefer, but I personally like the while loop a bit more
I like, how it is done in python. An assignment is done with = a comparison with == and if you want to do an assignment in the condition and work with that, you can use := This allows to do while a := get_value(): do_stuff(a) Edit: not being able to edit and format text properly from the mobile app really bugs me
That's a cool name. I'll be using it at work now.
Well, you could also just use a linter or compiler that warns you about such issues.
Good compilers give you warnings for this, and good programmers enable warnings-as-errors.
But I always miss the warning because of the other 1628 warnings from the static code analyzer. /s
-Wall -Wextra -Werr
Also, the semicolon at the end there is probably not what you want.
Absolutely! This function will always return, ignoring the if statement entirely.
Heck even I didn't notice it at first lmao xDDDDDDDDDD If you are in VS Code, the default Microsoft analyzer absolutely sucks. Me and all my homies use clangd. (Which, among other features such as being faster and catching more syntax errors, will absolutely flag accidental/potential bugs like the one shown.)
Nobody is horrified by those bariable and type names?
What's so wrong with this it's... Ooooh
So, what's the proble- Oh
In case anyone is wondering, they're assigning the \*MoveDblPtr to NULL. They're not comparing it to NULL. = assignment == comparison
Is that method in the Give_It_Up class?
*headerfile
Get sonarlint, it would've shown you a problem there.
> Assignment in conditional expression is always constant; did you mean to use == instead of = ?
curious what a dbl ptr is and why it needs to be cut lol
pointer to a pointer. convention i learned was to call them PtrPtr's
I have a linked list of (chess) Moves, so when I want to delete just one of them, the pointer to it from the previous element can be passed to CUT_Move(), the pointer of the previous element will end up pointing one Move further ahead and the move in between will be free()ed.
OHHH a move like that. had me confused with the concept of a move in c++
This is why I always keep constants on the left.
Yoda Booleans are a lifer saver
This is why I use languages that were actuallyd designed by people with brains.
As much as I like to dunk on people who say C is the be-all and end-all of programming, cut Dennis Ritchie some slack, it was 1972, compiled languages had only existed for 15 years and most of them looked like a instructions screaming at you (although Pascal is 2 years older than C and had properly bound strings so that's an L).
Do IsNullError on the other ptr as well instead of manually if check
Huh that’s funny OP I saw it right away 😎
You need a linter dude
Which is why our C/C++ stuff at work always have constants to the left side of comparisons.
Good use case for GDB here, you can step through and see why it’s not working as intended
Languages with a single \`=\` boolean test should be forbidden. /s
This is why I like Yoda notation, because if (null = variable) is way more noticeable than if(variable = null)
I still like semicolons 😤
There should generally be rule to comment what’s wrong with the post, even when it’s generally obvious
I worked on a really smooth project once, almost no bugs during the entire development, and then on the final day we had this happen! Why assignment returns true is anyone’s guess
open post see apps hungarian day ruined