[Tutor] Refactoring
Hi, I have been hearing this refactoring of code. Well does it have any thing like this in Python and if it is then what is it all about. Thanks. Regards, Ejaj ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Refactoring
On 2012/03/08 03:13 PM, Ejaj Hassan wrote: Hi, I have been hearing this refactoring of code. Well does it have any thing like this in Python and if it is then what is it all about. Thanks. Regards, Ejaj ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor /Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure./ -- Martin Fowler in Refactoring: Improving The Design Of Existing Code [1] As for Python IDEs that have built-in refactoring tools, I know of PyCharm [2] which handles it. [1] http://martinfowler.com/refactoring/ [2] http://www.jetbrains.com/pycharm/features/ -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Refactoring
Hi, I have been hearing this refactoring of code. Well does it have any thing like this in Python and if it is then what is it all about. Thanks. Regards, Ejaj Refactoring is just a way of restructuring code. It can be breaking code apart to more logical pieces -- moving parts of a class to a new class for instance or renaming methods to better say what they're about, subclassing or superclassing things and such. -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Refactoring
Ejaj Hassan wrote: Hi, I have been hearing this refactoring of code. Well does it have any thing like this in Python and if it is then what is it all about. Refactoring is a general technique that applies to any language, not just Python. Refactoring means to take a program which is written in a complicated way, not easy to understand, and gradually simplify it so that it becomes easier to understand and maintain without starting from scratch or changing the behaviour. The trick with refactoring is to find parts of the program that have common, related code. Here is a toy example -- suppose I had code that looks like this: # Ask the user for a number between one and ten. print Please enter a number between 1 and 10 while True: a = int(raw_input(My number is: )) if 1 = a = 10: break print I'm sorry, your number was not between 1 and 10 print please try again # Ask the user for a number between ten and twenty. print Please enter a number between 10 and 20 while True: b = int(raw_input(My number is: )) if 10 = a = 20: break print I'm sorry, your number was not between 10 and 20 print please try again # And one more between twenty and 100. print Please enter a number between 20 and 100 while True: c = int(raw_input(My number is: )) if 20 = c = 100: break print I'm sorry, your number was not between 1 and 10 print please try again print a+b+c Look at all that duplicated code! And if you look carefully, you'll see a silly bug in it as well. You might re-factor that code like so: def get_number(low, high): # Ask the user for a number between low and high. print Please enter a number between, low, and, high while True: n = int(raw_input(My number is: )) if low = n = high: break print I'm sorry, your number was not between, low, and, high print please try again return n a = get_number(1, 10) b = get_number(10, 20) c = get_number(20, 100) print a+b+c Notice that in refactoring, the aim should be to end up with code that is easier to maintain in the future. In this example, all the repeated code is now in one place, so instead of having to make changes to it in three different places, we can make it in one place. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Refactoring
Philip Smith wrote: This is a subject which seems to pop up periodically so apoliogies but I wonder whether anyone can help: a)Is there yet an alternative Python refactoring tool to bicycle repair man (bike)? Find and Replace ;-) b)If not is there any documentation on the appropriate use of bike? I can't seem to get to grips with it unaided. BRM is nicely integrated into Eclipse/Pydev but BRM itself is incomplete and brittle, I only trust it to make changes in the current module. There does not seem to be any prospect of an update to BRM any time soon. Don. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Refactoring
This is a subject which seems to pop up periodically so apoliogies but I wonder whether anyone can help: a) Is there yet an alternative Python refactoring tool to bicycle repair man (bike)? b) If not is there any documentation on the appropriate use of bike? I can't seem to get to grips with it unaided. Thanks much Phil ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Refactoring
Alan Gauld wrote: Picking up Kent's message about refactoring, my approach tends to mean I more often refactor by combining classes into a higher level one, or reverting a class to a native data type than I do by breaking a class into smaller pieces. Most folks tend to mean the opposite direction when they say refactor - breaking a class or method out into two. Hmm, refactoring is so much more than that. Common refactorings for me are extracting common code to a new function or method, moving an attribute or method from one class to another, changing a functional interface to an object-based one, changing the signature of a method, extracting a base class (OK, I do use base classes sometimes ;) There is a catalog of refactorings here: http://www.refactoring.com/catalog/index.html and I recommend Martin Fowler's book to anyone who hasn't read it: http://martinfowler.com/books.html#refactoring Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor