Home ยป pattern

pattern

Be Kind to Your GC: Examining IDisposable and Finalize Patterns

Through my discussions with various developers, I have noticed a common theme when it comes to disposing objects. Most developers are confused, or at least unclear, on why and when to use dispose and finalize.

Let me start off with this: The purpose of dispose and finalize is the same… to improve the performance of the application and the host system. You don’t need to implement either. When the GC runs, unused managed memory will be reclaimed. When your process exits, the entire process’s virtual memory will be released. When the system re-boots, any open native handles will be closed. Dispose and finalize are two patterns that make it easier for your application to be a “good citizen,” releasing memory as soon as it is no longer needed and closing handles as soon as you are done with them.
Read More »Be Kind to Your GC: Examining IDisposable and Finalize Patterns

Anti-Pattern and Code Smell Defined

I had pretty much assumed that nearly all software developers would know what I mean when I use the words anti-pattern and code smell. Over the last couple of months, I have come to the conclusion that this is not strictly true.

What is an anti-pattern?

When asking this question, I have received a number of different responses. Everyone seems to have a basic understanding, but most don’t have any clearer idea than “it’s bad code.”

To understand what an anti-pattern is, you must first know what a pattern is.
Read More »Anti-Pattern and Code Smell Defined

Object Cesspool Anti-Pattern

Object pools are a common pattern in development. The canonical example is probably a database connection pool.

If objects returning to the pool are not validated, the pool can gradually fill with garbage objects, turning it into a cesspool of unusable objects.

How to Identify

The two easiest ways to identify if your object pool may become a cesspool:
Read More »Object Cesspool Anti-Pattern

Input Kludge Anti-Pattern

As an anti-pattern, input kludge describes a poor user interface that either rejects or incorrectly handles valid input.

How to Identify

The most obvious code smell that may point to this pattern is ad-hoc input validation routines.

Look for:

  • JavaScript validation function defined directly in web page or view code.
  • Server-side validation routine defined in the presentation layer or the controller.

Example

$('form').on('submit', function(event) {
    if (!(/^.*@.*\.(com|edu|net|org)$/g.exec($('#email').val()))) {
        alert('You must enter a valid email address.');
        return false;
    }
})

Read More »Input Kludge Anti-Pattern

Code Smells and Anti-Patterns

What is an Anti-Pattern?

If you know what a pattern is, then you probably already know about anti-patterns, even if you didn’t know it! In a nutshell, a design pattern is an observed good method to solve a re-occurring problem. You have probably observed patterns in your own development.

For instance, maybe you have an object that needs to be accessed by multiple threads, but the integrity of the application requires that there be no more than one instance. In the countless times that this problem has been solved in the past, most “good” solutions have taken a similar form… what we now call the Singleton pattern.

Read More »Code Smells and Anti-Patterns