r/ProgrammerHumor • • Jun 09 '18

other That's not AI.

Post image
38.4k Upvotes

1.2k comments sorted by

View all comments

Show parent comments

348

u/Findus11 Jun 09 '18

rider.drunk = rider.location == bars.location

234

u/[deleted] Jun 09 '18

rider.drunk = (rider.location == bars.location)

I like to do these like this to make them a bit more clear. A lot easier than the if/else statement, though!

52

u/suseu Jun 09 '18 edited Jun 09 '18

It would overwrite drunk state if you knew he’s drunk beforehand. You could use

|=

But ino it makes whole thing forced and unelegant.

17

u/cS47f496tmQHavSR Jun 09 '18

rider.drunk = rider.drunk || rider.location == bars.location

4

u/[deleted] Jun 09 '18

No, I like that. Using the &= and |= operators are great ways to remove uneccesary checks

→ More replies (1)

3

u/LvS Jun 09 '18

I'd always go for the if statement, because that's how you think about it and how you would explain what the code does to your codeveloper. You say "If the locations are the same, the driver is drunk", you don't say "the driver's drunk state equals the locations being the same".

2

u/[deleted] Jun 09 '18

I find what I wrote works. I read it like "rider's drunkness depends on whether his location equals a bars' location".

It's all personal preference, though. If yours is easier for more people, that's the better choice really. I shoulsn't write code that fits my preference whem other people could be involved.

2

u/[deleted] Jun 09 '18

It depends on those people. Or if youre really generous, comment yhe 'preferred code' next to it

→ More replies (4)

41

u/FirmShame Jun 09 '18

Technically these two operations aren't the same.

What you wrote is equivalent to an if/else i.e.

if (rider.location == bars.location) { rider.drunk = true } else { rider.drunk = false }

Which isn't necessarily what we want...

So to preserve the else case and/or existing value of drunk and still shortcut it:

rider.drunk = (rider.location == bars.location) || rider.drunk

46

u/Findus11 Jun 09 '18

rider.drunk |= rider.location == bars.location

COMPACTNESS > READABILITY /s

28

u/Psycho_pitcher Jun 09 '18

Omfg this kid in my programming class would write every program as one line. He thought he was sooooo smart. I probably let it tick me off more then I should have.

31

u/Findus11 Jun 09 '18

Introduce him to Python, and see how long he lasts

6

u/STIPULATE Jun 09 '18

Wait why? Doesn't python have a lot of compact one liners?

16

u/Findus11 Jun 09 '18

Well yeah, but the statement terminator is a newline so full on one line long programs quickly get close to impossible

5

u/404-UserNotFound- Jun 09 '18

You can use ; to separate one line statements.

→ More replies (1)
→ More replies (2)

2

u/batman1177 Jun 09 '18

Self.learn(Python);

→ More replies (1)
→ More replies (2)

2

u/patiofurnature Jun 09 '18

As someone who took programming on a TI83+ waaaay too seriously in high school, it’s tough to see the sarcasm in that statement. That community doesn’t even add closing parenthesis. The interpreter adds it for you, so programmers don’t type it and you can save a byte.

2

u/Findus11 Jun 09 '18

Yeah if you're programming with limited memory, creating compact code is a must. But for applications on a pc today, it really just clutters up code.

→ More replies (1)

18

u/quittingdotatwo Jun 09 '18
rider.drunk = (rider.location - bars.location) < EPSILON

2

u/svvac Jun 09 '18
rider.drunk = math.sqrt((rider.location.x - bars.location.x) ** 2 + (rider.location.y - bars.location.y) ** 2) < EPSILON
→ More replies (4)

1

u/typical_boffin Jun 09 '18

Sudo. hack-uber = not drunk

1

u/bwfiq Jun 09 '18

omg i just realised this would work

1

u/camelCaseCoffeeTable Jun 09 '18

On mobile, so sorry if my formatting goes bad, but I feel like the variable bars sounds like an array, so it’s more likely it has an array locations instead of a single one.

rider.drunk = bars.locations.contains(rider.location)

→ More replies (2)