Refactoring

From $1
    Table of contents

     

    Refactoring is the process of changing a software system in a way that won't alter the external behavior of the code but wil improve its structure. It can be applied to all possible programming languages.

    The best example of what refactoring is will be the name refactoring tool.This tool is bound to the F2 key. If you press F2 in a parameter or local variable and edit the local variable, all the references in your method are updated according the moment. If you rename a class, MonoDevelop will also change the name of the constructor.

     

    Resloving Namespace

     

    Another Refactoring tool is the Resloving Namespace.When applied to a class name, this tool will look for a namespace that declares the class, and will insert the required 'using' declaration at top of the file. If there are several namespaces defining the same class, all available options will be shown, so the user can chose.

     

     

    The 'Show Code Generation Window' tool (Alt + Insert by default)

     

    This refactoring tool allows quick generation of snippets of code, based on the current context. Here are some examples:

    • Generate a constructor, initializing a set of selected fields.
    • Generate properties for a set of fields.

    • Override base class members.

    • Implement ToString().

    • Implement equality methods.

    • Introduce a parameter null check in a method.

     

     

     

     

     

     

    Declare Local

     

    This function can really speed up the software development. First type the initializer for your local variable. See this example with "GetName()":

     

    Now select "Declare Local" from the context menu or hit Control+^ (default key) which gives you a correct local variable declaration and selects the name for you:

     

     

     

    Create Method

     

    It creates a method definition out of a method call. First write down your method call:

     

    Opening the context menu and selecting "Create method" gives you:

     
     

    Extract Method

    This refactoring tool could be found in the context menu when selecting a part of the text. It generates a method out of a block of code and automatically adds the required parameters, based on the variables being used in the block of code.

     

     

    Create Class

    Create class (or struct) is almost the same as CreateMethod. Just do a "new" operation and let MonoDevelop create the type (in a new file) for you.

     

    Introduce Constant

     

    It is the fastest way to define a constant out of a magic number (or string).

    Example:

     

    In this method a constant is used.  This constant should be given a better name. Select the constant, open the context menu and hit "Introduce Constant". A dialog appears where you could name the new constant. After you have named it the source should look like:


     

     

    You can edint the refactoring key bindings from the option menu of MonoDevelop Preferences menu menu:

     

    There are many other Refactoring tools and of course there is a way to write your own Refactorings but for more information check this article: 

    http://monodevelop.com/Developers/Articles/Refactoring

    Tag page
    • No tags

    Files (0)

     
    You must login to post a comment.
    Page last modified 22:13, 30 Nov 2010 by TodorStoyanov?