11

my frontend colleagues always keep amazing me with their create way of writing code:

```
const input = "a";

const result = {
"a": () => console.warn("A was selected"),
"b": undefined,
"c": () => console.log("hello")
}[input || "c"]?.();
```
Poor man's switch construct ... (facepalm)

Comments
  • 1
    What's the rationale behind that?
  • 2
    @kamen

    ask my coworkers :-D

    The guy doing this wanted to assign the output (in my case I'm just printing stuff though) directly into a JSON object. So he needed it to be one expression, that directly returns something.

    The proper way would be (imho) to refactor it into a separate variable and just assign the result to the JSON object's property.

    In a way they are really lazy but do stuff that is so crazy to read afterwards. It's really a who different mindset to where I'm coming from.

    One of my most important goals is always to write readable code, never smart code. It always comes back to (or others).
  • 4
    Fuck even is that?
    Ew...

    Also I really hope having the input be constant is just for testing.
  • 5
    This requires a fair degree of familiarity with the language... and negative reasoning skills. I'm impressed.
  • 4
    I think I have seen similar constructs in a way to code called branch less.

    The idea is that you do not have any branches as such?

    Or its a overzealous way of functional code ;)

    Either way its bad.

    The only excuse would be if it was extremely performance sensitive and this in some way was faster.
  • 0
    Yeah branchless.
    I like it.
  • 0
    @Ranchonyx yes, in reality is a function parameter.
  • 1
    @msdsk this guy is really creative in both positive snd negative ways.
    Most of the time though he‘s thinking way to complicated. He can mane a simple click even handler in angular seem like rocket science!
  • 2
    @Voxera @c3r38r170 It's a common branchless technique, but no one in their right mind would try to write branchless optimizations in JavaScript. What's more likely is that this dev originally learned something else but recently worked a lot with Python and they didn't know that JS has switch statements, because this is also a common technique to imitate switch statements in Python.
  • 1
    @msdsk It's not _that_ advanced, just a dictionary and an overzealous appreciation for the flexibility and simplicity of lambdas, can happen to anyone who switched to JS recently.
  • 1
    @lbfalvy Another reason to hate Python.
  • 0
    @c3r38r170 There are few properties to python that aren't reasons to hate it, the only one that comes to mind is itertools which is afaik unparallelled for an STL iterator transformation toolbox.
  • 0
    Yeah great example of a rather clever piece of code but needlessly clever and hard to read.

    You describe it perfectly: feels like it's written by someone with an entirely different way of thinking.

    I get the branchless principle but... Branches add clarity and a familiar format to most js devs
  • 0
    Not sure but doesn‘t a dictionary lookup require branching?
  • 3
    @Lensflare in the best case its just an indexed jump, as in no compare, just set the instruction pointer.

    But for that to be of any real use you need a language and compiler that generate that sort of code and no js compiler will :)

    And most other high level language compiler will not do it either.
  • 3
    Map instead of switch is a really good technique from clean code standpoint but not like this.

    The point is to replace a switch where instead of you running different parts of code in a switch, you map a string to a function call and keep the code in that function. As long as you keep that map clean and always having a function call the benefit is that is way easier to read and adding a new case means adding a new entry to that map.

    When you encounter the map and start reading it you instantly see which function interests you to change, while with switch its harder to reason with all case-code-and-break or case fallthrough. Developers spend around 80% of their time reading code so its better to make their job easier.

    The solution above was someone trying to be overly clever and missed the whole point. The point is to make the code easier to read, not to just use a map with JS hacks.
Add Comment