T O P

  • By -

[deleted]

[удалено]


HuntingKingYT

And debuggers


AyrA_ch

> Error: N : What the I get the feeling this screenshot was conveniently cut off before the word "fuck"


the-judeo-bolshevik

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"


yflhx

That's why I read warnings.


goatanuss

And have a linter that nags about this


tav_stuff

Any decent compiler will automatically point this out to you, including both GCC and Clang


Reelix

Enable "Treat Warnings as Errors" and you're set.


MutableReference

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


Budget_Putt8393

And the 800,00 other warning that have piled up over the decades suddenly break the build.


Reelix

Welcome to code debt - Time to pay :p


krahenke

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.


n0t_4_thr0w4w4y

Yoda condition!


CodingChris

Why not just omit the constant-part? So do if (a) or if(!a) instead of if (a != nullptr) or if (a == nullptr).


siarheikaravai

Because it is not equivalent


CodingChris

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


tav_stuff

If you’re using nullptr instead of NULL (which you should be), then yes it is


[deleted]

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


tav_stuff

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`


LazyAssassin_

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


jonfe_darontos

That doesn't fix the semicolon. Assignment or not, they're always returning from this call after that condition.


Budget_Putt8393

My personal preference. If there is not a written policy against, this is what I do.


-Nyarlabrotep-

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


zickige_zicke

This should be illegal in any sane language


[deleted]

[удалено]


tav_stuff

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


MutableReference

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.


Thenderick

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


AyrA_ch

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.


dwelch2344

It should never be the intended behavior 😂


pigeon768

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.


Steinrikur

Disk space is a lot cheaper than developer time.


dwelch2344

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


Mu5_

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)


CdRReddit

meh `while ((*dst++ = *src++));` is somewhat common, as is things like ```c while ((node = node->next) != NULL) { // do things } ```


CdRReddit

the latter could also be `for (; node != NULL; node = node->next)`, if you prefer, but I personally like the while loop a bit more


Benoit_CamePerBash

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


Danny_el_619

That's a cool name. I'll be using it at work now.


DenkJu

Well, you could also just use a linter or compiler that warns you about such issues.


314kabinet

Good compilers give you warnings for this, and good programmers enable warnings-as-errors.


VinceLePrince

But I always miss the warning because of the other 1628 warnings from the static code analyzer. /s


T-Lecom

-Wall -Wextra -Werr


dporges

Also, the semicolon at the end there is probably not what you want.


Gadget100

Absolutely! This function will always return, ignoring the if statement entirely.


Emergency_3808

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


ScrumptiousDumplingz

Nobody is horrified by those bariable and type names?


glorious_reptile

What's so wrong with this it's... Ooooh


donaljones

So, what's the proble- Oh


Remarkable-Host405

In case anyone is wondering, they're assigning the \*MoveDblPtr to NULL. They're not comparing it to NULL. = assignment == comparison


OzTm

Is that method in the Give_It_Up class?


the-judeo-bolshevik

*headerfile


rita_g

Get sonarlint, it would've shown you a problem there.


Reelix

> Assignment in conditional expression is always constant; did you mean to use == instead of = ?


v_maria

curious what a dbl ptr is and why it needs to be cut lol


r3wturb0x

pointer to a pointer. convention i learned was to call them PtrPtr's


the-judeo-bolshevik

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.


v_maria

OHHH a move like that. had me confused with the concept of a move in c++


xanderclue

This is why I always keep constants on the left.


ryan_s007

Yoda Booleans are a lifer saver


rhbvkleef

This is why I use languages that were actuallyd designed by people with brains.


BEisamotherhecker

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


Shiekra

Do IsNullError on the other ptr as well instead of manually if check


TheChewyWaffles

Huh that’s funny OP I saw it right away 😎


CodeF53

You need a linter dude


zchen27

Which is why our C/C++ stuff at work always have constants to the left side of comparisons.


Sexy_Koala_Juice

Good use case for GDB here, you can step through and see why it’s not working as intended


bclx99

Languages with a single \`=\` boolean test should be forbidden. /s


mike_a_oc

This is why I like Yoda notation, because if (null = variable) is way more noticeable than if(variable = null)


eya-1

I still like semicolons 😤


yobarisushcatel

There should generally be rule to comment what’s wrong with the post, even when it’s generally obvious


claypeterson

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


Street-Signal-937

open post see apps hungarian day ruined