Refactoring Browser (Bicycle Repairman)
Syver Enstad
syver-en+usenet at online.no
Sat Nov 9 11:18:48 EST 2002
I am just so excited and impressed by the Bicycle Repairman, that I
had to show an example here:
Here's an instance method from a class (It's from a little Tkinter app
that I made for my eldest son to sharpen up his multiplication skills)
def ferdig(self):
minutes, seconds = self._model.endMinutesSeconds()
svarText = (u'Du har løst %d oppgaver på %0.0f '
'minuter og %0.0f sekunder'%
(self._model.correctAnswers(), minutes, seconds))
svarText2 = u'Det blir %0.1f riktige svar i minuttet'%((self._model.correctAnswers()/self._model.timeSpent())*60)
endMsgText = u'%s\n%s'%(svarText, svarText2)
self._endMsgLabel.configure(text=endMsgText)
if askyesno("Ferdig!",
u'Vil du spille engang til?'):
self._model = GangeModel()
self._endMsgLabel.configure(text = '')
self.showGangestykke()
else:
self.quit()
Now I just wanted to get a feel for how good the extractMethod refactoring in
Bicycle Repairman worked, so I marked the following:
svarText = (u'Du har løst %d oppgaver på %0.0f '
'minuter og %0.0f sekunder'%
(self._model.correctAnswers(), minutes, seconds))
svarText2 = u'Det blir %0.1f riktige svar i minuttet'%((self._model.correctAnswers()/self._model.timeSpent())*60)
endMsgText = u'%s\n%s'%(svarText, svarText2)
And chose ExtractMethod from my Bicycle Repairman menu in Emacs.
Bicycle Repairman changes the code to:
def ferdig(self):
minutes, seconds = self._model.endMinutesSeconds()
endMsgText = self.endMsgText(minutes, seconds)
self._endMsgLabel.configure(text = endMsgText)
if askyesno("Ferdig!",
u'Vil du spille engang til?'):
self._model = GangeModel()
self._endMsgLabel.configure(text = '')
self.showGangestykke()
else:
self.quit()
and:
def endMsgText(self, minutes, seconds):
svarText = (u'Du har løst %d oppgaver på %0.0f '
'minuter og %0.0f sekunder'%
(self._model.correctAnswers(), minutes, seconds))
svarText2 = u'Det blir %0.1f riktige svar i minuttet'%((self._model.correctAnswers()/self._model.timeSpent())*60)
endMsgText = u'%s\n%s'%(svarText, svarText2)
return endMsgText
Very neat, I know that this is the way a Refactoring Browser is
supposed to work, but I can't help being impressed anyway :-)
So, many thanks to the people who have worked hard to create a Python
Refactoring Browser, and also making it possible to use it from
different tools.
--
Vennlig hilsen
Syver Enstad
More information about the Python-list
mailing list