I was performing a code and one of the developers needed to return a success/failure result from a method and in the event of an error, return an error message. To solve the problem he used an out parameter like this:

The above snippet is a very simple version of the example in question but this is a big code smell for me. Personally, I hate out parameters and for me it’s a sign that the method is trying to do too much. My preferred way to fix this would be to return a custom enum and then use a custom attribute on the enum to define the error messages. In code we could then use an extension method on the enum to return us the error message. Using this approach means we can get rid of the out parameter and we don’t have all the error message tangled all over our solution.

To create a custom attribute for the enum we need to inherit from the attribute class.

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

MY EPISERVER BOOK

EPISERVER NEWSLETTER

Connect

Hi, I'm Jon, I write articles about creating and optimizing websites as well as making money from your online business. I am technical architect and technology fanatic by profession. You can find out more about me by signing up to my newsletter.