Archive

Archive for the ‘Refactorings’ Category

Refactorings that work with strings – Use IsNullOrEmpty

November 25th, 2011 Comments off

When checking that a user has provided a valid input, you will often want to ensure that it is not null or empty. Strings are reference types and can be equal to a null value like any other reference type. Strings can also be empty, meaning their values equal “” and they have zero length. The IsNullOrEmpty call of the System.String class indicates whether the given string is a null or an empty (“”) string. Before this call appeared in the .NET Framework, we were checking strings as follows (for example):

Refactorings - Use IsNullOrEmpty sample code

Read more…

Refactorings for concatenating and splitting strings

November 25th, 2011 Comments off

Concatenation is the process of appending one string to the end of another string. A string is basically a sequence of Unicode characters. An important property of strings is that they are read-only (immutable). Once a string has been created, it cannot be changed. So, when a string is updated, the .NET framework actually discards the original string and creates a new string. In other words, the concatenation of strings is creating an entirely new string, allocating enough memory for everything, copying all the data from the existing values of all concatenated strings and then copying the data from each string. As the string grows, the amount of data it has to copy each time grows too. Having already concatenated strings allows you to avoid such problems.

Read more…

Refactorings to convert between static and instance members

October 31st, 2011 Comments off

Classes allow you to create instance members and static members. Instance members are available when an instance of the class is created and have access to the object’s data. Static members do not require an object created and can be called directly.

Static methods are useful when creating functions that are not reliant on any instance of a class. An example of the extensive use of static members is the System.Math class, which is a library of mathematical functions and constants provided by the .NET framework.

Read more…

Refactorings – Make Extension Method

October 31st, 2011 Comments off

The Make Extension refactoring converts a regular method into an extension method based on the selected parameter type. Extension methods allow you to extend an existing type, including classes, structures or interfaces, with the new functionality, without having to derive from that class and without modifying the code of the type itself. The extended type is taken from the parameter that you choose when applying the refactoring.

Read more…

Creating a simplest refactoring using the RefactoringProvider control

September 30th, 2011 Comments off

In this article we will create the simplest refactoring which will remove the active comment. Sometimes, there are comments left that are not utilized any longer, such as implemented TO-DO comments, UNDONE comment that is now complete, etc. We will use the RefactoringProvider control shipped in DevExpress Refactor! Pro for this purpose.

Read more…

Refactorings for anonymous methods and lambda expressions

September 27th, 2011 Comments off

An anonymous method is a method without a name – which is why it is called anonymous. You don’t declare anonymous methods like regular methods. Instead, they get declared right inside a member as an inline code block. This is one of the greatest benefits of an anonymous method – it removes the requirement to create a named method elsewhere in the code, especially for simple operations. For example, you are able to pass an inline anonymous method as a delegate parameter, which means that you can use anonymous methods anywhere a delegate type is expected:

Refactor! Anonymous method sample code

Read more…

Refactorings for anonymous types

September 27th, 2011 Comments off

Anonymous types are type declarations that are generated automatically by the compiler without having to explicitly declare it. They provide a convenient way to encapsulate several read-only properties into a single object that is not declared in the code. Anonymous types are supported by C# and Visual Basic programming languages starting from Visual Studio 2008.

Read more…

Refactorings for implicitly-typed local variables

September 27th, 2011 4 comments

Implicitly-typed local variables are variables declared without specifying the type explicitly. The type of such variables is being inferred from the expression that is used to initialize the variable at the time the code is compiled. Implicitly-typed variables are really useful for LINQ that creates anonymous types in queries, and for which you want to assign variables. However, implicitly-typed locals can be used with any variable declaration to enhance the readability, for example:

Refactor! Implicit variable sample

Read more…