Variable declarations will help prevent some bugs and will not
encourage any others (that I've thought of)!
I think what he's describing is a situation something like this:
[Horb-dee-dorb-dee-dorb, I need a new function to say hello.]
def say_hello
var first_name = "Jay"
puts "Hi, #{first_name}."
end
[Great, that works perfectly. Two weeks later: I need to add some
initialization to that.]
def say_hello
init_screen
init_printer
init_window_system
init_text_to_speech
var first_name = "Jay"
puts "Hi, #{first_name}."
end
[Wonderful. Two weeks later: Oh, yeah, I should actually use those
outputs.]
def say_hello(destination)
var destination
if (destination == "screen")
init_screen
if (destination == "printer")
init_printer
if (destination == "window")
init_window_system
if (destination == "voice")
init_text_to_speech
var first_name = "Jay"
destination.puts "Hi, #{first_name}."
end
[Two weeks later: More cruft I don't feel like making up a fake example
for.]
def say_hello(destination)
var destination
if (destination == "screen")
init_screen
if (destination == "printer")
init_printer
if (destination == "window")
init_window_system
if (destination == "voice")
init_text_to_speech
var first_name = "Jay"
var a = 3 * 4 + 5
Thread.join
acts_as_bad_example
destination.puts "Hi, #{first_name}."
end
[Two weeks later: Hey, why is the name even hard-coded?]
def say_hello(destination, first_name)
var destination
var first_name
if (destination == "screen")
init_screen
if (destination == "printer")
init_printer
if (destination == "window")
init_window_system
if (destination == "voice")
init_text_to_speech
var first_name = "Jay"
var a = 3 * 4 + 5
Thread.join
acts_as_bad_example
destination.puts "Hi, #{first_name}."
end
Oooooops.
And even if you remember to delete the hardcoding of "first_name"
completely, you could just as easily misspell it in the new definition.
The problem is that requiring "var" on the first use of a variable works
really well if, and only if, you write all your code in order.