Robot 37
I found a little something. And some other somethings.
There’s this:
function WorldProxy:moveFB(name,command, steps, callback)
local rq = {
robot=name,
command=command,
arguments={steps}
}
self:requestWithCallback(rq,callback)
end
function WorldProxy:requestWithCallback(rq, callback)
local response = self.world:request(rq)
callback(Response(response))
end
And there are these:
function WorldProxy:launchRobot(name,robot, callback)
local kind = "RonRobot1"
local shields = 99
local shots = 99
local request = {
robot=name,
command="launch",
arguments={kind,shields,shots}
}
local responseDict = self.world:request(request)
callback(Response(responseDict))
end
function WorldProxy:look(name, callback)
local request = {
robot=name,
command="look",
arguments={}
}
local responseDict = self.world:request(request)
callback(Response(responseDict))
end
Duplication, my good siblings, is the enemy. We remove it:
function WorldProxy:launchRobot(name,robot, callback)
local kind = "RonRobot1"
local shields = 99
local shots = 99
local request = {
robot=name,
command="launch",
arguments={kind,shields,shots}
}
self:requestWithCallback(rq,callback)
end
function WorldProxy:look(name, callback)
local request = {
robot=name,
command="look",
arguments={}
}
self:requestWithCallback(rq,callback)
end
We test. Tests fail. We are frankly gobsmacked. Revert, confused. Test. Do more slowly. Realize we pasted (rq,callback)
into methods who called it request
not rq
. Do correctly. Green. Commit: refactor to use requestWithCallback everywhere.
Look at who said rq
and make all them say request
. Test, green commit: rename rq to response in WorldProxy.
Was That Really Worth Doing?
Heck yes! It already confused me once today. It’s sure to happen again. It’s best to call the same things by the same names.
Which reminds me, I have the word “steps” occurring a lot, but the official spec word is “distance”, and that confused me yesterday among other days. Let’s look for that one.
Found lots of “steps”, replaced with “distance”. Tests green, commit: rename steps to distance throughout.
These names have confused me. Confusion is expensive in terms of time spent and errors created. Best to fix them.
Methods Not In Alpha Order
Often when I refactor, I put an extracted method right after the one from which it was extracted, because I’m going to display them both here in the article. That tends to create an object whose methods are not in alpha order, and with no IDE to speak of, that makes things hard to find.
So I manually sort them. Just sorted WorldProxy. Test, green, commit: alphabetize WorldProxy class.
Alphabetize World. Test, commit. Robot. Test, commit. Knowledge, test, commit.
Small Steps
These tiny improvements—Kent Beck refers to them as tidying—make the code a better place to be, a bit more clear, a bit less prone to tripping us up. And it can be quite relaxing, since most tidying doesn’t require a lot of deep thought.
This thing is looking quite nice. I’m sure there are things that could be simpler or more clear, but they’re not jumping out at me. I might have to actually do a feature or two.
Next time …