16

I have one confession.
I have never ever in 15 years of coding used switch case...
Never liked the syntax.
Am i the onlyone ?

Comments
  • 6
    Interesting! 😄
    I'm not a big fan either but I find it in some cases more convenient than multiple if-elseif blocks (e.g. with string constants or enums).
  • 3
    I only like it in Go. It accepts a multiple match case and it's more idiomatic than else if (in Go else if is very weird) .
    But yes, otherwise, I don't like it.
  • 5
    In Go and Kotlin, I prefer switch (or when) over if whenever I have multiple similar conditions.

    In many other languages, I also rarely use switches.
  • 5
    Pattern match gang rise up
  • 5
    I use it in C all the time when I check a variable for different static values with different actions because it's much easier to read than if-else chains. Not only, but especially for state machines where it's even idiomatic.
  • 1
    @sebach1
    Dude what?!
    Else if in go is as straight forward as it gets.
    No unnecessary parentheses, no Elif,elseif or whatever special keywords.
  • 0
    @metamourge Yes, I like the absence of unnecessary parentheses and the if kw reuse.
    But its fmt is horrible and that doesn't seem go-like for me. It's more idiomatic to skip the else block giving a return in the if, and that affects directly on how else if seems.

    But I think the most important thing that makes the if else very underused in Go vs other langs is that in Go there aren't (in general, when compared to the rest of langs) that much of use cases from my perspective. Maybe because of select stmt, errs as values (and switch ofc) but mostly because the Go community is always promoting "make it simple".
  • 2
    Well, time to get the holy water and purge this demon
  • 0
    @Haxk20 switch, if, vlocks?!
  • 1
    When I code in elixir I use similar syntax for pattern matching
  • 0
    I used it a few times in various langs. It has better performance I guess, but thats negligible.
  • 0
    It's one of my favs...

    I just discovered that python doesn't have it, and i was shocked

    Never used WHILE in my life
  • 1
    @momad that's what the default case is for, if desired.
  • 0
    They can be more performant than the alternatives, but I try to avoid them as they encourage your methods to do more than one role.

    Outside of factories I use them as a clue I'm doing something wrong.
  • 0
    @LMagnus I guess your code doesn't involve finite-state machines?
  • 0
    @jespersh true.
    Neither the kinda guy who would make a relational table to store "M" and "Mrs" related to "tb_users" for the "fk_title" field. I just put a varchar(5) 😂
  • 0
    I was just like you but then I started to learn Elixir, a functional language that uses pattern matching as one of its main ways to control flow. After that I started to employ switch case more often because it fundamentally changed the way how I looked at switch case / pattern marching and made me more familiar with the syntax. But in the end it all depends on what you're doing, what language your coding in and probably how others you collaborate with would write it I guess.
Add Comment