5 ms·
My favorite (not actually horrible) switch statement trick: Instead of string x; switch (y) { case 0: x = "foo"; break; case 1: x = "bar"; break;
by jlebar 6y ago
My favorite (not actually horrible) switch statement trick:
Instead of
string x;
switch (y) {
case 0: x = "foo"; break;
case 1: x = "bar"; break;
}
try an IIFE!
string x = [&] {
case(y) {
case 0: return "foo";
case 1: return "bar";
}
}();
Once you get used to reading it, there are a bunch of advantages. Among them, you can't forget a "break", and you can't forget to assign into `x`.
- mehrdadn 6y agoI think you meant switch(y) instead of case(y)?
- lmilcin 6y agoAlways prefer simple over clever. Both statements do same thing but only one of them is simple to understand and hard to break for a novice developer (or roughly 3/4th of your team).
- mpfundstein 6y agoif your novice dev cant understand the second example, he should be fired on the spot
- Jtsummers 6y agoThey shouldn't be fired, they should be taught. And it depends on what languages they already know and which version of C/C++ they were taught. C++'s lambdas looked very strange to me even though I'd worked with C++ code for 15 years, but nothing newer than C++03 (both what I learned in school and due to code I worked on professionally just being that old). It took me a while and finally sitting down with a couple books on "Modern C++" to grok what was going on with that syntax.
- lmilcin 6y agoOh, wow, what an attitude. Did you consider that it is possible that everybody can read the second example but may need to spend more time reading it to comprehend if they haven't seen it before and may make mistake interpreting the code? Did you consider not every company is Intel or Google and there is a lot of companies that can't get "top 1% talent"? Readability is about making it easy to understand the code, without putting effort into reading. Readability is important because code is written once and red many times by people who may need to read a lot of code and don't want to spend much time trying to understand every line of it.
- kortilla 6y ago“Novice”
- KeytarHero 6y agoIn the case of the lambda version, the compiler will fail if you forget a break statement or don't handle a possible case. Plus this way you can make x const, which can also prevent breaking other code further down. It seems to me the extra safety it provides actually makes it harder for a novice to break your code? Unless you mean strictly in the sense of "harder to break compilation", but I'm not sure that's a good thing (insert obligatory mention of Rust here)
- NeutronStar 6y agoThe code is going to get executed way more times than it is read.
- KeytarHero 6y agoThis also lets you make x const
- Gaelan 6y agoWait, what language is this? That doesn't look like C.
- dyingkneepad 6y agoThat's a C++ lambda.
- Jtsummers 6y agoC++ lambda. You can basically do this: auto succ = [](int i) { return i + 1; }; To make variables in the surrounding scope visible (i.e., create a closure) you have to specify that they're available and how. Grabbing a reference to all variables in scope you could do: int x = 0; auto inc_x = [&] () { return x++; }; This makes x available via reference so it can be modified. If you just want the value: int x = 0; auto always_one = [=] () { return x + 1; }; And you can constrain which variables are visible within the lambda: int x,y; auto foo = [&x] (auto n) { x += n; }; The return type can be determined by the compiler, or they can be made explicit (I chose not to). The first examples all returned integers, the last one has void return type.
- dyingkneepad 6y agoLambdas are a C++ thing, not C. If 0 and 1 are the only possible values, you can be much much more elegant: string x = y ? "bar" : "foo"; Else: string x = (y == 1) ? "bar" : (y == 0) ? "foo" : "";
- jolmg 6y ago> you can't forget a "break", and you can't forget to assign into `x`. Same benefits that you also get with: string x = y == 0 ? "foo" : y == 1 ? "bar" : "baz" ; Only, using ?: additionally forces you via syntax to specify what x's value should be when all prior conditions fail. In both your examples, x is undefined when y is neither 0 nor 1.
- im3w1l 6y agoAn autoformatter will ruin the way you laid it out and make it look awful.
- userbinator 6y agoThen stop using or pandering to stupid tools.
- saagarjha 6y agoThat's a mean way to refer to your coworkers…
- jolmg 6y agoThe formatting is besides the point.
- userbinator 6y agoI don't see how either of those is simpler than string x = y ? "foo" : "bar"; or if y can take more values than 0 or 1, then that strongly suggests it should become a table lookup: string x = some_strings[y];
- usefulcat 6y agoI think the point is more the general technique, not this particular usage of it. Regarding the ternary, nested ternary expressions aren't the most readable thing. Regarding table lookup, sure but only if all values of y are sequential integers.