Decoration is best, except when it isn’t

Decoration is best, except when it isn’t:

Decoration and module extension are both viable ways to compose objects in Ruby. Which to use is not a simple black-or-white choice; it depends on the purpose of the composition.

For applications where you want to adorn an object with some extra functionality, or modify how it presents itself, a decorator is probably the best bet. Decorators are great for creating Presenters, where we just want to change an object’s “face” in a specific context.

On the other hand, when building up a composite object at runtime object out of individual “aspects” or “facets”, module extension may make more sense. Judicious use of module extension can lead to a kind of “emergent behavior” which is hard to replicate with decoration or delegation.

Missing refactoring…

Crazy, Heretical, and Awesome: The Way I Write Rails Apps | James on Software:

To decouple the logging from the creation of the database record, we’re going to use something called a service object. A service object is typically used to coordinate two or more objects; usually, the service object doesn’t have any logic of its own (simplified definition). We’re also going to use Dependency Injection so that we can mock everything out and make our tests awesomely fast (seconds not minutes).

[Again, a missing refactoring from the last major project…]

Source: @brynary

Sandi Metz: SOLID Design Principles – Dependency Injection

Sandi Metz: SOLID Design Principles – Dependency Injection:

Because of the style of coding in Job, it contains dependencies that effect my ability to refactor and reuse it in the future.

[An excellent example of something that was hard to think through in the last significant codebase I worked on. There are always tradeoffs in style, testing, and time spent. We managed as best we could, but I think we should have done more of this (easy to say now.)]

Source: brynary

Your tests are lying to you

Your tests are lying to you:

Well designed code is easy to test. As a rule of thumb, anytime I get over about two or three lines of setup code for testing a method, I normally take a step back and ask myself if this method is doing too much.

Test speed

The other advantage of running tests purely in isolation is that they’re fast. Very fast. When I’m coding Rails apps these days, thanks to advice from Corey Haines I’m running a spec_no_rails folder which runs independently from the rest of my Rails app. Rails apps by default epitomise this problem: default model tests exercise the whole system from the database up. By running your tests independently you’re not having to clean the database or start Rails each time you run your tests, which means that much of your interesting code can be tested in under a second. Gary Bernhardt has more information on how to set this up in his excellent Destroy All Software screencast series.

[One thing not mentioned here is a pet peeve of mine. I see a lot of tests where the dev is testing implementation rather than input and outputs. To me, checking how something was implemented is not relevant. That the method returns the expected results for a given input is. “Implementation tests” also causes a great deal of test rewrite when refactoring.]

Select and copy text within Quick Look previews

Select and copy text within Quick Look previews:

To make text selectable in Quick Look previews, you just need to enable a hidden Finder setting. Select and copy the code below, open Terminal (/Applications/Utilities), paste that code at the prompt, then press Return:

defaults write com.apple.finder QLEnableTextSelection -bool TRUE; killall Finder

After a second or two, the Finder will restart. Once it does, you’ll be able to select text in Quick Look previews and copy it to the Clipboard for use elsewhere.

If you decide you don’t deserve to select text in Quick Look, you can turn this feature off with another Terminal command:

defaults delete com.apple.finder QLEnableTextSelection; killall Finder

[There’s an improvement.]

HTTP Signature authentication scheme

A Bit More About the New Joyent Cloud API:

To solve this problem with SmartDataCenter, we’re introducing the HTTP Signature authentication scheme, which is already released as open source. The HTTP Signature scheme allows for digital signatures to be leveraged as your credentials, and does not imply any particular key management scheme. It should be suitable for just about any REST API out there, so we’re certainly hoping if you’re an API vendor you take a look at it, but rather than jump into the technical details of the spec itself, I thought it would be interesting to show you some CLI candy, so lets walk through some examples.

Source: Joyeur

Ruby On REST: Introducing the Representer Pattern

Ruby On REST: Introducing the Representer Pattern:

The calls for more object-orientation in Ruby frameworks are growing ever louder. Most web frameworks, like Rails, come with a nice database abstraction, a Rack-like HTTP endpoint, routing, and a template engine. It’s the lack of additional abstraction layers that make many programmers ask for “more OOP”.

[Yeah, been feeling this for a while. The problem is unless you can put in some serious refactoring to an established project the weight of getting things done (momentum) often overwhelms the desire to improve the codebase (which, of course, makes the future refactoring larger in the ugliest of loops).]

More on pattern mismatches (MVC etc.)

ActiveRecord (and Rails) Considered Harmful – Literate Programming:

If you’ll notice, I basically have said that models are a problem. Controllers are a problem. Views are a problem. MVC has served the web well, even if it isn’t the GUI style MVC that named the pattern. But I think we’re reaching its limits; the impedance mismatch between HTTP and MVC, for example, is pretty huge. There are other ways to build web applications; I’m particularly excited about WebMachine. I don’t have a constructive alternative to offer here, I just know there’s a problem. I’m still mulling this one over.

[Some interesting thinking here. It feels like it makes certain things easier but is not fleshed out. Maybe because it’s new, but maybe it just doesn’t feel quite right. I’m fairly convinced that part of the problem is the amount of functionality we often cram into one application.]

Your Rails Application is Missing a Domain Controller

Your Rails Application is Missing a Domain Controller:

When an application has multiple Domain Controllers I place them in a dedicated directory under app called services to keep them separate from the Model entities in app/models. This helps communicate that Rails Action Controllers should delegate to the Domain Controllers, not directly to Model Entities. The Domain Controllers and Model Entities together, represent the domain layer of the system. I have also discussed extracting the entire domain layer from the Rails framework, but that’s another discussion. Let’s keep this blog post civil for now!

[I’ve also refactored in this direction a bit lately. I’m beginning to wonder of MVC needs to be replaced in these cases with “MDCV” to begin with…]