3
nitwhiz
8y

!rant

Aside from performance concerns.. which one would you prefer for an optional callback?:b

I always use B, but i'm kinda interested in what you guys say.

Comments
  • 4
    I see that === 'function' is waay more logical in A. Lets go w/ that instead.😅

    #nosleep
  • 4
    Kinda of like B, perlish.
    A if I had to maintain it though
  • 2
    B appeals to code hipsters like me
  • 1
    I prefer B for anything other then a function/callback, but if you change the type check to function you can make sure it is a function before executing it
  • 2
    Can you not `callback && callback()`?
  • 0
    @fattymiller thats pretty smart, too, nice idea!

    Even tho this might be hell for others who have to work w/ the code..^^
  • 0
    A

    It's not about compacting the code as much as possible. You should code so it's possible to skim through and understand.

    These alternatives have pretty much no performance impact either way, so it's more showing off than leaving readable code after you.

    Hipster code. Stick with readability as if the person inheriting your code is a crazy serial killer.
  • 0
    @viking8 absolutely, of course.

    But readability is a relative thing. Imagine like 2 or 3 ifs and a loop where this callback gets called in. Imho, a compact line is much more readable there as another if-thing.

    You should always go for readability, i never said sth else, but i was just curious how others would compress such an often-happening, easy thing.(:

    I wouldn't call it hipster code right away, perl existed ages ago and introduced magically working oneliners.
  • 1
    I'd definitely go with C. It's concise, expressive and it doesn't involve any "wait what was that again, oh yeah, right..." Moment when you go back to it months later. Especially B I think could seriously trip you up, it's a beautiful line of code, but maintenance is probably a massive pain.
Add Comment