Monthly Archives: March 2008

How exceptionality and naming are linked

Each method should have a clear, simple purpose. The name of the method describes this cohesive meaning. Exceptions denote behavior that breaks the cohesion.

The naming of .NET Parse and TryParse methods illustrates how naming and exceptionality are linked via cohesion:

// Parse promises to *parse* and thus not being able to
// parse is exceptional.
    double value = Double.Parse("1.5");
catch (FormatException) { /*...*/ }

// TryParse promises just to *try to parse* and not being
// able to is unexceptional.
double value;
bool result = Double.TryParse("1.5", out value);

Consequently, if you change the error handling mechanism of a method, the name of the method should probably change too.