Archive

Author Archive

The Lock to Try/Finally refactoring

August 27th, 2012 Comments off

Locking is essential in programs with multi-threading. It restricts code from being executed by more than one thread at the same time. The lock statement gets the exclusive monitor for an object and ensures that no other thread can access the object until the lock is released.

The lock statement automatically generates exception safe code, and in fact it is a syntactic shortcut for a call to the methods Monitor.Enter(obj) and Monitor.Exit(obj) with a try/finally block. However, if the thread is aborted after the lock is acquired but before entering the try block, the lock will not be released. Also, bear in mind that the Monitor.Enter() waits to acquire the lock forever which may introduce a possible deadlock condition.

Read more…

Refactorings for converting loops: ForEach to For and back

August 27th, 2012 Comments off

Let’s take a look at the two CodeRush refactorings allowing us to convert a for-loop into foreach-loop, and a foreach-loop into for-loop, and compare these two loops to see what the difference is between them and which one is preferred.

The for-loop is useful for iterating over elements and for sequential processing. It uses an iteration variable and is good when iterating over many items sequentially with a precise condition of the iteration termination. No collection of elements is required, but we can use the iteration variable to index a separate collection. In its basic form, the loop contains three sections:

Read more…

Implementing the IDisposable pattern using CodeRush

August 15th, 2012 4 comments

The IDisposable Pattern is used to release and close unmanaged resources such as files, streams, and handles as well as references to managed objects that use unmanaged resources, held by an instance of the class. For classes that require a resource cleanup, we recommended following the IDisposable pattern, which provides a standard and unambiguous pattern. The pattern has been designed to ensure reliable, predictable cleanup, and to prevent temporary resource leaks.

Read more…

Coding Helpers – Add Initialization

August 15th, 2012 Comments off

The Add Initialization code provider inserts an initialization of the selected field or auto-implemented property to each constructor of the active type. The code provider is useful when you want to initialize those objects with a different default value:

Read more…

Coding Helpers – Add Else Statement

August 15th, 2012 Comments off

The Add Else Statement code provider performs the operation its name implies – it generates the ‘else’ statement of the current conditional statement. The ‘else’ statement is required when you want to execute a piece of code if a condition is not met. In other words, the ‘else’ is useful when an alternative case is required on the ‘if’ statement.

Read more…

Coding Helpers – Add XML Comments

August 14th, 2012 Comments off

Programming languages such as C# and Visual Basic support the creation of XML Documentation Comments, allowing developers to quickly annotate and document their source code to keep the documentation in a standard format and to gain benefit from the information as you code. Documentation is important to ensure that developers can quickly learn and use the source code.

Read more…

Refactorings for changing member signatures overview

August 9th, 2012 Comments off

This is just a quick list to organize a bunch of refactorings that change member signatures. Click the refactoring name to learn more about it.

Add Parameter

The refactoring adds a new parameter to a method declaration and updates all calls accordingly. This refactoring is useful when you need to quickly add a new parameter to an existing method because it needs more information from its caller that wasn’t passed in before.

Read more…

Code Issues specific for declaring variables and constants

August 9th, 2012 Comments off

In addition to code issues specific for declaring types, CSharp and Visual Basic language specifications have a few restrictions on declaring constant or simple variable declarations. Having this issue in the code editor right in front of a developer allows him to fix the issue immediately without compiling the project.

Read more…