Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Just because you CAN do something doesn't mean you SHOULD.

Metaprogramming has a place in Ruby but for the examples in the article there are far more readable ways to implement it.

Readability > cleverness every single time.



> Just because you CAN do something doesn't mean you SHOULD

This is hacker news, not "production code" news. Abusing things in unexpected ways is pretty much the definition of hacking.

Many people seem to be jumping on this article as if the author was suggestion a new programming pattern we should start using, rather than an interesting look at some of the lesser-known quirks of ruby. I'm pretty sure even the author would agree that triple nested method definitions are not something we should use for production code.


This is a fundamental part of the language, the idea that objects and classes are dynamic, not static.

What I would say is that although we should have an extremely good reason to employ these techniques in production, I believe that every professional Ruby programmer should be able to understand them and/or figure them out in a few minutes.

It’s not like we’re talking about obfuscated C.


Agree with you 100%, and the ability to dynamically define methods is one of Ruby's great strengths.

However, metaprogramming is a power that should be used wisely. When implemented unnecessarily it reduces readability (and probably performance) for no real gain.

The devise source contains some great examples of metaprogramming used properly:

https://github.com/plataformatec/devise/blob/master/lib/devi...


> Readability > cleverness every single time.

Readability is in the eye of the beholder. For those unfamiliar with higher-order programming, maps are just a 'too clever' form of a for loop.

A clean and understandable solution is one matching the problem being solved in a precise way. In search for simplicity one can't forget that programming is a trade, and one should be expected to actually learn some shit.


To that point, Jef Raskin famouly said that “intuitive == familiar,” and all-too-often, that’s exactly what people mean when they talk about “intuitive" code and/or user interfaces.


Indeed. Looking over the article, the first example is sort of obvious if you ever worked more than few hours with a decent dynamically typed language, and the rest expose interesting functionality that could be papered over with a macro in order to build something useful. Like, you know, object-oriented programming can be built by hiding lexical closures under a macro or two, and was in fact built that way in the past.

In other words - just because you don't understand something doesn't mean it's "clever code".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: