News for hackers. Stuff that matters.
Re:Clear is better than clever [pdf] by hotBacteria on Monday May 06, 2019 @08:56AM (#19838964)
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)

Re:Clear is better than clever [pdf] by 5trokerac3 on Monday May 06, 2019 @09:37AM (#19839254)
If 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.
Re:Clear is better than clever [pdf] by apta on Monday May 06, 2019 @10:27AM (#19839697)
Golang doesn't have enums
Re:Clear is better than clever [pdf] by 5trokerac3 on Monday May 06, 2019 @10:39AM (#19839795)
Neither does JS, but there are ways to accomplish the same thing[0].

[0] https://stackoverflow.com/questions/14426366/what-is-an-idio...

Re:Clear is better than clever [pdf] by acdanger on Monday May 06, 2019 @10:56AM (#19839997)
TypeScript does and it’s one of my favorite features.
Re:Clear is better than clever [pdf] by apta on Monday May 06, 2019 @02:49PM (#19842521)
That's not really the same. That's badly designed and error prone.
Re:Clear is better than clever [pdf] by tabtab on Monday May 06, 2019 @12:35PM (#19841033)
That 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.

Re:Clear is better than clever [pdf] by elcritch on Monday May 06, 2019 @01:14PM (#19841525)
IMHO, 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.
Re:Clear is better than clever [pdf] by tabtab on Monday May 06, 2019 @05:51PM (#19844284)
Maybe, 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.
Re:Clear is better than clever [pdf] by Roboprog on Monday May 06, 2019 @07:01PM (#19844797)
You 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.

Re:Clear is better than clever [pdf] by tabtab on Wednesday May 08, 2019 @08:58PM (#19864405)
As I mention above, it "degenerates" better in that if more code is needed in each sub-block, it's easy to add. I'm okay with syntax that could factor such out if it doesn't require overhauling the block when the starting simple pattern goes away over time. As they say, the wrong abstraction is often worse than no abstraction. Change can kick the simple pattern away.
Re:Clear is better than clever [pdf] by Roboprog on Monday May 06, 2019 @07:06PM (#19844836)
Alternative solution: put the if/else block in an “IIFE” (which Go supports, just like JavaScript) and change the assignments to returns.
Re:Clear is better than clever [pdf] by illys on Wednesday May 08, 2019 @06:02AM (#19857389)
If 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...)
Re:Clear is better than clever [pdf] by pojzon on Monday May 06, 2019 @05:02PM (#19843848)
I 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.

Re:Clear is better than clever [pdf] by brigandish on Monday May 06, 2019 @09:35PM (#19845644)
I've not heard of the supplier pattern, any chance you would direct me to a resource about it?
Re:Clear is better than clever [pdf] by fjsolwmv on Tuesday May 07, 2019 @08:31AM (#19848770)
A Supplier is when a function needs a value, and instead of providing a value as an argument, you pass in callable:

Foo(int x){return x+1} becomes Foo(Supplier x){return x() + 1}

Re:Clear is better than clever [pdf] by pojzon on Tuesday May 07, 2019 @08:59AM (#19848935)
Function that supplies value, can also encapsulate alot of data generation code. After that you can write a strategy to select which data generation algorithm (supplier) shpuld be used.

Thats my cause.

Re:Clear is better than clever [pdf] by brigandish on Wednesday May 08, 2019 @12:27AM (#19855867)
Thanks.
Re:Clear is better than clever [pdf] by pojzon on Tuesday May 07, 2019 @01:16AM (#19846805)
Im pretty sure you know what i meant, no need for nitpicking.

(Does not make me any less right)

Re:Clear is better than clever [pdf] by stevesimmons on Tuesday May 07, 2019 @02:36AM (#19847114)
Just 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.)

Re:Clear is better than clever [pdf] by brankoB on Tuesday May 07, 2019 @02:56AM (#19847187)
I, too, have no idea what a supplier pattern is. Don't think that's a very mainstream notion.
Re:Clear is better than clever [pdf] by oblio on Tuesday May 07, 2019 @04:28AM (#19847521)
I'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?
Re:Clear is better than clever [pdf] by brigandish on Tuesday May 07, 2019 @07:23AM (#19848387)
I can't nitpick what I don't know about. I know the strategy pattern, not the supplier pattern.

It sounds interesting, that was all.