Conditional Code


You’re writing a small web application for a new client. You’re wrestling with the credit-card processing module–it doesn’t do what you want. puts statements and temporary output litter your code. Three hours and eight coke-cans later, you solve it! Joyous, you wipe out the temporary code, and email it to the customer.

They reply back: it’s printing some temporary variables. Oops.

It’s printing out other credit-card numbers.

Identity theft ensues.

Double-oops.

Their email makes it clear: their lawyer will be in touch with you. You throw everything in a suitcase and bolt out the door.

Mexico, here I come!

There’s a common problem in software–how do you write code that only runs only in “debug” mode? We’re all used to that style of peppering our code with puts statements and temporary output. Learn how to use a debugger!

But there’s a deeper problem, one that large companies commonly face, too; how can you write debug-only code? For example, you might want to throw some functions in your class that print out extra functions, or bypass certain lengthy steps–just for debugging.

Ruby provides a surprisingly simple but powerful solution to this:

if statements.

Observe the code below:

class DataCruncher
def crunch_current_task
current_task = Tasks.find(:first, conditions => ['owner_id = ?', current_user.id])
current_task.start
# ...
end
end

Say we wanted to print out the task ID and title in debug mode. Assuming you have DEBUG_MODE defined in your environment.rb file in your Rails application (or in a global constant somewhere, in Ruby), you can do this:

class DataCruncher
def crunch_current_task
current_task = # ...
if DEBUG_MODE
Logger.log("Starting task #{current_task.id}: #{current_task.name}")
end
# ...
end
end

Let’s take it a step further. Say you wanted to create a short_circuit method, in debug mode ONLY, that makes sure the task finishes in five seconds. You can (surprisingly) do this:

class DataCruncher
if DEBUG_MODE
def short_circuit
current_task = # ...
current_task.time_to_process = 5
end

def crunch_current_task
# as above
end
end

The key here is that the if-statement is inside the class declaration, not inside a method declaration.

What will this do? If you try to access some_task.short_circuit, in debug mode, it’ll work; but in non-debug mode, Rails will tell you the method doesn’t exist!

You can even take this a step further (although the design decision might not be great) in a multi-user system, and define different/specific functions that users can use, based on their current role. For example, administrative users might have access to methods like flush_data, delete_user, and fun things like that. Non-admin users not only don’t have access to those methods, but they don’t even know they exist! The down side, of course, is rather than getting an “access denied” message, you get a “method not found” error. Annoying to try and handle, considering it could pop up in any part of your code.

Still, this is a powerful and useful feature of Ruby that you can use to make your life a little easier.

And of course, printing debug statements is as easy as:


class Debug
def print(message)
puts message unless System.DEBUG_MODE == false
end
end

But the power and flexibility of conditional code is enormous; what are some fantastic use-cases for something like this? What kind of situations can it (or has it) really help you out in?

Tags: , ,     Posted in Development

Rate this article:
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Related Content


Leave a Reply