News for hackers. Stuff that matters.
Re:Ruby Best Practices (full book available for download) by richcollins on Wednesday March 17, 2010 @02:17PM (#1199239)
Keyword arguments via hashes is certainly not a best practice for "Beautiful API Design". Sending messages is part and parcel to good OO design. Code that deconstructs hashes to emulate keyword arguments is invariably hideously complicated.
Re:Ruby Best Practices (full book available for download) by sandal on Wednesday March 17, 2010 @02:28PM (#1199274)
I agree with you from a language design standpoint, but seriously, I doubt you have much Ruby experience if you're saying this. And if that's the case, you really don't have a basis to be commenting on my book in such a critical and dismissive way.

Using positional arguments for more than two or so things creates connascence of position, which quickly becomes annoying.

Using block based DSLs so that you can break things down into small functions that each do their part is a good way around this, but these tend to be hard to extend dynamically.

Typically well designed Ruby systems provide both: Pseudo keyword arguments for dynamic needs, and some DSL-type syntax for pleasing the eyes.

Yes, it sucks that we don't have real keyword arguments. But no, it's not nearly as bad as you think.

Re:Ruby Best Practices (full book available for download) by richcollins on Wednesday March 17, 2010 @02:57PM (#1199346)
I've been using Ruby since 2005 and I am a top 100 contributor to Rails (http://contributors.rubyonrails.org/). Rails design suffers for its heavy use of "keyword arguments".

positional arguments for more than two or so things creates connascence of position

Which is precisely the reason that you shouldn't pass more than two arguments to a method (unless you are creating a list or hash for their intended purpose). If you have more than two arguments to a method, it's a sign that you should reify them into an object.

Using block based DSLs so that you can break things down into small functions that each do their part is a good way around this

Why do people love making things complicated by misusing blocks. Just create an object and set slots on it. Blocks are best used for delaying computation, not setting up state.

I think that Zed Shaw's essay on the "Master" and the "Expert" is applicable here: http://www.zedshaw.com/essays/master_and_expert.html

Re:Ruby Best Practices (full book available for download) by sandal on Wednesday March 17, 2010 @03:17PM (#1199399)
Wow, and this is exactly the kind of douchebaggery I was trying to get away from in my book. You know, I think on the first page I mention that the answer to "what the best way" to do something in Ruby is always "it depends". The notion that you can follow singular design principles in all situations is absurd, if you ask me.

Context is king, and what you've done is taken a single point from my book, ripped out its context, strawmanned it to death, and used it to bolster you're own "Top 100 rails" status.

I encourage readers to actually see what I had to say in the book by downloading the PDF before taking these arguments too seriously. If there are some constructive examples to be shown that seem promising and pass peer review, they'll definitely make it into the open source version.

It might even be worth it to write up a section describing the tradeoffs that richcollins has pointed out, but it's definitely not something as universal as he makes it sound.

Re:Ruby Best Practices (full book available for download) by richcollins on Wednesday March 17, 2010 @03:31PM (#1199440)
used it to bolster you're own "Top 100 rails" status.

I've mostly stopped using Rails and Ruby in favor of Io, so I have no interest in bolstering my Rails status. I was just providing evidence to counter your assertion that I was a Ruby newb.

The notion that you can follow singular design principles in all situations is absurd, if you ask me.

I agree that "it depends", but using Hashes as arguments has become idiomatic Ruby, which is unfortunate in most cases.

Re:Ruby Best Practices (full book available for download) by sandal on Wednesday March 17, 2010 @03:51PM (#1199484)
I wasn't assuming you were a newb, just that you didn't have much exposure to what are commonly regarded as well designed Ruby libraries. It didn't occur to me that you'd have had that exposure and yet sound as if you were shocked and appalled about what is common practice.

I still would really like to see examples of well designed open source Ruby projects that follow what you consider to be decent design principles. I think that'd make a much stronger case for your argument, and might open my eyes to something that's been in a blind spot.

Personally, I feel like a certain amount of API design is bound to what consumers will expect. We can certainly stretch and shift their tendencies, but if we fly in the face of them, our perfection in a vacuum will never be appreciated.

Re:Ruby Best Practices (full book available for download) by richcollins on Wednesday March 17, 2010 @04:34PM (#1199573)
Which libraries do you consider well designed? I'll let you know how I think that they could be improved.
Re:Ruby Best Practices (full book available for download) by sandal on Wednesday March 17, 2010 @04:52PM (#1199614)
I've already written an entire book on this and given it away for free. I suggest you read it and write a review. Be specific, and show good counterexamples. I'll be happy to link you when you do.

Until then, stop acting like you're some sort of authority (NOTE: This is not to claim I am -- because I'm not and no one is.) I just think it'd make sense to put your ideas into a bit of context so people can see that you're not just being critical for the sake of being critical.

Re:Ruby Best Practices (full book available for download) by richcollins on Wednesday March 17, 2010 @06:23PM (#1199841)

    #from your book
    
    def distance(*points)
      case(points.length)
      when 2
        x1,y1,x2,y2 = points.flatten
      when 4
        x1,y1,x2,y2 = points
      else
        raise ArgumentError, "Points may be specified as [x1,y1], [x2,y2] or x1,y1,x2,y2"
      end
      Math.hypot(x2 - x1, y2 - y1)
    end

    puts distance(1, 2, 5, 6)
    puts distance([1, 2], [5, 6])

    # why would you choose to make a complicated interface that requires a complicated implementation?

    # simple, clear interfaces that allow for simple implementations
    # are almost always preferrable to complicated ones that save a
    # keystrokes through syntax

    class Point
      attr_accessor :x
      attr_accessor :y
  
      def initialize
        self.x = 0
        self.y = 0
      end
  
      def self.with(x, y)
        new.set(x, y)
      end
  
      def set(x, y)
        self.x = x
        self.y = y
        self
      end
  
      def distance_to(a_point)
        Math.hypot(a_point.x - x, a_point.y - y)
      end
    end

    puts Point.with(1, 2).distance_to(Point.with(5, 6))
Re:Ruby Best Practices (full book available for download) by sandal on Wednesday March 17, 2010 @09:22PM (#1200212)
So you mean that you prefer this:

Point.with(1, 2).distance_to(Point.with(5, 6))

Over this?

distance [1,2], [5,6]

I guess it depends on how many interesting things you wanted to do with points. As things get more complicated, maybe something like this is worthwhile, but I don't feel that making everything an explicit object is categorically better.

But the section you're quoting is among the most contrived examples that RBP gives, it's mainly just part of a list of the different sorts of argument processing Ruby offers and the trade-offs betweeen them.

Maybe you skimmed and missed that, or maybe you read through and chose to ignore that detail.

Re:Ruby Best Practices (full book available for download) by stevedekorte on Thursday March 18, 2010 @02:00PM (#1201995)
The problems I have with "distance [1,2], [5,6]" are:

0. It breaks the fundamental OO convention and doesn't encapsulate logic and data.

1. It's not clear what it means unless you look up the docs.

2. It's implementation is complex, making it more difficult to understand and maintain

3. It fails to separate concerns, making it less extensible/modifiable. e.g. What if you wanted to override the setting behavior? You couldn't do it in one place.

Re:Ruby Best Practices (full book available for download) by lg on Wednesday March 17, 2010 @02:23PM (#1199261)
Would you prefer a *rest argument? An options hash is the best way to make your ruby API flexible.
Re:Ruby Best Practices (full book available for download) by richcollins on Wednesday March 17, 2010 @03:00PM (#1199353)
Plain old objects are the best way to make your API design flexible. You can't easily change the behavior of setting a key/value pair for an individual hash. Objects are designed to implement behavior, Hashs are meant to hold key value pairs.
Re:Ruby Best Practices (full book available for download) by lg on Wednesday March 17, 2010 @07:03PM (#1199902)
Right, the keys/values need to have supporting logic. And yeah if you're sticking procs in a hash, just use an object; but if your object is just a bunch of fields, use a hash. And often that's all you need. I just want to override the marginWidth on this fooBar.

Remember you don't need to destructure the hash. You've got a local one initialized to sensible defaults, and you .merge! it with the param one, and then just use the hash values as your local vars.

Re:Ruby Best Practices (full book available for download) by richcollins on Wednesday March 17, 2010 @08:53PM (#1200161)
If your object is a bunch of fields, you should still use an object. It might need behavior later.

I think the central issue is that there is too much friction to creating new classes in Ruby. It feels official and final. In Io, you just clone an existing object like you would send any other message.

Re:Ruby Best Practices (full book available for download) by jamesbritt on Thursday March 18, 2010 @12:35AM (#1200470)
There's also an embrace of YAGNI and "Do the simplest thing possible ..."

I'd rather start with a hash and then see if it later demands more, than start inventing objects that, based on existing use cases, need only be hashes or structs.

I'd also rather go through a number of pre-1.0 iterations to get real-world usage to shake out the painful API parts. Up until then I'm happy to break compatibility if experience says there's some bad code in there.

Monkeybars, for example, had some methods that began life using positional args, but they were changed to use hash values. I hated it.

Aside from the extra typing required for every call, with positional args I got used to reading code and seeing things in a fixed order, so when I had to use the method myself I knew exactly what needed to go where. (These methods had 2 arguments, with maybe an optional third or so.)

With the hash args the order could vary so I had to keep looking up what the possible argument key values could be.

(Thing about using hashes for args: with positional arguments you need only know what role an argument is playing in order to know what sort of value to pass in, but with a hash you have to know the exact key name. That ends up being more stuff to look up in the docs, especially if the API designer has a quirky way of naming things.)

You might think that at least for a code reader the use of hash keys would better document the call, but the method name and the names of items being passed in were almost always sufficient.

I suspect this particular change was a result of over-thinking these methods and expected usage instead of actually paying attention to what the code was doing right now, and how people were using it right now.

There are times when my gut tells me, "You know what's going to happen with this code later", and I try to take that into consideration, but (usually) simple wins for me.

Re:Ruby Best Practices (full book available for download) by richcollins on Thursday March 18, 2010 @02:21AM (#1200634)
With the hash args the order could vary so I had to keep looking up what the possible argument key values could be.

You can set slots on objects in any order that you want to:

    Foo.new.set_x(x).set_z(z) ...
Re:Ruby Best Practices (full book available for download) by jamesbritt on Thursday March 18, 2010 @04:13AM (#1200766)
That is insanely ugly. I will pass, thanks.
Re:Ruby Best Practices (full book available for download) by richcollins on Thursday March 18, 2010 @01:03PM (#1201798)
Setters are composable and you can modify their behavior. Hash literals ... not so much.

It would seem you only see the surface beauty of things. [redacted -- good point]

Re:Ruby Best Practices (full book available for download) by jamesbritt on Thursday March 18, 2010 @01:13PM (#1201835)
"It would seem you only see the surface beauty of things."

Is that how you defend an API? Insult the user?

Holy shit that's stupid.

Re:Ruby Best Practices (full book available for download) by extension on Monday April 19, 2010 @09:38AM (#1276652)
How about adding this to the Point class?

  def to_hash
    {:x => @x, :y => @y}
  end
  
  def to_ary
    [@x,@y]
  end

  def [] i
    case i
    when 0,:x
      @x
    when 1,:y
      @y
    end
  end
Should work if the receiving method interprets the arguments properly.
Re:Ruby Best Practices (full book available for download) by sandal on Thursday March 18, 2010 @10:19AM (#1201335)
Can you recommend an Io book for me? I want to hunt down the author and try to encourage them to make their code more Rubyish. Because you know, that's helpful.
Re:Ruby Best Practices (full book available for download) by richcollins on Thursday March 18, 2010 @01:02PM (#1201793)
I did recommend to the Io author that he create a Hash literal syntax because I like the fact that Ruby has one ;-)

To be clear, I think that it is great that you created this book. I just responded as I did because I'm often frustrated when I use Ruby APIs that sacrifice good design (imo) for syntactic terseness.

Re:Ruby Best Practices (full book available for download) by kogent on Wednesday March 17, 2010 @03:22PM (#1199412)
The book is opensource, why not submit a <strike>patch</strike> pull request. :)
Re:Ruby Best Practices (full book available for download) by sandal on Wednesday March 17, 2010 @03:29PM (#1199429)
In all seriousness, all it takes is some code examples that make me and a few others say "whoa, cool" and it'll make its way in.

If RBP becomes "Gregory Brown's Best Practices", it'll be epic fail. I am encouraging folks to prove whatever they can wrong in this book.