5 ms·
You can write it without resorting to one-line conditionals: if(cond1){ myVar = 1; } else if(cond2 && cond3){ myVar = 10; } else if(cond2 && !c
by hotBacteria 7y ago
You can write it without resorting to one-line conditionals:
if(cond1){
myVar = 1;
}
else if(cond2 && cond3){
myVar = 10;
}
else if(cond2 && !cond3){
myVar = 100;
}
else {
myVar = 4;
}
But when I encounter this kind of situations I have other problems than code formatting anyway:
with 3 conditions you have 2^3 possibilities to check: are you really really sure (!cond1 && !cond2 && cond3) should give you defaultValue ? Do you really want 1 even if !cond2 ? etc...
When possible these conditions should be avoided in the first place (depending on context of course)
- 5trokerac3 7y agoIf you're running through that many conditionals, it may be clearer to work them into a state enum that you can use with a switch.
- apta 7y agoGolang doesn't have enums
- 5trokerac3 7y agoNeither does JS, but there are ways to accomplish the same thing[0]. [0] https://stackoverflow.com/questions/14426366/what-is-an-idiomatic-way-of-representing-enums-in-go https://stackoverflow.com/questions/14426366/what-is-an-idio...
- tabtab 7y agoThat makes code formatting inconsistent because many prefer the other way. The VB.Net style is more compact and readable in my opinion: if cond1 then myVar = 1 else if cond2 and cond3 then myVar = 10 else if cond2 and not cond3 then myVar = 100 else myVar = 4 end if The VB.Net style also makes it easier to identify mismatched blocks because the "enders" are named differently. (One can also put the assignment on the same line as the conditional, but I generally don't recommend it.) Some may complain it's slightly verbose, but few if any will claim it's not clear.
- elcritch 7y agoIMHO, better but it’s still confusing when you have multiple places where you name the variable. Functional programming style forces you to be more explicit and pushes you to separate out the assignment expression. In Elixir I’d do: myVar = cond do cond1 -> 1 cond2 && cond3 -> 10 cond2 && !cond3 -> 100 true -> 4 end ML languages have similar constructs.
- tabtab 7y agoMaybe, but that misaligns the values. I'd rather see them lined up. And often more complex logic may be added to the sub-blocks such that the simple pattern goes away. Code should be able to "degenerate well", meaning it shouldn't require lots of rework when the initial pattern fades or changes in the future. It's one of the reasons I often use If/else instead of switch/case statements.
- deleted 7y ago[deleted]
- Roboprog 7y agoYou assigned “myVar” in every branch, right? Let me reread that again to make sure you really are assigning myVar in every case. That’s a problem. One I’ve seen all too often, but a problem, nonetheless.
- 7y ago
- deleted 7y ago[deleted]
- pojzon 7y agoI think its a poor example. How often you are going to write something like that in RL scenario? In case of more complex data generation you could use supplier pattern and combine it with strategy. No need for any if else statements at all. Sure it requires a lot more of work but is future proof and clear.
- brigandish 7y agoI've not heard of the supplier pattern, any chance you would direct me to a resource about it?
- pojzon 7y agoIm pretty sure you know what i meant, no need for nitpicking. (Does not make me any less right)
- stevesimmons 7y agoJust maybe he genuinely doesn't know what you mean. I mainly use Python and have no idea what you are talking about. (You might consider reflecting on your tone and message. It presupposes bad faith on the other party, is dismissive and condescending, and you are probably less right than you think you are.)
- brankoB 7y agoI, too, have no idea what a supplier pattern is. Don't think that's a very mainstream notion.
- oblio 7y agoI'm not OP, but I don't know what you mean... Googling it leads me to Supplier in Java 8, which is probably not what you meant?
- brigandish 7y agoI can't nitpick what I don't know about. I know the strategy pattern, not the supplier pattern. It sounds interesting, that was all.
- illys 7y agoIf you are worried with all the cases, you can very easily display your code as a truth table with nearly no overhead in this way: myVar = (!cond1) && (!cond2) && (!cond3) ? myValue_0 : // case 0 0 0 blabla (!cond1) && (!cond2) && (cond3) ? myValue_1 : // case 0 0 1 bla (!cond1) && (cond2) && (!cond3) ? myValue_2 : // case 0 1 0 blablabla (!cond1) && (cond2) && (cond3) ? myValue_3 : // case 0 1 1 (cond1) && (!cond2) && (!cond3) ? myValue_4 : // case 1 0 0 (cond1) && (!cond2) && (cond3) ? myValue_5 : // case 1 0 1 (cond1) && (cond2) && (!cond3) ? myValue_6 : // case 1 1 0 (cond1) && (cond2) && (cond3) ? myValue_7 : // case 1 1 1 defaultValue; // uninteresting default (null, -1...)