Timeline for Are assignments in the condition part of conditionals a bad practice?
Current License: CC BY-SA 4.0
21 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 31, 2019 at 4:29 | comment | added | Stephen C | I would just like to know what makes your "definitive answer" definitive? :-) | |
| S Aug 31, 2019 at 3:27 | history | suggested | TigerTV.ru | CC BY-SA 4.0 |
just a typo fixed
|
| Aug 30, 2019 at 22:13 | review | Suggested edits | |||
| S Aug 31, 2019 at 3:27 | |||||
| Oct 31, 2016 at 20:24 | comment | added | Christophe | +1 "Considering that debugging is twice as hard as writing a program in the first place, if you're as clever as you can be when you write it, how will you ever debug it?" B.W.Kernighan | |
| Oct 31, 2016 at 12:03 | comment | added | Tulains Córdova | @JopV. It's a truth, nonetheless. | |
| Oct 31, 2016 at 11:39 | comment | added | jobukkit | -1 for Computer time is cheaper now than programmer's time, this is the mentality that causes computers to become slower after every major software update. | |
| Oct 30, 2016 at 21:04 | comment | added | scrowler | I'd love to know whether this concept will eventually kill code golf | |
| Oct 30, 2016 at 18:47 | comment | added | Diane M | @LukeA.Leber What ? An IDE ? Please ! explainxkcd.com/wiki/images/0/0f/real_programmers.png | |
| Oct 30, 2016 at 18:22 | comment | added | Diane M | @DanielJour Lol, I thought about that exact counter-example a few hours after writing the comment, haha. This one is OK, for a couple of reason : 1- C does not offer any kind of equivalent foreach stucture and 2- you are just assigning the loop variable here, just like you would in a for loop. Nothing like writing inside an array. | |
| Oct 30, 2016 at 17:20 | comment | added | hvd |
@Malcolm As far as I'm concerned, not much, but then, I don't see too much of a problem with assignments in loop conditions either. As far as some others are concerned, it's a disguised goto and makes it harder to reason about code correctness. Which I think is a valid point of view, even if it's one I don't share.
|
|
| Oct 30, 2016 at 17:15 | comment | added | Malcolm | @hvd So what's wrong with a break statement? | |
| Oct 30, 2016 at 16:07 | comment | added | hvd |
@Malcolm The only ways to write it outside of the loop condition have code duplication, the break; statement, or a separate function. Extracting it into a function should be fine, but the other two are not generally considered good practice, at best they'd be considered as less bad than an assignment inside the loop condition.
|
|
| Oct 30, 2016 at 15:50 | comment | added | Malcolm | @DanielJour Personally, I don't write this in one line anyway, both assignment and the check will be a separate line in the loop. It doesn't bother me to write a couple of additional lines to avoid the assignment in the condition once, and if it's more than once, I would extract it into a function anyway. | |
| Oct 30, 2016 at 5:59 | comment | added | Daniel Jour |
@ArthurHavlicek I agree with your general point, but code with side effects in conditionals is really not so uncommon: while ((c = fgetc(file)) != EOF) as the first that comes to my mind.
|
|
| Oct 30, 2016 at 5:49 | comment | added | Luke A. Leber |
@Kevin - Modern IDEs flag situations when there is a potential problem because there is a potential problem. This could (for example) be because of missing parenthesis (if(a = someFunction() == true)). After properly parenthesizing the expression, no warning is displayed. This eliminates all would-be confusion regarding the order of evaluation in such an expression.
|
|
| Oct 30, 2016 at 5:32 | comment | added | Kevin | @Luke: "My IDE can clean up X, therefore it's not a problem" is rather unconvincing. If it's not a problem, why does the IDE make it so easy to "fix?" | |
| Oct 30, 2016 at 4:17 | comment | added | Luke A. Leber | @ArthurHavlicek - Any decent IDE will flag such an assignment, thus explicitly highlighting the suspect area, making it less likely to mess up. Great IDEs even have a one-click refactoring mechanism. | |
| Oct 29, 2016 at 21:06 | comment | added | Diane M |
@MilesRout There is. There is something wrong with any code having a side effect where you don't expect it, i.e. passing function arguments or evaluating conditionals. Not even mentionning that if (a=b) can easily be mistaken for if (a==b).
|
|
| Oct 29, 2016 at 20:31 | comment | added | mrr | Very pithy ideology, but the fact remains that there's nothing wrong with assignment in conditionals. It's far preferable to early exit from a loop or duplicating code before and inside a loop. | |
| Oct 29, 2016 at 19:23 | history | edited | Tulains Córdova | CC BY-SA 3.0 |
edited body
|
| Oct 29, 2016 at 19:03 | history | answered | Tulains Córdova | CC BY-SA 3.0 |