Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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). -
sebach1845yI 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. -
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. -
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.
-
@sebach1
Dude what?!
Else if in go is as straight forward as it gets.
No unnecessary parentheses, no Elif,elseif or whatever special keywords. -
sebach1845y@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". -
Quirinus7525yI used it a few times in various langs. It has better performance I guess, but thats negligible.
-
vortex47675yIt's one of my favs...
I just discovered that python doesn't have it, and i was shocked
Never used WHILE in my life -
LMagnus20575yThey 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. -
@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) 😂 -
Leya17715yI 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.
Related Rants
I have one confession.
I have never ever in 15 years of coding used switch case...
Never liked the syntax.
Am i the onlyone ?
rant
if else
switch_case