5 ms·
I'd say the money quote is: "To put it concisely, I think that’s what a lot of critiques of “magic” really boil down to: the notion that just because you can d
by jerf 17y ago
I'd say the money quote is:
"To put it concisely, I think that’s what a lot of critiques of “magic” really boil down to: the notion that just because you can do something doesn’t mean you should. Accepting this can be tough, but I think it’s a necessary part of becoming a good programmer; if you don’t have a certain sense of discipline and respect for the power you’re wielding, well, you might up cloning dinosaurs and getting eaten by them when the power goes out."
Going beyond Ruby, I do have this criticism for a number of the really dynamic languages. Doing one of the mighty wizardly things might work, but it often has side effects that only a wizard will know. Sure, hacking on the Array class in Javascript can be cool, but breaks things like property enumeration in "weird" ways (well, I understand them but a newbie won't)... are you really really sure that your awesome new functionality couldn't have just been done with a simple function that takes an array as the first argument? Or did you actually need to extend Array?
Sometimes the answer is, yes, you needed to extend Array. OK then. But it frequently isn't. Wizardly code tends to tie your code up in snarls; one minute you're wizarding this, the next that, then the wizard hunks start interacting, then all hell breaks loose and you get an incomprehensible mess, when it could probably have just been some functions with a bit more verbosity on the calling side. I know; I've been through this pattern at least two big times, including the refactoring back into functions, and I consider this a shameful admission, justified only by "I was young and we all are young at some point"... and I can't help but notice I rarely lose much of significance when I do the refactoring back into functions.
- ionfish 17y agoThe trouble is, many people appear to believe that your argument that there are things which one generally shouldn't do with dynamic languages has the corollary that one should not be able to do them, i.e., that these features should not be available in the first place. The nuance of "Most of the time you shouldn't do this, but sometimes it's necessary or even the correct approach" tends to get lost; only the first clause gets parsed, and we have yet another round of stupid arguments in which one side says "Dynamic languages are bad because you can change the way core objects function", and the other says "Dynamic languages are good because you can change the way core objects function". There is a possible explanation for this: language fundamentalism is an easy position to hold, while subtlety is demanding. Dogma merely requires one to follow its commands, rather than using one's judgement to negotiate between conflicting demands. I hope that this isn't, in fact, the reason that these arguments perpetuate, but I fear that it is.
- jerf 17y agoI totally agree; thank you for highlighting that.
- waldrews 17y agoThere's something to be said for language enforced limitations supporting encapsulation when creating an ecosystem of reusable components - something Java/C# has been great at. An analogy is enforced immutability. Unlike Ruby, Python has immutable strings and tuples, and requires the use of immutable objects for hashmaps - a great choice for library writers, though a minor inconvenience for application programmers who want to say "trust me, i won't change this object later."
- jmillikin 17y agoSmall nitpick -- Python doesn't require immutable objects for dictionary keys, it just won't work correctly in some cases with mutable keys.
- ubernostrum 17y agoWell, technically the requirement is to implement both __hash__() and one of __eq__()/__cmp__(). But Python fudges this a bit in how it handles user-defined types.
- carbon8 17y agoThe recommended and near-universally used Ruby hash keys are immutable. Symbols are used most frequently, but even if you pass a string as a hash key it gets automatically copied and frozen. freeze is also a method on Object and can be used anywhere to make an object immutable.
- tdavis 17y agoI completely agree. I'll step back from my normal state of bashing the Rails community for a moment and give a quick personal example from Python: I use an incredibly helpful library that basically allows one to create a public API out of their Django models with very little effort. It even generates documentation and validation, etc. The problem is, it uses metaclass "magic" and decorators for everything. I understand both these concepts well enough, but when an entire system is built around using these two powerful constructs, unwinding how things happen becomes rather difficult (for someone who doesn't use metaclasses and decorators for everything) -- to the point that if the library can't do something, it isn't even worth my time to properly modify it. There were certainly other ways to go about creating the provided functionality. Django's inner-class-based approach would have been a good start! And there'd be nothing wrong with supplying helpful decorators, or even using metaclasses here and there, but the library (at least to me) screams "magic for magic's sake".