Geek Proud, GeekOut.

Which way is the Exit?

I thought I would try something new for content, and thought every now and then I can throw out some very useful programming tips. I would love to be able to embed code directly into a post and that’s something we will look into but until then we shall be using external links to display and show code to you.

As a programmer you learn certain things over time, books can teach you only so much and there is nothing quite like learning from your mistakes. I thought it might be interesting for some of you to see why certain strategies are better to implement than others. The strategy that we will tackle today I have named “Exit Early”. Now I have no idea if this is the official name for this particular programming tip but it is my name for it.


The idea with “Exit Early” is that you always aim to program the shortest path first. This generally will stop you getting into a very heavily nested code and a very difficult path to follow. To try and make an example of this I am going to give you a bit of a situation. Below is what is known as pseudocode, this is basically just a text description of what your program is going to do, have a quick read through

  1. Has there already been a debug level set?
    1. No?
      1. Is there a system debug level set
        1. No
          1. Grab the required debug level from the user
          2. Set the value in the system
          3. Return the value set
        2. Yes
          1. Set the value in the system
          2. Return the value set
    2. Yes?
      1. Return the value set

Well if you were given a problem like this as a developer and you did not have much experience then your first instinct may be to just follow the instructions and make the program do exactly as you were asked. There would actually be nothing wrong with that approach and yes you would get the job done. However, it may not produce a very maintainable bit of software due to its nested complexity. To help demonstrate what I mean I have pieced together a little bit of HTML and Javascript using a tool called Trinket. Take a look the example code and try changing the debugLevel variable in the exitEarly.js. What happens if you change the systemDebugLevel variable as well? Can you easily understand what is going to happen?

You can maybe see from the code that it soon begins to look complicated. Take a look at the requirements again and the first thing I would change is the initial check for the previously set debug level. If you check for the positive first you can start to apply an “Exit Early” strategy.  Now because I can’t actually demonstrate code in here you’re going to have to look at a second example. It’s important to note that the HTML has not changed and neither has the displayDebugLevel function. The initial test to see if there has been a level set has been made the opposite of what it was. You have to remember that when you use a return the function will immediately return the value to the thing that called it and not to go any further. Now I am not saying that this newer version of the code is perfect but I personally find it a lot easier to follow. You can hopefully see that the “Exit Early” strategy has made the code a lot less nested which makes it generally less prone to bugs and logic errors.

So there you have it, a first little experiment from me. How did you find it? I’m really eager to get your feedback and believe me when I say I welcome all questions. This post is aimed at somebody who might know something about programming and wants to get better so did this help you at all? Would you like to see more tips on programming in general? Maybe you would prefer it if we can look into a tool to help us embed code directly into the website? Give us your feedback and maybe we can do more of these in the comments section or via our Reddit, Twitter or Facebook.


Drop us a line

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.