Timeline for Returning true/nothing instead of true/false?
Current License: CC BY-SA 4.0
32 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 5, 2020 at 10:05 | comment | added | john c. j. | Related discussion: wiki.c2.com/?ReturnBooleanEvaluations | |
| Jun 4, 2020 at 9:00 | history | tweeted | twitter.com/StackSoftEng/status/1268467563203264514 | ||
| May 30, 2020 at 23:31 | comment | added | Bernhard Barker | Depending on the language and tool, code checking tools might raise a warning or error if you try to return "nothing" (as in have no return statement, or an empty one) if the function returns other things, despite the code being able to run without errors. | |
| May 30, 2020 at 23:12 | comment | added | Bernhard Barker |
Most, if not all, examples where it's valid to sometimes return a value and sometimes return "nothing", the "nothing" you're returning would in fact be null, None, nil, NULL or equivalent. So returning true/"nothing" is comparable to returning a Boolean true/null in Java (even if Java insists that only false is false, unlike some other languages, including C++, for what it's worth).
|
|
| May 30, 2020 at 23:04 | comment | added | Robbie Goodwin | More usefully, how might that not be OK? How could it be hard to see that “I cannot eat meat” and “I can not eat meat” mean the same; “I can eat no meat” almost exactly the same. Do they, or not? "Can not eat meat" is in your context ambiguous - it clearly does not mean “able to abstain from meat”. Duh! “Can eat no meat" is ambiguous how, exactly? My suggestion is that there’s no ambiguity at all. | |
| May 30, 2020 at 19:27 | answer | added | hildred | timeline score: 2 | |
| May 30, 2020 at 18:32 | comment | added | user949300 | In your specific case of Javascript, ESLint has a rule for this: eslint consistent-return. | |
| May 30, 2020 at 17:36 | answer | added | Frank Hopkins | timeline score: 0 | |
| May 30, 2020 at 17:11 | history | protected | gnat | ||
| May 30, 2020 at 16:58 | comment | added | Justin Time - Reinstate Monica |
As an aside, this behaviour can actually be somewhat observed in C++, primarily in constructs such as the ol' safe bool idiom. It returns either a truthy (pointer-to-member-function) or "nothing" (NULL) value, with the "nothing" being falsy. Not entirely the same as this, but close.
|
|
| May 30, 2020 at 15:34 | answer | added | ruds | timeline score: 1 | |
| May 30, 2020 at 15:33 | answer | added | Anon | timeline score: 2 | |
| May 30, 2020 at 14:53 | answer | added | sphoenix | timeline score: 0 | |
| May 30, 2020 at 14:31 | answer | added | MT0 | timeline score: 1 | |
| May 30, 2020 at 13:09 | comment | added | Boann |
To truly return nothing, write return foo(); – that won't return anything!
|
|
| May 30, 2020 at 13:05 | comment | added | Flater | Somewhat countering a lot of comments and answers here, assert logic specifically uses the "true or assert exception" approach by intention. It may not be as common in concrete business logic but that doesn't mean it doesn't serve a specific purpose. | |
| May 30, 2020 at 9:09 | answer | added | OliverRadini | timeline score: 1 | |
| May 30, 2020 at 8:13 | comment | added | Bergi |
No, it's not OK, and you should write function foo() { return (a === b); } instead.
|
|
| May 30, 2020 at 5:29 | comment | added | Doc Brown | "I think it is better to follow the way how it works in serious languages like C++ and Java" - sorry, but you are confusing "serious" with "type safe". Weakly typed languages have their place and are in no way "less serious" than type safe languages. When you post here question, I heavily recommend to avoid such nonsense ratings about programming languages, otherwise don't be astonished about getting lots of downvotes and close votes. | |
| May 30, 2020 at 1:10 | history | became hot network question | |||
| May 29, 2020 at 19:59 | history | edited | john c. j. | CC BY-SA 4.0 |
edited body
|
| May 29, 2020 at 18:37 | answer | added | Jörg W Mittag | timeline score: 7 | |
| May 29, 2020 at 18:25 | vote | accept | john c. j. | ||
| May 29, 2020 at 17:58 | answer | added | Thomas Owens♦ | timeline score: 21 | |
| May 29, 2020 at 17:44 | comment | added | Andres F. | @Steve Wouldn't that question be is it ok to return truthy/falsy values instead of a boolean? I understood in this case "nothing" doesn't mean false or a falsy value, otherwise the question would have been framed like that. I might be wrong, of course :) | |
| May 29, 2020 at 17:43 | comment | added | Andres F. | @johnc.j. What is their justification? Maybe it makes sense (though I don't see how, a priori) | |
| May 29, 2020 at 17:33 | comment | added | john c. j. | @AndresF. Yes, this is exactly what I think. But I see a lot of people who have better coding skills than mine and who don't follow this way. And this is the reason of my doubts: are they wrong? | |
| May 29, 2020 at 17:32 | comment | added | marstato | You can't return nothing in JavaScript. Your first example returns undefined. | |
| May 29, 2020 at 17:31 | comment | added | Steve | In general across many languages, the question may be whether the operators handle false in the same way as nothing - notoriously, SQL is an example of a language where nothing (or null) is not equal to either true or false (and null does not equal null, either!). If I were you, if nothing has the same semantic meaning as false in your program, then I'd use the explicit value false, in accordance with how most people understand boolean values to work. | |
| May 29, 2020 at 17:26 | review | Close votes | |||
| Jun 4, 2020 at 3:01 | |||||
| May 29, 2020 at 17:23 | comment | added | Andres F. | What is the conceptual "type" which accepts values "true" and "nothing". It's certainly not a boolean. What is the intent? Why not a true boolean with true/false? | |
| May 29, 2020 at 17:10 | history | asked | john c. j. | CC BY-SA 4.0 |