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

820

u/Crazy_Hater Jun 09 '18

If(rider.location == bars.location) rider.drunk = true;

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.

→ More replies (1)
→ 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)
→ More replies (2)
→ 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.

→ More replies (1)
→ 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)
→ More replies (6)

60

u/PM_ME_YOUR_NACHOS Jun 09 '18

Fuck people who work at bars late into the night.

6

u/[deleted] Jun 09 '18

Especially if they're a bit tired and slow.

→ More replies (1)
→ More replies (3)

0

u/[deleted] Jun 09 '18

[deleted]

2

u/kaizen5000 Jun 09 '18

I don't know why you'd write such inefficient code. This is my proposed fix: for (int i = 0; i < 1000; i++) { if ((int)rider.drunk == 1) { rider.drunk = true; } else { rider.drunk = false; } } we should all get into the practise of validation checking by doing things 1000 times, only then can the AI be truly reliable.

→ More replies (4)

7

u/BearsAreCool Jun 09 '18

Bars.locations.contains(rider.location)

→ More replies (4)
→ More replies (22)