Aloha Ruby Conf 2012 Refactoring from Good to Great by Ben Orenstein

Confreaks · 92K views · 2012 · 44:19

Open on YouTube

Software architecture & designPython, Ruby & scripting languagesTesting, debugging & developer toolstalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering Ruby and Rails questions about refactoring good code into great code and about implementing the null object pattern.

Recommended by 1 people across 1 different months, 2013 to 2014.

What it is

A 44-minute live-coding talk by Ben Orenstein at Aloha Ruby Conf 2012, recorded by Confreaks, with no slides. He refactors Ruby code on stage, covering the Open-Closed Principle, types of coupling, composition, the NullObject and Decorator patterns, and testing smells such as Mystery Guest.

What people said

Recommends Boris Stitnicky · 2014-07 · Stack Overflow · on “Splittings files in Ruby”

The question wheter there is more efficient way of accomplishing your task depends on what do you mean by "efficient" and in what way do you reuse your code. Obviously, computational complexity is not a question here, so you must be asking about ergonomical efficiency. The code you wrote is good enough. But if you want to refactor it from good to great, you would be invited to make more thorough use of object-oriented techniques. For example, you would write an object RowValues : class RowValues < Array def self.of line new line.split "\t" end def build_file_name file "#{self[file.hometown_index]}.csv" end def write file_path open( file_path, "a+" ) { |f| f << self[0] + "\n" } end end def sp

Cites Yeggeps · 2013-03 · Stack Overflow · on “Mongoid association & null object pattern?”

How would you implement the null object pattern on a Mongoid relation? Class Owner include Mongoid::Document embeds_one :preference end Most owners won't have a preference, and thus I want them to have a NullPreference instead, as described in Ben Orenstein's excellent talk [this video] . What I would like is something like this: class NullPreference def name 'no name' end end owner = Owner.new preference = owner.preference preference.name => 'no name' I found a related question [LINK] regarding the same thing in ActiveRecord, no answers though. Edit: I'm using Mongoid 2.6 otherwise I could've used autobuild: true and get a real Preference and use the defaults instead.

Cites Antonio Carlos Ribeiro · 2013-06 · Stack Overflow · on “Best Practices for Laravel 4 Helpers and Basic Functions?”

Laravel's helpers.php method is to add it to your "files" in composer.json ( [LINK] ): "autoload": { "classmap": [ ... ], "files": [ "app/libraries/helpers.php" ], }, What I do is to create small classes (a few methods per class, one line per method, everything extended from something and DRY, that's my goal), class ExtendedCarbon extends Carbon\Carbon { public function formatDDMMAAAA($date) { /// format and return } } save them to them in app/libraries and add to composer.json: "autoload": { "classmap": [ ... "app/libraries", ... ], }, Execute composer dump And then just use them wherever you need $formatted = (new ExtendedCarbon)->formatDDMMAAAA($date); Watch this video about refactoring: