前回、Rubyのリファクタリングでイケてないコードを美しいオブジェクト指向設計のコードへ改良するための方法という記事を書いて、いい反響をいただいたので第2弾を書いた。 Ben Orenstein氏の講演で話されていた前回のとはまた別のリファクタリング方法。元ネタはこちら。 github.com 【リファクタリング前のコード】 class JobSite attr_reader :contact def initialize(location, contact) @location = location @contact = contact end def contact_name if contact contact.name else 'no name' end end def contact_phone if contact contact.phone else 'no phone'
![RubyのリファクタリングでNull Objectを使ってコードをスッキリさせる方法 - ベルリンのITスタートアップで働くジャバ・ザ・ハットリの日記](https://cdn-ak-scissors.b.st-hatena.com/image/square/bca1157b27221315847303b2b0a22deb95a4daaa/height=288;version=1;width=512/http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F51wEP6kgANL.jpg)