The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Ruby Substrings and Testing Legacy Code

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jake Scruggs

Posts: 336
Nickname: aurorae314
Registered: Feb, 2008

Jake Scruggs is a developer at ThoughtWorks
Ruby Substrings and Testing Legacy Code Posted: Mar 31, 2008 9:02 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jake Scruggs.
Original Post: Ruby Substrings and Testing Legacy Code
Feed Title: Jake Scruggs
Feed URL: http://jakescruggs.blogspot.com/feeds/posts/default
Feed Description: Ruby, Rails, Metrics, Testing, and pursuit of clean code.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jake Scruggs
Latest Posts From Jake Scruggs

Advertisement
Recently Josh Cronemeyer and I were working on writing a game in Ruby. Gosu, a 2D game library for Ruby and C++, and Chipmunk, a 2D Physics engine, do lots of the heavy lifting so we thought it would be a fun Saturday afternoon thing to do. However, the examples had no tests so when we tried to change some stuff, and of course it didn't work, we were all sorts of clueless as to why.

Well, wrapping tests around legacy code is not a lot of fun but it is an effective way of debugging. Here's how I like to do it:
Step one:
Identify potentially troublesome code (As in "What the hell is that doing?").
Step two:
Write some tests that verify the functionality of the code.
Step three:
Pull out the offending code into a method or methods.

There's a fair bit of interplay between steps two and three, testing one piece may require extracting it.

Step four:
Either you've found your problem or you've accomplished four things:
1. You really understand the troublesome code.
2. You've refactored it into a more readable version.
3. You've put some code under test.
4. You at least know where the problem isn't.

I adopted this strategy after reading Michael Feathers "Working Effectively with Legacy Code" -- which is good reading.

Eventually we found the problem to be an erroneous substring.
"hello"[0] doesn't return "h"
Ex:

irb(main):001:0> "hello"[0]
=> 104
Which is the character code of 'h', so we tried:

irb(main):002:0> "hello"[0,1]
=> "h"
irb(main):003:0> "hello"[4,5]
=> "o"

Aha! Now we thought that "string"[x,y] gives you the characters between position x and y. But no, we got some really strange behavior until we realized that "string"[x,y] gives you y number of characters starting at x position. The reason "hello"[4,5] gives "o" is because if you ask for 5 things starting at position 4 there's only one character left, which Ruby is only too happy to hand over. "hello"[0,1] is the way to get the first letter, just as "hello"[1,1] is how to get the second letter.

Now one of the things that drew me to Ruby in the first place was it's intuitiveness. Often I found that if I didn't know how something worked, I could just write what I thought would work and it did. But substrings are a rare anomaly -- I find I have to look up how they work every time. And I still make mistakes even after I check the ruby doc. Does anyone know the history behind this weird syntax?

Read: Ruby Substrings and Testing Legacy Code

Topic: Ruby In Steel 1.2 - Beta 3 Previous Topic   Next Topic Topic: sharesource , github

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use