Simple things

def englishNumber number
  left = number
  write = left/100              # How many hundreds left to write out?
  left = left - write*100       # Subtract off those hundreds.

  if write > 0
    return 'one hundred'
  end

  write = left/10               # How many tens left to write out
  left = left - write*10        # Subtract off those tens.
end

puts englishNumber( 32)
puts englishNumber(100)

The above code snippet is part of a program that turns an integer into it’s english version. E.g. if the number “1” passes through the program it will return “one”.

The whole program is relatively simple and pretty straightforward but I wanted to talk about it because it exposes one of the most interesting things I’ve found in programming. Simplicity is the gift and curse of programming.

For the last hour I’ve written out this program then manually went through each step to find out what’s really happening. With pen to paper I’ve drawn out each sequence, following along as the program moves from line to line. I’ve spent the last hour trying to figure out what happens in the above section.

I knew that it was an easy solution but I couldn’t quite figure it out. At the end I realized that I was returning the float instead of the integer in the “write” section. It makes a difference using “32 – 32″ as opposed to “32 – 30″.

These are the biggest mistakes I make when writing code. It’s the simple things that sometimes trip me up. Whether it’s missing out an apostrophe for quotation marks or not writing out the full variable name, I always find small errors when I go over my code.

I have a high attention to detail but with programming you must take that to another level. It’s a gift and a curse because code allows you to solve complex problems with relatively simple structures, algorithms and programs. On the other side it can also be a curse because you can make simple mistakes if you’re not thoroughly attentive to detail.

This also boils over to real life. The biggest problems we have our usually related to the simpler things. Overweight? You probably are eating the wrong foods at the wrong time. Tired? You might not be getting enough sunlight or exercise.

Of course most of the time there are a lot of other external factors which also contribute to these problems. However, the best steps to solving them are most effective when addressing the basic issues first.

Let’s make sure to take care of the simple things.

 
0
Kudos
 
0
Kudos

Now read this

How to write a switch statement in ruby

case a when 1..10 puts "It's between 1 and 10" when 8 puts "It's 8" when String puts "You passed a string" else puts "You gave me #{a} -- I have no idea what to do with that." end Programming languages are very similar to spoken... Continue →