Front Page Forums MOOM My perspective of Ruby ‘barewords’

  • My perspective of Ruby ‘barewords’

    Posted by Mel Riffe on 2018-01-12 at 01:01

    First off, this is not meant to disparage anyone’s views on their use or non-use of Ruby barewords’. This is my attempt to codify’ why I like them and use them extensively; I also instruct the rest of my team to use them.

    When I wrote Smalltalk code there was no getting to instance variables without an accessor. Period. So it’s kind of engrained in my coding DNA, as it were. Also, I purposely write small’ objects; the surface of their interface is small, just a handful of public methods. The behavior is private. I also try to keep the number of instance variables to an absolute minimum.

    But I find that barewords give me a lot of power and flexibility. They allow me to change the internal shape of my object, its internal behavior, without worrying if I’m forgotten an instance variable. They also allow me to change the source of the information my object needs if it’s using a collaborator.

    I think what really helps me is intention revealing names, and tests, lots of tests. I’m also strict/stingy with my input parameters.

    However I’ve not run into a case where it was an extra chore when reading the barewords’. I’m not saying it won’t happen, just that it hasn’t happened.

    Thank you for letting me ramble/share.

    replied 8 years, 7 months ago 0 Member · 4 Replies
  • 4 Replies
  • Sarah Neuber

    Member
    2018-01-14 at 12:33

    Interested to see some example code that demonstrates what you nicely explained. Any chance of that?

    • Mel Riffe

      Member
      2018-01-14 at 14:39

      I’ll see what I can obfuscate’ for illustration purposes. Thank for you asking.

      • Mel Riffe

        Member
        2018-01-16 at 23:57

        Sorry for the small example, but this is the best I can do, due to client restrictions.

        task :get_mac_licenses => :environment do
          vendor = SparkLabsVendor.new
          license = MacLicense.new vendor: vendor
          LicenseManager.fill_up license: license
        end
        
        # /* -- */
        class LicenseManager
        
          def self.fill_up **args
            new( args ).fill_up
          end
        
          def initialize license:
            @license = license
          end
        
          def fill_up
            STDOUT.puts "#{self.class}##{__method__}; Licenses to get is: #{licenses_to_get}"
            licenses_to_get.times do
              license.create!
            end
          rescue => e
            # Something bad happened! Now what?
            STDERR.puts e
            raise e
          end
        
          private
        
          attr_reader :license
        
          # The number of unallocated licenses to keep on-hand.
          # TODO: Should this be configurable?
          def buffer
            @buffer ||= 100
          end
        
          # The number of licenses to procure. This is the difference
          # between our buffer size and how many already exist.
          def licenses_to_get
            @count ||= buffer - license.count
          end
        
        end
  • Sarah Neuber

    Member
    2018-01-22 at 07:43

    Thank you Mel!

    I was especially happy to see that your example looks very similar to code I tend to write these days :slight_smile: That really gave me some extra confidence that I can def. stand up in this class as an equal to many of the other great devs in here :grin:

    Thanks!!

Log in to reply.