why are undergrads in compsci so obsessed with switch statements

@wallhackio not an undergrad but purrsonally i just despise how if-elseif chains look

@aescling but switch statements add a bunch of indentation so for me it's picking your poison

@wallhackio @aescling Do they?

switch (some_enum) {
case VALUE1:
do_something();
break;
case VALUE2:
do_something_else();
break;
}

vs.

if (some-enum == VALUE1) {
do_something();
} else if (some_enum == VALUE2) {
do_something_else();
}

That's the same amount of indentation...

@vaporeon_ @aescling I would always do it like

switch(gorp) {
case 'norp':
console.log('norp');
break;
case 'sorp':
console.log('sorp');
break;
default:
console.log(' what the gorp???');
break;
}

@wallhackio @aescling You can switch statement on a string in JavaScripts? Even though gorp and 'norp' don't point to the same address? :psyduck:

@vaporeon_ @wallhackio you wanna see some real fucked up shit? switch (true):

  // the navbar links to every chunk of the gallery.
// the current page does not get a link.
// index 0 links to `/`.
/** @type {Node[]} */
const linksByIndex = [];
for (let i = 0; i < chunkedImages.length; i++) {
switch (true) {
case i === chunkIndex:
linksByIndex.push(li({ class: "active" }, `${i + 1}`));
break;
case i === 0:
linksByIndex.push(li({}, a({ href: "/" }, `${i + 1}`)));
break;
default:
linksByIndex.push(li({}, a({ href: `/${i + 1}.html` }, `${i + 1}`)));
}
}
Follow

@aescling @vaporeon_ I guess I understand the undergrads now because the i++ just made my eye twitch even though it literally doesn't matter

@vaporeon_ @aescling
It's "more performant" to use ++i*

*It really doesn't matter and even if this does give a teeny, marginal performance boost it's the kind of thing a modern compiler will catch for you anyway

@wallhackio @aescling If I remember correctly, in one of our courses we were told that this is something that a compiler can easily optimise

@wallhackio @aescling I was agreeing with you, I was saying "we were taught the same thing"

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.