r/ProgrammerHumor • • Feb 03 '22

Meme Well Fuck

Post image
27k Upvotes

993 comments sorted by

View all comments

Show parent comment

116

u/superluminary Feb 03 '22

It’s not a const though, it’s a static bool.

47

u/Ok_Blueberry_5305 Feb 03 '22 edited Feb 03 '22

Idk about other languages, but C#doesn't allow you to do assignments in an if condition. Regardless of whether it's a const or not

Edit: I misremembered it as an error. It will let you do it, but will show a suggestion asking if you meant ==.

Edit 2: seems I might've just been imagining things? It doesn't seem to raise a suggestion unless one side is a constant. In this case (because =true) the compiler would ask if you mean it, while simultaneously letting you do it.

9

u/ClikeX Feb 03 '22

It's perfectly fine in Ruby.

18

u/Galactinus Feb 03 '22

As is c and c++. I have made this mistake

5

u/SANatSoc Feb 03 '22

Unfortunately this is such a subtle little fucker, even seasoned devs can make this mistake

→ More replies (3)
→ More replies (1)

15

u/bistr-o-math Feb 03 '22

And perfectly fine in a lot of languages (c, c++, Java, JavaScript)

The value of an assignment is the assigned value. So a=b will assign value of b to a, and (a=b)==b will be true.

This is also why things like a=b=c=d=1 work: will assign 1 to all of the variables a,b,c,d (first assigning 1 to d, the n assigning value of (d=1) to c, and so on

6

u/theDreamingStar Feb 03 '22

Programming sentient robots in Javascript is my new kink.

→ More replies (2)

12

u/isomorphica Feb 03 '22

That is not true. In C# you certainly can embed assignments in other expressions, such as if conditions.

For example (where result is a reference type variable and valid is a bool variable):

if/while ((result = GetResult()) != null)

if/while (valid = IsValid())

The value of an assignment expression is equal to the value of the variable after the assignment.

2

u/Ok_Blueberry_5305 Feb 03 '22 edited Feb 03 '22

Yep, I misremembered the suggestion severity. Edited to correct

1

u/theqmann Feb 03 '22

I remember reading someone elses code that had every subfunction execute inside an if statement:

if (hadError |= subFuction())
{
  // error stuff
}    
    
if (hadError |= subFuction2())
{
  // error stuff
} 
return (hadError);

Driving me crazy.

1

u/AlarmingAffect0 Feb 03 '22

Is there a valid reason to allow this?

→ More replies (2)

3

u/[deleted] Feb 03 '22

I've done it in c# on accident all the time, and it will compile and run. However, it will yell at you and ask if you meant '==' instead of '=' (see CS0665)

2

u/daneelthesane Feb 03 '22

My dumb ass is grateful for this fact, too. I make this mistake all the time. Thanks, years of working in SQL!

2

u/Ellweiss Feb 03 '22

In any case, most modern IDEs would at the very least show you a warning if you do that.

1

u/devjoel Feb 03 '22

C# program here. I was thinking the same thing lmfao. Who did this compile?!?

-1

u/SaaS_Founder Feb 03 '22

Sounds like you have no idea how the C compiler works. What makes the assignment operator different from any other operator?

1

u/ryecurious Feb 03 '22

Say what you will about Powershell, but this would never happen with -eq as the equality operator.

1

u/AlarmingAffect0 Feb 03 '22

the compiler would ask if you mean it, while simultaneously letting you do it

Something about this sounds imprudent...

1

u/RedPill115 Feb 03 '22

C#doesn't allow you to do assignments in an if condition

It's a rule added because C and C++ did, leading to errors like that in the comic.

1

u/TehMephs Feb 03 '22

This is why c# is nice. Plus you have StyleCop to bitch about all sorts of extraneous whitespaces and missing spaces between comment indicators as errors

0

u/badvok666 Feb 03 '22

The const part should not matter, its not a statement so in many languages wont compile and will be detected as writen by the ide

2

u/RickJohnson14 Feb 03 '22

"Which languages do you use?

...

  • Other: HTML