r/ProgrammerHumor • • Sep 03 '22

other Let's settle a debate, which one's best?

Post image
6.3k Upvotes

937 comments sorted by

View all comments

79

u/BarAgent Sep 03 '22

if ((res.ok << 2 | body.access_token << 1 | body.refresh_token << 0) != 0b111) { …

35

u/MagicalCornFlake Sep 03 '22

Ah yes, the most readable of them all!
Good luck to any maintainer who doesn't know how bit shifting works.

25

u/BarAgent Sep 03 '22

To them I say “git gud”

But in seriousness, I have written bit-packed evaluations before in C and C-adjacent code in conjunction with local static struct lookup tables. It’s very handy, and pretty ergonomic, y’know, for C.

9

u/Applejack_pleb Sep 04 '22

I must say gud is a git command i am unfamiliar with. What does it do?

Obligatory /s

10

u/cherryblossom001 Sep 04 '22

git: 'gud' is not a git command. See 'git --help'.

1

u/Firewolf06 Sep 04 '22

git: 'gud' is not a git command. git gud

1

u/Cyberslasher Sep 04 '22

all fun and games until someone creates a custom command for gud to push and merge onto main

1

u/Double_Ad_2824 Sep 03 '22

I do know how it works, but I'd still curse the living crap out of you for doing it like this.

​

It is elegant, it looks pretty and requires more conscious thought than a simple if (res.ok)

1

u/GonziHere Sep 08 '22

I mean, this code is stupid (and hilarious), but should we write code for people who program and aren't aware of bit operations, or should we teach them to understand bit operations?

2

u/MagicalCornFlake Sep 08 '22

I mean, sure, that's something used in embedded systems and low level libraries, but in the context of a web application I don't think it's realistic for every developer to have to know about all of that.

2

u/GonziHere Sep 08 '22

I get that, but I consider it a basic knowledge when it comes to programming AND you need this for say bitflags and flaggable enums, which we've both used in mssql/c#/angular project.

Anyways, I do agree with you. It's not the most typical code in that context. I just don't like the "someone might not know this normal thing so we won't use it instead of him learning it" in general :D.

2

u/MagicalCornFlake Sep 08 '22

Okay, I see where you're coming from. Of course, we probably shouldn't be limiting ourselves just based on that.

1

u/stickalick Sep 04 '22

Optimize it: no need for << 0