Ways to ask but why

~~sometimes~~ all the time you find code that makes you go O.o Someone did the weirdest thing, but it works. Probably.

My favorite thing to say in these moments is "But why?". When it's really weird and we're good friends, I post this gif.

You have to be careful with that. It works when trust is super duper extra high and even then you have to be ready to back it up with a good conversation.

Here's a few better ways to ask that question.

Asking "but why" makes people feel judged and puts them on the defensive. Personally I love explaining but why and will talk your ear off. But normal people have a much smaller ego and will shut down when confronted directly.

Instead you approach with obvious curiosity, validate that this is in fact a brilliant solution you would never think of, and ask about all the constraints they were solving for that you didn't know about.

60% chance they tried the obvious solution and it didn't work. That's why the code looks weird. It has to!

Then you ask them to put that answer in a comment.

Cheers,
~Swizec

PS: the last 2 will probably result in a "Oh thanks! I'll do that next time". Let it go, next time is fine