Comment by RangerScience

8 years ago

Oh, neat! It's always cool when the authors of content pop onto HN (I see from you karma you're new here, welcome!)

Yeah - I heard your point when I first read your post, but it took some more time and reading to understand it. It's a good distinction.

What I'm not getting is... So you've got a really precise and elegant line between "language" and "library", but it's not clear what the difference is between patterns and.... maybe idioms?

attr_accessor, has_many, and carrierwave's mount_uploader (to provide many examples) are all examples of a "pattern" (in the sense of a repeated thing, not a design pattern) in Ruby, but I can't determine what's then in need of "fixing" or what abstraction features could be added to remove the "pattern".

> I see from your karma you're new here, welcome!

You missed the "created: 3246 days ago" bit, then? :-) But you're right that I don't spend much time here - thanks for the welcome!

> it's not clear what the difference is between patterns and.... maybe idioms?

You're right, it's not a clear distinction. But for our purposes, the important point about design patterns is "must be reimplemented from scratch for each use". Have you read the Gang of Four book? Their patterns all require the programmer to define a bunch of new classes from scratch each time the pattern is used. Compare the Decorator pattern (https://en.wikipedia.org/wiki/Decorator_pattern) to Python's decorators - the pattern requires the programmer to create new classes, forward methods, etc, but the Python feature requires the programmer to write one line of code (which then does all the class-creation and method-forwarding under the hood). The Python feature has successfully obviated the need for the pattern. You still need to write the same amount of application-specific business-logic code, but the machine handles all the tedious plumbing for you. Similarly, attr_accessor and has_many handle the tedious plumbing of getters/setters and collections-backed-by-join-queries, respectively. So the necessary abstraction features have already been added, and there's nothing left to fix. An interesting intermediate case is the Iterator pattern in Python (https://en.wikipedia.org/wiki/Iterator_pattern). Python still has explicit iterators, but standardises the interface so library code can make use of user-provided iterators. It also provides a convenient language-level facility for building iterators, using generator syntax. Haskell, arguably, has entirely absorbed iterators into the language, replacing them with lazy lists.

It's worth noting that the key property of Ruby that allows for things like attr_accessor is the ability to add methods and fields to an object or class at runtime, and thus to do so programmatically. Lisp can similarly dispense with a lot of patterns that are necessary in Java, because Lisp has compile-time code generation in the form of macros. Peter Norvig's talk on this is great: http://norvig.com/design-patterns/ppframe.htm

  • > You missed the "created: 3246 days ago" bit, then? :-)

    Tots did. Errr... welcome, travelling from the land of Lurk?

    > You're right, it's not a clear distinction...

    Still, does the naming sound right? Patterns that get "encoded" into the language, or made trivial by abstraction features within the language, are idioms?

    > Have you read the Gang of Four book?

    No :/ The closest I've come, weirdly, was tutoring a friend through a design pattern class. Each time she'd come up and say, "The name of this week's pattern is such-and-such", and from that, I'd get 90% of the way to knowing that pattern was. I've always wanted just a simple list of the names, maybe with linked single paragraph descriptions, but every prior time I've looked I haven't been able to find one.

    > add methods and fields to an object or class at runtime

    Yes! That's a favorite Ruby trick. Straight up tho - what is the formal name of doing that, specifically, the formal name of the category to which attr_accessor and has_many belong to?

    • > Patterns that get "encoded" into the language, or made trivial by abstraction features within the language, are idioms?

      Yeah, that sounds like a reasonable term to use.

      > The closest I've come, weirdly, was tutoring a friend through a design pattern class.

      It's a bit old now, but I think it's probably still worth reading the GoF book - it's clearly written, and much better than most subsequent imitators. In particular, each pattern comes with a "when not to use this" discussion, that more people should pay attention to :-/

      > I've always wanted just a simple list of the names, maybe with linked single paragraph descriptions

      Would http://wiki.c2.com/?CategoryPattern do?

      > what is the formal name of doing that, specifically, the formal name of the category to which attr_accessor and has_many belong to?

      I'm not sure. Adding methods to a class at runtime is called "metaprogramming" (which has the more general meaning of "writing code that writes or modifies code"). I don't know of a name for a method that performs metaprogramming, though.