0

Best Practices For Coding (Accessibility)

Posted by Danielle Smith on 14:02 in
Good afternoon everyone!

Today I will be continuing on with my mini series of blog posts on the best practices for coding. If you haven't done so already, I strongly recommend that you read the blog posts already written in this series, which can be found below:

Best Practices For Coding (Intro)
Best Practices For Coding (Readability)

What is meant by "Code Accessibility"?

Code accessibility means that all parts of the code solution are accessible on execution, even if there is only a remote chance of that particular part of code being run. The idea is to remove any "unreachable code" (or preferably plan ahead and avoid it altogether to begin with!) as it:

  1. Takes up memory usage unnecessarily.
  2. Causes unnecessary caching as the code would never be run. 
  3. It takes up database time and effort to maintain even though it will never be run. 
Generally, I haven't seen much of this in my current work place as we are good at planning ahead and designing our functions before we start coding. We never code completely blindly as this is where problems start to occur.

It's also important to track changes in your application, so if functionality is modified it may make some parts of existing code redundant. If this is the case, don't leave it in there active just comment the lines out. This way, the code will not take up any additional memory or caching, plus if you need to go back and re-implement the code you can do so with very little difficulty.

Example of Bad Code Accessibility

Below is an example of code that has poor accessibility (as the condition will never be met providing the code remains the same as it is):










Note that if there is a condition that could possibly be met (though it is very unlikely to be met but there is a chance it could be), this is not an example of unreachable code. If it is physically impossible for execution to reach a particular part of code and therefore leaving it completely redundant, that is unreachable code and a demonstration of poor code accessibility. As a developer, you want to produce code that not only works, but performs well.

In SQL, occasionally error messages pop up saying that there is unreachable code. However, this shouldn't be relied on completely as I have noticed on occasions where SQL Server tells me that the command has been completed successfully, when actually it didn't even do anything! Be warned and make sure you read your code thoroughly so you can follow it through.

Conclusion

As a conclusion, I hope you have seen the importance of code accessibility and the impact it can have on the performance of your code (and queries). Obviously, your code will still work without being completely optimized, however why leave redundant code that will never be used? Along with maximising your code's readability, your queries, stored procedures, functions and subroutines will look and perform much better.

What's Next...

Stay tuned for tomorrow as I will be discussing code re-usability, something which should always be at the back of a developers mind when they are programming anything as you don't want to waste time and effort on an overcomplicated solution when they can reuse subroutines and functions that have already been written previously! This post will be the last within this mini series so don't miss out.

Thank you for reading today's blog post! If you have any questions/comments/feedback, please leave them in the comments section below and I will get back to you as soon as I can. Alternatively, please like my SQL Genius Facebook Page and leave a message on there. Thanks! :)

0 Comments

Post a Comment

Please post any feedback or comments here...

Copyright © 2009 SQL Genius - Personal Development of a Junior All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.