Sunday, December 23, 2007

QOTD

Coding Horror: Size Is The Enemy


Java is like a variant of the game of Tetris in which none of the pieces can fill gaps created by the other pieces, so all you can do is pile them up endlessly.

links for 2007-12-24

Wednesday, December 19, 2007

How Not To Write An API

So a future version of Rails will get Pluggable Controller Caching. That's a good idea, and good on Rails for formalizing this extensibility. Better than monkey patching.

Looking at the implementation, though, leaves me cold. Let's look at a code snippet:


def read(name, options = nil)
super
@ether.get(name)
end


With some commentary:


Invoking super for each method call ensures that the proper messages get logged for each action


Wow, way to trust your users! </sarcasm> Requiring a call to super is very bad practice. Any time you are relying on a client of your class to call something, you're risking someone forgetting to make the call.

It's better to use the Strategy Pattern. Here, you finalize the API method (here it is read) and provide an abstract method or your subclasses. Marking the method as final (which I don't think you can do in Ruby, but I digress) ensures that a subclass can't override your functionality, and the abstract internal method ensures that your client doesn't have to call back to super.

Here's how I'd fix it:


def read(name, options = nil)
# some logging and error handling
read_internal(name, options)
end

protected

def read_internal(name, options = nil)
raise "Subclasses must implement this method"
end


Ta-da! No more forcing the user to call super, thus making your algorithm safe and well encapsulated.

links for 2007-12-20

Thursday, December 13, 2007

SQL Server Adapter for Rails 2.0

UPDATE: Everything works now. Turns out I had a plugin installed that monkey patched the SQL Server Adapter. This plugin was from an older version of Rails, and the Rails 2.0 adapters now extend from AbstractAdapter. Fixing the parent class in my plugin monkey patch (to extend from AbstractAdapter) fixed this up and now I no longer get the error.

I just installed the SQL Server Adapter Gem from gems.rubyonrails.org for my new Rails 2.0 project:


gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org


When I attempt to use it, I receive this error message:


TypeError: superclass mismatch for class SQLServerAdapter
from /var/lib/gems/1.8/gems/activerecord-sqlserver-adapter-1.0.0/lib/active_record/connection_adapters/sqlserver_adapter.rb:190
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `require'
from /home/sladd/development/workspace/DSES2/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:496:in `require'
from /home/sladd/development/workspace/DSES2/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in'


Any ideas? My database.yml looks like this:


development:
adapter: sqlserver
mode: odbc
dsn: dses_rollup
username: sa
password:


Has anyone successfully installed the new SQL Server adapter as a gem into a Rails 2.0 project?

I've asked the Ruby on Rails Talk Google Group, so we'll see if that turns up any help.

Wednesday, December 12, 2007

Programming in Scala - The Book

Artima has just released a pre-release copy of Programming in Scala. Much like the Pragmatic Programmers handle their book publishing, you can buy the pre-release copy as a PDF now, and Artima will provide free updates as the book progresses. You can choose to purchase just the PDF, or the PDF/Printed bundle.

Scala is a really fun programming language, combining object oriented and functional aspects into one dynamic and powerful package. Best of all, Scala runs on the JVM, so it can take advantage of the entire Java ecosystem.

If you're interested in functional programming (and you should be) but don't want to completely abandon your investment in OOP or in Java, then you need to give Scala a look.

One of my favorite features of Scala is the Actors support. You can use Actors to achieve Erlang style concurrent and scalable systems on the JVM. Now that's hot.

links for 2007-12-13

Thursday, December 6, 2007

Douglas Crockford on Software Quality

When Douglas Crockford talks about Software Quality, you should listen. If you are involved in writing software and need to understand why software is difficult, or if you are planning a software project, you need to watch this presentation.

Includes such gems as "No rational person can do software", "Programmers don't understand how they spend their time", and "If you took all the code you wrote over the past year, you could probably type it into the computer in one day." One of my favorites: "Programming is a social activity."

Oh, and mad props to Crockford for drawing a parallel between building software and Mr. Blandings Builds His Dream House. Not only is it a funny movie, it's one of my Mom's favorites. While Crockford found many apt parallels, he unfortunately was wrong about one. Software construction is not nearly as funny as this movie.

Partially related to this presentation is Joel's article on Five Ways to Fail a Software Project.

Study your craft and improve it.

links for 2007-12-07

Disclaimer

I'm probably required to say that the views expressed in this blog are my own, and do not necessarily reflect those of my employer. Also, except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the BSD License.