Visit Sponsor

Written by 12:05 pm Clean Code

So You Want Cleaner Code? Return Statement Execution

I’m going to start an infrequent series on Clean Code examples. If you are enjoying this series, come to CFUnited 2010 and CF.Objective() 2010 to catch my presentation, Making Bad Code Good- Part 2, a live version of this series.

The main idea here is to provide practical examples of working code that can be written in a cleaner fashion. You may agree or disagree with what I’ve written, and I want to hear from you either way in the comments.

If you have a code sample you’d like to see refactored, send it to me through email (if you have my email already).

Code Sample







This 5 liner example isn’t too terrible, but we can certainly do better. The guiding principle in clean code is to make your code easy to understand for humans. In this case, the code isn’t too obfuscated, but it is a little overly wordy.

The intent of the above code sample is to return whether FindKey.Recordcount is greater than 0. This is handled by the initial conditional branch evaluation:



This evaluation is already as simple as it can be, so let’s attack the verbosity of the statement. Simple code is rarely measured in the smallest lines of code possible, but in this case we can shorten the code to clarify the intent.



The solution in this case is to take advantage of the return statement as an execution zone. We can put any evaluation we want to in the return statement, including boolean statements, function evaluations, anything we want really. After making this change to the original code sample, we can see the code is easier to understand.

Thoughts? Ideas? Opinions? Tell me all about it in the comments:

Visited 1 times, 1 visit(s) today
[mc4wp_form id="5878"]
Close