Saturday, March 23, 2013
Choy suet code refactoring notes
Choy Suet Code Refactoring
My RPi code is now like a bit wot of choy suet, and I found it messy to do more and more patching. So I made up my lazy mind to start doing some refactoring. I only heard about the term refactoring perhaps last year. I thought that it meant grouping similar code segments and make them functions, and that is all. It should be similar what I learn in primary school, look for HCF (or LCM, but I forgot what is LCM) Highest Common Factor. Just to make sure I did not misunderstand the term, so I googled.
Code refactoring - Wikipedia
Code refactoring is a "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior",[1] undertaken in order to improve some of the nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity to improve the maintainability of the source code, as well as a more expressive internal architecture or object model to improve extensibility.
By continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you'll find that it is easier to extend and maintain code.
—Joshua Kerievsky, Refactoring to Patterns[2]
Typically, refactoring is done by applying a series of standardised basic "micro-refactorings", each of which is a (usually) tiny change in a computer program's source code that either preserves the behaviour of the software or at least does not modify its conformance to functional requirements. Many development environments provide automated support for carrying out the mechanical aspects of these basic refactorings.
Refactoring is usually motivated by noticing a code smell.[3] For example the method at hand may be very long, or it may be a near duplicate of another nearby method. Once recognized, such problems can be addressed by refactoring the source code, or transforming it into a new form that behaves the same as before but that no longer "smells". For a long routine, one or more smaller subroutines can be extracted; or for duplicate routines, the duplication can be removed and replaced with one shared function. Failure to perform refactoring can result in accumulating technical debt.
There are two general categories of benefits to the activity of refactoring.
Maintainability. It is easier to fix bugs because the source code is easy to read and the intent of its author is easy to grasp.[4] This might be achieved by reducing large monolithic routines into a set of individually concise, well-named, single-purpose methods. It might be achieved by moving a method to a more appropriate class, or by removing misleading comments.
Extensibility. It is easier to extend the capabilities of the application if it uses recognizable design patterns, and it provides some flexibility where none before may have existed.[2]
Before applying a refactoring to a section of code, a solid set of automatic unit tests is needed. The tests are used to demonstrate that the behavior of the module is correct before the refactoring. If it inadvertently turns out that a test fails, then it's generally best to fix the test first, because otherwise it is hard to distinguish between failures introduced by refactoring and failures that were already there. After the refactoring, the tests are run again to verify the refactoring didn't break the tests. Of course, the tests can never prove that there are no bugs, but the important point is that this process can be cost-effective: good unit tests can catch enough errors to make them worthwhile and to make refactoring safe enough.
The process is then an iterative cycle of making a small program transformation, testing it to ensure correctness, and making another small transformation. If at any point a test fails, the last small change is undone and repeated in a different way. Through many small steps the program moves from where it was to where you want it to be. In order for this very iterative process to be practical, the tests have to run very quickly, or the programmer would have to spend a large fraction of his time waiting for the tests to finish. Proponents of extreme programming and other agile methodologies describe this activity as an integral part of the software development cycle.
List of refactoring techniques
Here are some examples of micro-refactorings; some of these may only apply to certain languages or language types. A longer list can be found in Fowler's Refactoring book[3] and on Fowler's Refactoring Website.[5] Many development environments provide automated support for these micro-refactorings. For instance, a programmer could click on the name of a variable and then select the "Encapsulate field" refactoring from a context menu. The IDE would then prompt for additional details, typically with sensible defaults and a preview of the code changes. After confirmation by the programmer it would carry out the required changes throughout the code.
Techniques that allow for more abstraction
Encapsulate Field – force code to access the field with getter and setter methods
Generalize Type – create more general types to allow for more code sharing
Replace type-checking code with State/Strategy[6]
Replace conditional with polymorphism [7]
Techniques for breaking code apart into more logical pieces
Componentization breaks code down into reusable semantic units which present clear, well-defined, simple-to-use interfaces.
Extract Class moves part of the code from an existing class into a new class.
Extract Method, to turn part of a larger method into a new method. By breaking down code in smaller pieces, it is more easily understandable. This is also applicable to functions.
Techniques for improving names and location of code
Move Method or Move Field – move to a more appropriate Class or source file
Rename Method or Rename Field – changing the name into a new one that better reveals its purpose
Pull Up – in OOP, move to a superclass
Push Down – in OOP, move to a subclass
Hardware refactoring
While the term refactoring originally referred exclusively to refactoring of software code, in recent years code written in hardware description languages (HDLs) has also been refactored. The term hardware refactoring is used as a shorthand term for refactoring of code in hardware description languages. Since HDLs are not considered to be programming languages by most hardware engineers,[8] hardware refactoring is to be considered a separate field from traditional code refactoring.
Automated refactoring of analog hardware descriptions (in VHDL-AMS) has been proposed by Zeng and Huss.[9] In their approach, refactoring preserves the simulated behavior of a hardware design. The non-functional measurement that improves is that refactored code can be processed by standard synthesis tools, while the original code cannot. Refactoring of digital HDLs, albeit manual refactoring, has also been investigated by Synopsys fellow Mike Keating.[10][11] His target is to make complex systems easier to understand, which increases the designers' productivity.
In the summer of 2008, there was an intense discussion about refactoring of VHDL code on the news://comp.lang.vhdl newsgroup.[12] The discussion revolved around a specific manual refactoring performed by one engineer, and the question to whether or not automated tools for such refactoring exist.
As of late 2009, Sigasi is offering automated tool support for VHDL refactoring.[13]
AMIQ DVT, an IDE for hardware design and verification, provides refactoring capabilities for e (verification language), SystemVerilog, Verilog and VHDL.
History
In the past refactoring was avoided in development processes. One example of this is that CVS (created in 1984) does not version the moving or renaming of files and directories.
Although refactoring code has been done informally for years, William Griswold's 1991 Ph.D. dissertation[15] is one of the first major academic works on refactoring functional and procedural programs, followed by William Opdyke's 1992 dissertation[16] on the refactoring of object-oriented programs,[17] although all the theory and machinery have long been available as program transformation systems. All of these resources provide a catalog of common methods for refactoring; a refactoring method has a description of how to apply the method and indicators[disambiguation needed] for when you should (or should not) apply the method.
Martin Fowler's book Refactoring: Improving the Design of Existing Code[3] is the canonical reference.
The first known use of the term "refactoring" in the published literature was in a September, 1990 article by William F. Opdyke and Ralph E. Johnson.[18] Griswold's Ph.D. thesis,[15] Opdyke's Ph.D. thesis,[16] published in 1992, also used this term.[17]
The term "factoring" has been used in the Forth community since at least the early 1980s. Chapter Six of Leo Brodie's book Thinking Forth (1984) is dedicated to the subject.
In extreme programming, the Extract Method refactoring technique has essentially the same meaning as factoring in Forth; to break down a "word" (or function) into smaller, more easily maintained functions.
Refactorings can also be reconstructed[19] posthoc to produce concise descriptions of complex software changes recorded in software repositories like CVS or SVN.
Automated code refactoring
Many software editors and IDEs have automated refactoring support. Here is a list of a few of these editors, or so-called refactoring browsers.
IntelliJ IDEA (for Java)
Eclipse (for Java, and to a lesser extent, C++, PHP, Ruby and JavaScript)
NetBeans (for Java)
JDeveloper (for Java)
Embarcadero Delphi
Visual Studio (for .NET)
ReSharper (addon for Visual Studio)
Coderush (addon for Visual Studio)
Visual Assist (addon for Visual Studio with refactoring support for VB, VB.NET. C# and C++)
DMS Software Reengineering Toolkit (Implements large-scale refactoring for C, C++, C#, COBOL, Java, PHP and other languages)
Photran (a Fortran plugin for the Eclipse IDE)
SharpSort addin for Visual Studio 2008
Sigasi HDT (for VHDL)
Xcode (for C and Objective-C)
Smalltalk Refactoring Browser (for Smalltalk)
Simplifide (for Verilog, VHDL and SystemVerilog)
Tidier (for Erlang)
AMIQ DVT (for e, SystemVerilog, Verilog and VHDL)
See also
Code review
Database refactoring
Design pattern (computer science)
Obfuscated code
Software peer review
Prefactoring
Separation of concerns
Test-driven development
Modular programming
References
...
.END
No comments:
Post a Comment