Archive for March, 2007

The Rules of Code Club

1. You do not mix code and content.

No Comments »

chris on March 29th 2007 in /dev/random

The Reality of Writing New Code

Ryan’s Tech Blog delves into the “why” of Microsoft creating a front-end client to CodePlex, Microsoft’s version of the venerable, much-loved SourceForge. In his post Ryan hits one truism - perhaps the central truism - of writing new software right on the head:

It’s more fun to write new code than read old code, but this fun wears off. After a certain initial momentum creating your new tool, you will inevitably come to a realization “this is going to take me for-fucking-ever”. Unless your itch is particularly strong, you’ll probably quit….

That’s what makes frameworks like Rails, Qt and .Net so incredibly valuable and fun to use: they, if they’re any good, take care of the mundane 90% of the code that will take for-fucking-ever leaving the developer to spend 100% of their time on the other 10% of code… that will take for-fucking-ever also but at least it’s the part of the project that can hopefully differentiate your online social widget mash-up app from the other guy’s.

Well observed Ryan. Well observed.

No Comments »

chris on March 28th 2007 in /dev/random

Deprecated @flash Warnings

I keep getting this deprecation warning:

DEPRECATION WARNING: @flash is deprecated! Call flash.[] instead of @flash.[].

which would be reasonable except that the code triggering it is this:

RUBY:
  1. <% for name in [:message, :error] %>
  2.     <% if flash[name] %>
  3.         <div id="flash" class="alert <%= name.to_s %>"><%= h( flash[name] ) %></div>
  4.     <% end %>
  5. <% end %>

Something's not sitting right with the @flash deprecation warning.

5 Comments »

chris on March 28th 2007 in /dev/rails

Feeling the Refactoring

We developers don't talk much about our feelings and the rest of the world would think us crazy to talk about "feeling" software development but that's just what I'm going to do here. I suspect if you're the sort who reads blogs about software development you're the sort who'll get what I'm going for.

code_migration1Lately I've gotten into the habit of spending a good chunk of my coding time forcing myself to forget all the new features I want to add, ignoring the bugs that already exist and instead simply going back into existing code and refactoring it mercilessly.

I've noticed that when I visualize the code in my Rails app it tends to take on specific shapes. Initially it starts out like the image on the left: top-heavy, all upper-body and no legs, kind'a like a sport climber or a swimmer. Views start out fully-loaded with HTML and CSS and Ruby embedded in them. Controllers are fatter than they should be, containing too much low-level functionality and knowing too much about their models. And the models tend to be svelt little things, practically sticks.

A good refactoring later its usually possible to strip almost all the Ruby code from the views, save for basic iterative functions and output statements. At this point the bloats migrates southward and the controller ends up too fat. The structure of the code now resembles a pear-shaped, middle-aged bank manager, still with sticks for legs.

code_migration2Some more refactoring and the code shape starts to look like the image on the right, all hour-glassy and sexy. The view is all-natural HTML and CSS, the controller has been slimmed down to the very basics it needs to manage V-M interaction, and almost all the real heavy lifting has been compartmentalized and moved into the models where it belongs.

It is at this stage that the code starts to "feel" right to me. It feels like I can leave it alone, secure in the knowledge that if I have to come back to it later I'll be able to understand it. That if I need to extend it I can be fairly confident of doing so without causing any destructive worm-hole effects. It it here that the code starts to feels good.

And that feeling is what makes refactoring so incredibly satisfying, often even moreso that writing the original code in the first place.

Feel the refactor.

No Comments »

chris on March 28th 2007 in /dev/random

We Are All Worthy of “Dude, WTF?”

One of the sites I tend to read each day is The Daily WTF? Worse Than Failure, because it's fun to laugh at one's own industry.

However Code Horror, another site I tend to enjoy, has posited that the big thing wrong with WTF is that the developers who might most learn from it are the sort who'd never read it in the first place:

I can absolutely guarantee that the kinds of developers who could benefit most from reading WTF simply do not-- and never will-- read the WTF website.

I quite disagree. This implies there's an "us" and a "them" and while it might be nice to be in the "us", I think we're actually all "them". If we write code then at some point we've written code worthy of a "dude, wtf?" and chances are it's probably still live.

In fact I myself did so just last week, as I continue to learn Ruby. I wrote a bunch of code to implement a feature I needed, then Canada Post was kind enough to deliver my copy of Programming Ruby at 9am on Saturday morning and by Saturday night I'd discovered that I could reduce roughly fourteen lines of my Ruby code to two lines of Ruby using a few built-in functions I hadn't previously know about.

Manually re-write functionality that already exists in the language is a staple of The Daily WTF? and any seasoned Ruby developer undoubtedly would have looked at my original code and... well, you know the rest.

My first year working at Chum, almost all I did for that year was refactor the existing VB code base. It was a brutal code-base, so nasty that Jay and I practically shared daily wtf?'s, err..., well, daily. Unquestionably the three of us improved those systems by many orders of magnitude. And yet I'm certain that every now and then Jay and Jonathan come across some of my old code that today makes them think "dude, wtf?".

You couldn't go through your applications and find a few reasons to laugh at yourself? And then learn from it?

Code Horror continues:

I know it's asking a lot. It's tempting to get your daily fix of drive-by amusement and move on. But I believe it's our collective duty to leave the profession of software engineering better than we found it. There's so much we can do, and WTF is merely a starting point.

With this I agree and we can improve it by starting at home, admitting we're amongst the "them", and leading by example.

1 Comment »

chris on March 26th 2007 in /dev/random

A Thought on the Modern Computing

Last night I got around to getting my venerable G4 desktop up to some semblance of modern-ness (modernity? modernality?). Took a couple hours but it all went smoothly until I upgraded to the latest version of QuickTime and now no video plays back at all (PPC + 10.3.9 + QT 7.1.5 = bad). No worries, I've got nothing better to do tonight than muck about with that.

As a command-line text editor I happen to like nano. Actually that's not quite true. I just dislike nano less than I dislike all the others (and if you're the sort who just started spitting at your monitor over how much vi/vim/emacs/pico are better and I'm a luser schmuck for not using [your editor of choice here] take your finger and jab it in your eye firmly and you'll have a decent approximation of my position on that issue). And I like editing text files as much as the next guy so I figured I'd just whip nano onto the G4 too. How hard can it be? It's only 117 k in size.

Installing nano

  1. Install DarwinPorts
  2. DarwinPorts won't work without the Apple Developer Tools
  3. Download 600 megs of Apple developer tools
  4. Install the developer tools (with ten minutes of system optimization)
  5. DarwinPorts won't work without X11
  6. Download 50 megs of X11 (with ten minutes of system optimization)
  7. DarwinPorts won't work without X11 headers
  8. X11 headers are part of the developer tools, but not part of the default install (duh)
  9. Re-download 600 megs of Apple Developer Tools (mea culpa, I figured after it was done installing I was done with it and trashed the installer)
  10. Re-install the developer tools, this time with X11 headers (with ten minutes of system optimization)
  11. Update DarwinPorts
  12. Install nano
  13. There is no step 13

Piece o' cake. 1.1 Gigs and 2.5 hours to install a 117 k of text editor. For my next trick I'll install GIMP by next Sunday.

Anyhow, enough of that. That's just the long of way of getting to....

Having to use all three major operating systems daily I've finally come to believe deeply and resolutely what I think I've known for years about the Operating System wars we all engage so heartily and smuggly in:

All operating systems suck. Yours does, mine does. In fact all of mine do. The one used by the smartest person I know does, as does the one used by the stupidest person. All our operating systems suck.

We're like cavemen who've recently just invented the wheel: all our wheels are still hand-carved by bashing rocks together and we're arguing about who carved the wheel the smoothest and which type of rock is best for wheel-making when in fact what we really need is 4-ply all-season radial run-flat tires attached to some decent shock absorbers.

The OS wars are not about who's OS is best, they're about who's OS sucks the least. All we're all doing is trying to find the one that sucks the least for us, as individuals. And an argument at that level is not an argument worth having.

So I hereby apologize to everyone whom I've ever implicitly or explicitly offended by deriding your OS choice. I was wrong and mine is no better, it just happens to suck less for me. But it still sucks.

And if anyone ever implies you're somehow defective for not being able to use a modern computer, poke them in the eye really hard. They deserve it. The software is defective, you are not.

When it comes to modern computing, I think Han Solo sums it up the best: "Good luck, [we're] gonna need it."

Part 2

Evidently I need to clarify some points above since some people seem to think I'm attacking the developers of our modern operating systems for software-related issues or bugs. I'm not. That misses the point entirely. Those developers are in the same boat we all are: their tools are also just as bad as ours and their OS is just as bad as ours. It isn't about the specific software per se.

If the caveman analogy didn't make the point clear enough perhaps this one will. The Wright brothers invented the airplane (our computers today are their airplane). But undoubtedly they had some issues with flying long distances in the fog, at night. That's us, trying to create stable, easy, unobtrusive consumer computers and operating systems.

it's perfectly reasonable for the Wright brothers to put a light on their plane, and then a brighter light, and then a whole bunch of lights to help them fly through the fog. That's us, improving languages, creating better compilers, doing more usability testing for the users.

But in hindsight the best way to fly through fog so far is by using radar, GPS and aircraft auto-pilot systems that can land a plane even if the pilot can't see the ground. That's... exactly where we aren't in computing and, and this is important, it's no one's fault.

No, I don't have a solution. That'd be like expecting the town doctor to tell Orville he just needs to pop up a few geo-stationary satellites and he'll be hunky dorky. Things just don't work that way and my whole point here isn't to lay blame but in fact to point out the very futility of doing so.

That's why the OS wars are so pointless. We're not at a point where being militant is useful since we're all suffering, perhaps in different ways and with different degrees of technical savvy, but all of us.

So when I say the modern OS sucks, it's not an attack on the makers, or the users, or to imply I somehow magically know how to fix it all. Statements like that are useless, just errant bitching. Rather it's a statement of perspective on the state of our infantile knowledge about computing in general compared to where we might hopefully end up one day, far far away.

I am, in fact, celebrating that our operating systems, our software, sucks. Try it, say it, dump the emotional personal investment you've made in your operating system as a reflection of your personality and world-view, take a step back and laugh at what we've created.

And then marvel at how incredibly far we've come in the last fifteen years. It's a good time for suckage.

3 Comments »

chris on March 22nd 2007 in /dev/random

RMagick Has NOT Defeated Me

Installing ImageMagick/GraphicsMagick and RMagick under OS X today has embodied almost everything I dislike about modern computing. After five hours of effort, following the various instructions on fourteen different web pages (thank god for browser tabs) and edit upon edit of arcane command line arguments, I give up. I conceed that they will not be installed, will not be operable.

Even though I'm absolutely comfortable in the command line, regularly have a number of terminal windows open, happily ssh into my various Linux boxes from my OS X laptop, it boggles my mind that I can install entire OS updates with a click of a button without interrupting my workflow yet stand-alone Linux-based apps seemingly cannot be installed without extracting a gallon of fresh blood.

After all this effort I'm left with no answers and a mash of:

/Users/chris/Documents/Downloads/RMagick-1.15.4/./lib/rvg/misc.rb:321: [BUG] Bus Error
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.8.0]
...
post-setup.rb: InitialCoords.rb example returned error code 6
...
post-setup.rb: RotateScale.rb example returned error code 6
setup.rb: Too many examples failed. The RMagick documentation cannot be installed successfully. Consult the README.txt file and try again, or send email to rmagick@rubyforge.org.
make: *** [all] Error 1
...
---> ext/RMagick
no ruby extention exists: 'ruby setup.rb setup' first
Try 'ruby setup.rb --help' for detailed usage.

Thanks for the info, the bus error, the error code 6, the Error 1. That's all extremely fascinating and absolutely useless.

Yeah, I could keep googling, open another half dozen web pages, waste a couple more hours but I won't. I quit. Dealing with non-essential software at this level is simply just too massive a waste of time, and today is one of the rare days when the sun is shining.

Screw you *Magick, I don't really need you anyhow.

UPDATE: A pizza, a few pints and some distance later and *Magick is defeated instead. Solution: remove everything carefully. Forget every other link except this one: Really Truly Getting ImageMagick & RMagick Working on OSX from Source without Using MacPorts(DarwinPorts) or Fink. Download that shell script and run it. That oughta' do it.

Thank you LabRatz.

No Comments »

chris on March 20th 2007 in /dev/random

Trying out BlogMate Within TextMate

This is a test of Todd Ditchendorf's TextMate plugin BlogMate, a plugin that's supposed to make blogging from TextMate faster and easier. So far, so good.

No Comments »

chris on March 19th 2007 in /dev/random

Class Objects from Strings

Or "Let the code write the code".

One of the beauties of Rails is that the "convention over configuration" mandate means that not just program execution but program implementation are extremely predictable. This is very handy when you'd prefer to have your code write the code, as I needed to this afternoon when I wanted to call the same function on a bunch of different classes who's names were available for the operation as an array of strings.

I first started out by spending far too much time mucking around with eval() but that felt kludgy and I've never liked using eval() in any language - I'm naturally gun-shy when it comes to arbitrary code execution. I may know all the code paths into that point today but what about six months from now? Will I always remember never to pass user data through there accidentally?

A little more reading of the Kernel module and the Module class revealed a much cooler and ultimately much simpler way to implement the same functionality: const_get().

In a nutshell:

RUBY:
  1. begin
  2.   the_obj = Kernel.const_get( ( activity.model_name ).camelcase )
  3.   activity_events = the_obj.find( :all )
  4. rescue
  5.   the_obj = nil
  6. end

Much nicer but still not as nice as I'd like since I've had to wrap it in the begin / rescue block. For reasons unexplained const_defined? always returns false when used in here.

Note: Some googling afterwards showed up this very nice example by Matt Biddulph in which he used const_get() to walk the ActiveRecord models and create OmniGraffle diagrams of their relationships. That's a nice mash-up.

No Comments »

chris on March 15th 2007 in /dev/rails, /dev/ruby

Converting Seconds to Timeclock Display

This seems like functionality that should already be built into a Ruby class (Time?) or a Rails helper but I couldn't find it. This simply converts a chunk of seconds into hh:mm:ss display:

RUBY:
  1. time_str = [ seconds / 3600, seconds / 60 % 60, seconds % 60].map { |t| t.to_s.rjust( 2, '0' ) }.join( ':' )

which outputs 02:44:13.

What I'm really looking for is a conversion from seconds into the likes of

1 day, 8 hours, 44 minutes, 11 seconds

similar to what the Rails DateHelper does with distance_of_time_in_words. If you know of such a thing, please point me to it.

No Comments »

chris on March 14th 2007 in /dev/rails, /dev/ruby