top | item 36901373

(no title)

cattown | 2 years ago

No! Don't do it!

I'm sure there are rare cases where these techniques are useful. Like creating developer tools or making your own object persistence layer or winning a code golf contest. But if you're an app developer for goodness sake just write a few extra lines of code. Do whatever it is you're doing the verbose and clear way, not the slightly shorter and super obtuse way.

Stuffing method definitions into classes at runtime, monkey patching, dynamically generating method calls with .send. These will all be very puzzling for any future developer that works on your code, senior or junior. And come with bunches of technical pitfalls. Writing clear and maintainable code is a higher calling for us than reducing LOC and showcasing neat tricks. Even if you call yourself a Rubyist. Speaking from experience.

discuss

order

goatlover|2 years ago

Arguably reducing LOC helps maintainability, as long as it's done in a clear manner. That's one of the main points of abstraction, and all programming languages offer various abstraction facilities, unless it's a language like brainfuck.

lacrimacida|2 years ago

Depends how it’s done. And you can overabstract or do the wrong abstractions thinking you gained something from your cleverness but quite often all you’ve done is you obfuscated, reduced the maintainability for others and you got stuck with a steaming pile. I’ve seen quite a few fellows do this deliberatley in order to become harder to replace and it works somewhat. But said codebases don’t age too well

KerrAvon|2 years ago

Came to see some Python zealot use the term “monkey patching” and was not disappointed

sho|2 years ago

You don't need to be a python zealot to use the term, and abhor the practice of, monkey patching. In fact rubyists should hate it even more. There's a special, nearly unattainable state of pure ethereal rage one can only reach after 6 hours debugging some incomprehensible failure only to finally realise it's due to someone who has suddenly become your mortal enemy monkey patched ".to_json" a week ago.

"Your honey, I admit to killing him, and I'm sorry, but in my defense, he monkey patched a core method"

"Case dismissed!"