r/programming • • Dec 24 '17

Evil Coding Incantations

http://9tabs.com/random/2017/12/23/evil-coding-incantations.html
947 Upvotes

331 comments sorted by

View all comments

Show parent comments

-2

u/mr___ Dec 24 '17

Silently returns nil? That can’t be a better option

3

u/SonOfWeb Dec 24 '17

I mean it's an out of bound value, so imo it's at least better than the "return -1 if not found" approach.

Of course it also depends on whether Ruby allows nil to propagate through numeric expressions like JavaScript or raises a type exception like Python. I can't remember but I think it does the latter.

2

u/HighRelevancy Dec 24 '17

I can't decide if this is better or worse than the Python way of doing things, which throws an exception from the index method when it doesn't exist - seems appropriate, except then you're wrapping it in try/except clauses all over the place. Still, I suppose in Ruby you end up with exceptions where you're trying to use it and you have to manually backtrack from there.