Current Events > There are zero good reasons for an assignment operator to have a return value.

Topic List
Page List: 1
RetuenOfDevsman
03/13/24 10:09:48 AM
#1:


I know you C guys like to do dumb crap like

if (butt = read_a_file_or_some_bull()) {
printf("hurr I so clever");
}

But everyone is laughing you for such bad practice.
... Copied to Clipboard!
Were_Wyrm
03/13/24 10:10:48 AM
#2:


Damn I get in trouble for not doing that...

---
I was a God, Valeria. I found it...beneath me. - Dr. Doom
https://i.imgur.com/0EJvC4l.jpg
... Copied to Clipboard!
Tyranthraxus
03/13/24 10:15:06 AM
#3:


It's a holdover from before void was a thing. People just "return 0'd" everything. Void still technically does that but now the compiler allows you to not type the return statement if your function is void.

Everything returns a value at the end of the day. Even your exe file returns something to the command shell.

---
It says right here in Matthew 16:4 "Jesus doth not need a giant Mecha."
https://i.imgur.com/dQgC4kv.jpg
... Copied to Clipboard!
Sephiroth_C_Ryu
03/13/24 10:19:08 AM
#4:


*Unleashes a Python on the topic*

*Is disappointed when Python merely begins talking about some kind of unexpected inquisition and bonking coconut halves together, and remembers that language was named after a comedian*


---
I am the Hunter of Topics. My post never fails to kill its prey.
*pounces* Nyaa!
... Copied to Clipboard!
RetuenOfDevsman
03/13/24 10:21:23 AM
#5:


Tyranthraxus posted...
It's a holdover from before void was a thing. People just "return 0'd" everything. Void still technically does that but now the compiler allows you to not type the return statement if your function is void.

Everything returns a value at the end of the day. Even your exe file returns something to the command shell.
Maybe in some languages. But in decent languages, using the assignment operator as a conditional will cause a compile-time error.
... Copied to Clipboard!
kirbymuncher
03/13/24 10:25:04 AM
#6:


this reminds me of how people say when you actually want to compare them, you should put them in the other order
if (read_a_file_or_some_bull() == butt) {

so that you get a compile error if you accidentally type only one equals sign instead of just getting a running but broken program. same with comparisons to null

---
THIS IS WHAT I HATE A BOUT EVREY WEBSITE!! THERES SO MUCH PEOPLE READING AND POSTING STUIPED STUFF
... Copied to Clipboard!
Tyranthraxus
03/13/24 10:25:53 AM
#7:


RetuenOfDevsman posted...
Maybe in some languages. But in decent languages, using the assignment operator as a conditional will cause a compile-time error.

That's just the default behavior. You can always change the compiler to cause compile time errors but that's generally more trouble than it's worth.

If you like compile time errors I suggest checking out Ada. That shit won't compile unless it's perfect.

---
It says right here in Matthew 16:4 "Jesus doth not need a giant Mecha."
https://i.imgur.com/dQgC4kv.jpg
... Copied to Clipboard!
Tyranthraxus
03/13/24 10:27:41 AM
#8:


kirbymuncher posted...
this reminds me of how people say when you actually want to compare them, you should put them in the other order
if (read_a_file_or_some_bull() == butt) {

so that you get a compile error if you accidentally type only one equals sign instead of just getting a running but broken program. same with comparisons to null

"Yoda notation" is actually good practice.

Instead of saying if (x == 5) you should say if (5 == x)

---
It says right here in Matthew 16:4 "Jesus doth not need a giant Mecha."
https://i.imgur.com/dQgC4kv.jpg
... Copied to Clipboard!
RetuenOfDevsman
03/13/24 10:28:02 AM
#9:


Tyranthraxus posted...
That's just the default behavior. You can always change the compiler to cause compile time errors but that's generally more trouble than it's worth.

If you like compile time errors I suggest checking out Ada. That shit won't compile unless it's perfect.
Ada has been my favorite for almost a decade. I actually taught a class on it last year.
... Copied to Clipboard!
kirbymuncher
03/13/24 10:29:46 AM
#10:


Tyranthraxus posted...
"Yoda notation" is actually good practice.
I agree with this but only because imo the language itself is fundamentally broken, it shouldn't accept that sort of thing in an if statement anyway

---
THIS IS WHAT I HATE A BOUT EVREY WEBSITE!! THERES SO MUCH PEOPLE READING AND POSTING STUIPED STUFF
... Copied to Clipboard!
Tyranthraxus
03/13/24 10:33:12 AM
#11:


kirbymuncher posted...
I agree with this but only because imo the language itself is fundamentally broken, it shouldn't accept that sort of thing in an if statement anyway

It's a little more complicated than that. It's mostly an issue with scripting than compiled languages.

https://knowthecode.io/yoda-conditions-yoda-not-yoda

---
It says right here in Matthew 16:4 "Jesus doth not need a giant Mecha."
https://i.imgur.com/dQgC4kv.jpg
... Copied to Clipboard!
RetuenOfDevsman
03/13/24 10:39:12 AM
#12:


Tyranthraxus posted...
"Yoda notation" is actually good practice.

Instead of saying if (x == 5) you should say if (5 == x)
It's good practice but it's also a solution to a really dumb problem. :P

But yeah, when I have to do c/cxx at work or for low level functions, I try to remember it.
... Copied to Clipboard!
kirbymuncher
03/13/24 10:41:54 AM
#13:


I don't see how scripting vs compiled really makes a difference here. Like, all the examples in that link are talking about doing accidental assignment in an if statement when really the solution is just that the language itself should not permit assignment in the condition of an if statement. or at the very least, if it does, it should use an unusual/unique syntax for that, like how python will not allow
if (x = 5):
do thing

but will allow
if (x := 5):
do thing

why is the code block on this site so bad lol it doesn't even keep whitespace that's like 80% the purpose of a code block in the first place

---
THIS IS WHAT I HATE A BOUT EVREY WEBSITE!! THERES SO MUCH PEOPLE READING AND POSTING STUIPED STUFF
... Copied to Clipboard!
Tyranthraxus
03/13/24 10:49:29 AM
#14:


kirbymuncher posted...
I don't see how scripting vs compiled really makes a difference here. Like, all the examples in that link are talking about doing accidental assignment in an if statement when really the solution is just that the language itself should not permit assignment in the condition of an if statement.

Because in strongly typed languages, attempting to assign a literal to a function always results in compile time errors. A script will let you do it and then crash later when $file.fullname doesn't exist because you accidentally made $file a string 10 lines ago.

---
It says right here in Matthew 16:4 "Jesus doth not need a giant Mecha."
https://i.imgur.com/dQgC4kv.jpg
... Copied to Clipboard!
kirbymuncher
03/13/24 11:03:25 AM
#15:


Tyranthraxus posted...
Because in strongly typed languages, attempting to assign a literal to a function always results in compile time errors. A script will let you do it and then crash later when $file.fullname doesn't exist because you accidentally made $file a string 10 lines ago.
that's not really inherent to scripting vs compiled, though. like I just gave the example that python does not let you assign in conditionals, so even if you typo there is no danger of accidentally overwriting values or functions. there's no reason other scripting languages couldn't do the same


---
THIS IS WHAT I HATE A BOUT EVREY WEBSITE!! THERES SO MUCH PEOPLE READING AND POSTING STUIPED STUFF
... Copied to Clipboard!
RetuenOfDevsman
03/13/24 11:04:04 AM
#16:


Tyranthraxus posted...
Because in strongly typed languages, attempting to assign a literal to a function always results in compile time errors. A script will let you do it and then crash later when $file.fullname doesn't exist because you accidentally made $file a string 10 lines ago.
That's not really fundamental to the way interpreted languages or the concept of a script works though. It's just a common paradigm among the more popular languages.
... Copied to Clipboard!
Tyranthraxus
03/13/24 11:28:54 AM
#17:


kirbymuncher posted...
that's not really inherent to scripting vs compiled, though. like I just gave the example that python does not let you assign in conditionals, so even if you typo there is no danger of accidentally overwriting values or functions. there's no reason other scripting languages couldn't do the same
(Most) Scripts are evaluated at runtime. I could put this gamefaqs post into a script and it will still execute every previous valid line. That's where you run into this problem. Even if a script "didn't allow assignment conditionals" the script would just break there instead of 10 lines later which is what the reversed conditional accomplishes.

---
It says right here in Matthew 16:4 "Jesus doth not need a giant Mecha."
https://i.imgur.com/dQgC4kv.jpg
... Copied to Clipboard!
kirbymuncher
03/13/24 11:37:20 AM
#18:


Tyranthraxus posted...
(Most) Scripts are evaluated at runtime. I could put this gamefaqs post into a script and it will still execute every previous valid line.
this is also sort of untrue. for example, if you have a long javascript script with a syntax error at the bottom. eg (pay attention to the unclosed parentheses)

do things
do more things
if (x = 5 {
do even more things
}
then when you try to run it you will get a syntax error, and none of the script will run at all even though the offending line is near the bottom. there is no reason that assignment in conditionals could not be given the same treatment

---
THIS IS WHAT I HATE A BOUT EVREY WEBSITE!! THERES SO MUCH PEOPLE READING AND POSTING STUIPED STUFF
... Copied to Clipboard!
Topic List
Page List: 1