Recursive Sum
Code: ,
Comments Off on Recursive Sum

In #ruby on Freenode, platzhirsch asked about how to total an array of Transactions when the Transactions may have parents. The two obvious approaches have pitfalls when there are a lot of Transactions, and he said he expects to have 23 million that may be deeply nested. Here’s his sample code:

Continue this post

Have You Seen This Cache?
Code: , , , , ,
Comments Off on Have You Seen This Cache?

It looks like syntax highlighting, image thumbnails, and compiling object files.
Let me explain.

Continue this post

Sizing Up My Queue
Code: , , ,
Comments Off on Sizing Up My Queue

I have a folder named “queue” that I download podcasts, videos, and books to. It occurred to me that it hasn’t been empty for years. That’s OK, the world is a very interesting place, and I care that I produce things, not just consume them.

But I wondered how big the queue is. Not in terms of disk space, that doesn’t tell me much because the different encoding rates and formats result in very different file sizes. (The disk space command is df -h, if you need it.) In terms of time.

Continue this post

The Two Kinds of Integers
Code: , , , ,
Comments Off on The Two Kinds of Integers

A small thing I see tripping up developers is that there are two kinds of integers: numbers and identifiers.

We don’t know what the integer 4 means unless we know what it’s for. Are we counting things, or identifying them? It’s really easy to slip between the two, like this real code snippet I found:

Continue this post

Isolating Polymorphism
Code: , ,
Comments Off on Isolating Polymorphism

I’m reading Code Complete, 2nd ed and it’s been like catching up with an old friend. I remember reading the first edition at my first tech job fifteen years ago. At the time a lot of it went by me, but rereading I can see a lot of things I had to learn on my own.

Continue this post

Type-Checking Interface in Ruby
Code: , , , ,
Comments Off on Type-Checking Interface in Ruby

One of my favorite parts in Sandi Metz’s excellent Practical Object Oriented Design in Ruby is when she describes how to enforce lightweight interfaces so that multiple objects can play a role reliably. And I thought: how I can I enforce this in a much-more heavy-handed and annoying way?

This question also ties into two ridiculous
tweets of mine:

Continue this post

Builder Methods
Code: , , , ,
Comments Off on Builder Methods

I was reading Corey Haines’ book Understanding the Four Rules of Simple Design (capsule review on the 2014 book reviews post) when I read:

In fact, over time I’ve developed a guideline for myself that external callers can’t actually use the base constructor for an object. Put another way: the outside world can’t use new to instantiate an object with an expectation of a specific state. Instead, there must be an explicitly named builder method on the class to create an object in a specific, valid state.

This was a brief aside at the tail of a longer discussion of interdependent and redundant tests. It really caught my attention and I’d like to hear it more thoroughly investigated. In the hopes of attracting Corey’s attention, I offer this cat pic and an exploration of the benefits of a similar practice I have:

Continue this post

Two Designs for SequenceIdGenerator
Code: , , , ,
Comments Off on Two Designs for SequenceIdGenerator

In my previous, marathon post on id generation, I mentioned that I was generating unique sequence numbers. It was straightforward to write a class to generate them:

Continue this post

Extracting Immutable Objects
Code: , , , , , ,
Comments Off on Extracting Immutable Objects

In the last few weeks I’ve been rehabilitating some of the first object-oriented code I wrote in the hopes of bringing my mailing list archive back online. Lately I’ve been refactoring some of the earliest, core code: the Message class. It manages the individual emails in the system and, because I didn’t understand how to extract functionality, had turned into something of a God Class.

Yesterday I tweeted about a really satisfying cleanup:


Continue this post

Inheriting From Built-In Ruby Types
Code: , , ,
Comments Off on Inheriting From Built-In Ruby Types

2015-05-09: While I still want to break down ActiveRecord models, I now disagree with the idea of inheriting from Ruby’s stdlib types and think they should always be wrapped. My RailsConf 2015 talk expands on the thinking below (especially about immutability!) and touches on reasons why not to inherit from stdlib.

Continue this post