Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,177,039 members, 7,899,699 topics. Date: Wednesday, 24 July 2024 at 05:38 PM

The Switch Statement Is Redundant - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / The Switch Statement Is Redundant (1307 Views)

Artificial Intelligence (AI) may Make Programmers Redundant / Validation Statement In Python / What Is Wrong With My SWITCH Statement(javascript) (2) (3) (4)

(1) (Reply) (Go Down)

The Switch Statement Is Redundant by asalimpo(m): 2:34am On Dec 03, 2017
Ok. I know i'll get flamed for this. But just do a lil thinking on this first.
Switch is cultural baggage that most new languages feel they have to support to not look odd.
So the main impetus for perpetrating this feature is conformity.
"Why doesnt this language have a switch statement " + puzzled look+ angry loook+ thinking thinking thinking
Decision: dump the language!!
Quick way to get your product ignored in the open minded world of programming is to be different.
So much for programmers being logical and open minded.
The better languages are dead in the dumps . Where is lisp haskell and others that were so well designed and ahead of their time?
ok. back to the topic:
a switch statement is just an if statement in disguise. You know this if you've been programming for a while.
So why call it "switch" and spawn it off as a new concept entirely? y not expand the if function/structure whatever to do what "switch does"?
That way the conceptual load will be lighter on the learner.

By the same token, there's nothing like if-else, if-else-else2 etc.
There's just one if.
That's it.
Most main stream languages are just poorly designed and these poor designs have been shovelled down our throats for so long that we think they're gospel.
In many books, you're told that if is branching.
Then, you're introduced to if.
then more complex if-oxymoronic. There's only IF. ONLY ONE. at athe compiler execution level, there's only1!
because, computers operate in only one of two states - it all resolves to either ON or OFF, 1 or 0,true or false. LEFT OR RIGHT.


Re: The Switch Statement Is Redundant by FrankLampard: 8:18am On Dec 03, 2017
Logically a switch statement is faster than a nested if statement with many conditions. A switch statement is faster when you are constantly checking for constant values.
Re: The Switch Statement Is Redundant by BlueMagnificent(m): 12:10pm On Dec 03, 2017
Don't forget that switch statements have fall-through if there is no break in the case of c/c++ . That comes in handy when u want to handle a condition and still want another block of code which is for another condition to run. Can't happen with if undecided
Re: The Switch Statement Is Redundant by 4dor: 12:21pm On Dec 03, 2017
Promises and Async/Await tongue
Re: The Switch Statement Is Redundant by asalimpo(m): 12:26pm On Dec 03, 2017
You're all saying the same thing.
Computer scientists say all programs can be written with just branching ,iteration and one other construct.
switch is conceptually,
<condition> <executable>
look at this switch statement that checks numbers:

case 1: <do semething>
case 2: <do something>
case 3: <do something else>
default: <do default>

look at an typical if scenario
else if(x==2){}
else if(x==3){}
so switch is syntactically shorter and more concise that the typical if.

but theyre conceptually the same.
So why not make an if that takes booleans and other types?

if(true){} -- it takes a boolean as a condition variable.

!: <do something>
2: <do something else>
3: <do something else>

|_______ isnt that what a switch statement is trying to do?!
but students learn only one if- not if that takes booleans,then if that takes non booleans aka switch statement.
No conceptual fragmentation.
Re: The Switch Statement Is Redundant by romme2u: 8:42pm On Dec 03, 2017

my brother the lord is your strength
Re: The Switch Statement Is Redundant by AbiZeus: 12:05am On Dec 04, 2017
It's very useful when done properly...

Switch "j":
Case " a":
Case "b":

Imagine aAfunc(y){
Switch y:
Case " ai":
Case "yb":

Think of how many if and else is Afunc, bFunc cFunc follow this paradigm...
Take a look at Redux Methodology ..... Check our react redux or ngrx angular x with observables (my love).....
Then you will truely understand why some things are better like that.... Using switch with binary logic strategy will even make complex if and else , switch small and beautiful.... If you want to go further and prove never use else statement on If statements and return because if the If statement is true it still checks the else statements (I think in some languages) the solution to that is after executing the logic in the scope you return a value or null depending on the logical flow so that if no conditions are met you can handle that like a Switch fallthrough....
I.e func by(x){
if (x == 1) {

if ( x == 2) {

// no conditions met now handle the fallthrough

throw error or dosomeC()
// get the idea

So switch statement is not redundant....
Re: The Switch Statement Is Redundant by asalimpo(m): 12:28am On Dec 04, 2017
handle the fall through you say?
just tack on a case that isnt matched. that's it.
switch is syntactic sugar for if. nothing else.
So whatever switch is doing is if underneath.

Your talk of switch being less verbose is what i addressed earlier.
switch is conceptually if.
so the if syntax shold be expanded to handle non booleans.

instead of if(<boolean>,<passStatement> , <failStatement>wink

if can be made more robust:
if(<distinctiveTypeArgument>,<condition-and executablePairs>wink

if u're using numbersl
//the pairs gets listed
//<condition> : <executable>
1: <statement to execute if arg is 1>
2: <statement to execute >
3: <....>
.. and so on and so forth
<ifNoMatchFound> : <statementToExecuteIfNoMatchFound>


Isnt this what a switch statement is doing?
I've demonstrated that if can do the same, without rewording the syntax.
NO unnecessary conceptual overload.
The syntax and concepts are not splintered off unncessarily.
The whole syntax is uniform and streamlined.

When you're if'ing with booleans, you know you're doing so.
when the condition variables isnt a boolean, no problem,he if statement is robust enough to handle that.
You dont go the roundabout route using a disguised if ,called a 'switch' statement.
Re: The Switch Statement Is Redundant by 4kings: 9:35am On Dec 05, 2017
Interesting... smiley
Re: The Switch Statement Is Redundant by Nobody: 12:21am On Dec 07, 2017
Personally, I avoid conditional statements when I can.

As a rule, it is the first pattern I follow.

I believe JS has

try {}

and python has the


Take this example:

A man (interpreter/compiler) is always standing at the door. Waiting for orders.

you come to the door, and ask for water, and it is available.

Using conditions
you: do you have water.
man: yes
you: get me a glass

Using try
you: get me water
man: here you go.

(1) (Reply)

What Is Programming And Why Should I Learn It? / Are You Interested In A Music Start-up / In Need Of A Good Wordpress Developer.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 25
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.