Archive

Archive for the ‘Refactorings’ Category

How to safely rename a public API member and mark it obsolete

July 31st, 2012 Comments off

There are times when public API members must be deprecated or its signature must be changed. In this case, we can not remove or modify public members, because other application that depend on these APIs may become broken and/or uncompilable. If you’d like to supersede a public member, you can use a special attribute called ‘Obsolete’. The Obsolete attribute marks an API member as one that is no longer recommended for use.

Read more…

How to create a method or constructor overload using CodeRush

July 31st, 2012 Comments off

The process of creating more than one method in a class with the same name is called as method overloading. Method overloading allows the developer to define several methods with the same name but with a different set of parameters. This way, one does not have to remember the names of multiple functions that serve a similar core purpose. When a call is made to one of these overloaded methods, the compiler automatically determines which of the methods should be used according to the arguments used in the call.

Read more…

Promoting locals and constant value expressions into parameters

July 31st, 2012 Comments off

There are times when you realize that a local variable or a field reference within a method would be more useful if it was a parameter. Having a new parameter on a method will increase its flexibility for consumers. To convert the local variable, you should remove its declaration from the body of the method, add it as a parameter and replace all occurrences of a local to a new parameter. The same steps must be performed in the case of field references. Furthermore, all method references in the entire solution should be updated to pass a new value as an argument to a method with a new signature.

Read more…

Converting methods into extension methods based on its parameters

July 23rd, 2012 Comments off

The Make Extension refactoring converts the chosen public method into an extension method based on the selected parameter type. To apply the refactoring, select the parameter for which you want to create an extension method:

Read more…

CodeRush encapsulation refactorings

July 23rd, 2012 Comments off

As we know, Encapsulation is an important object-oriented programming concept. It is also known as a data hiding mechanism. Encapsulation enables a group of properties, methods and other members to be considered a single unit or object. The idea of encapsulation is that an object’s internal data should not be directly accessible from an object instance. With correct encapsulation, a developer does not need to understand how the class actually operates in order to communicate with it via its publicly available methods and properties.

Read more…

Refactoring using statements with CodeRush/Refactor!

May 9th, 2012 Comments off

As many of you know, the using statement is a good tool for managing types which will be accessing unmanaged resources. The using statement provides a simple and convenient syntax that ensures that objects that implement the IDisposable interface are correctly disposed.

If the object is not disposed, CodeRush highlights the undisposed variables with the “Undisposed local” code issue:

Read more…

Refactoring multi-conditional statements: the switch statement and the “if-else-if” ladder

April 30th, 2012 Comments off

There are two methods for multi-conditional processing: the “if-else-if” ladder and the switch statement. The If-Else-If ladder is a combination of ‘if’ and ‘else’ statements that is used to test a series of conditions. If the condition of the first ‘if’ statement is met, the code within the ‘if’ executes. Otherwise, the program flow is passed to the neighborhood ‘else’ statement, in which the next ‘if’ statement is located. This continues as a series of ‘if’ statements nested within the previous ‘else’ statements until all conditions have been checked. A default branch or a code block may be executed in a final ‘else’ statement if no condition is previously met.

Read more…

Refactorings for graphics – Convert to Size

March 23rd, 2012 Comments off

The Convert to Size refactoring from DevExpress Refactor! Pro consolidates selected numeric parameters into a System.Drawing.Size (System.Windows.Size in WPF projects) or SizeF object. It might be useful to combine two parameters into a single parameter, reducing the overall number of method parameters and increasing code readability.

Read more…