Home > Development > blogs
Overview
When you think about writing efficient code, the first couple things that come to mind are speed and memory footprint. Although keeping your applications responsive and quick while maintaining a reasonably small memory footprint is definitely a key consideration when writing efficient code, you also have to keep in mind that in many cases you're either getting paid by the hour, or have a limited number of hours available to work on the application.
Since the ultimate constraint in many application development projects is money and time, there are several other things to consider in gauging the efficiency of code that you may not have thought about: applicability, reproducibility, and maintainability.
Before you read on, if you are simply here for speed and footprint efficiencies, I suggest you take a look at the following links:
- http://www.insideria.com/2008/03/flex-ria-performance-considera-1.html
- http://www.iheartair.com/?p=100
The first link is to a previous post in the series that contains a bunch of optimization links with plenty of code samples. The second link is to resources for a presentation on memory management and optimization containing a bunch of code samples. That being said, let's talk about applicability, reproducibility, and maintainability.
Efficient Code: Applicability
In the quest for speed and optimization you may often read blogs, books, and articles that other developers have written to learn new tips and tricks on how to write your code. During this quest you'll find all sorts of nifty new techniques to throw into your applications. Before implementing newly discovered "efficiencies" into your code stop and ask yourself: "Is this code technique applicable to my situation?"
Here's an example: Say you have an application that lists products. Although only a limited amount of the products can be viewed in the UI at any point in time, the end user can move from one end of the list of products to the next by using a scrollbar.
During the development of the above application you read someone's blog and learn about deferred instantiation in TileList. You discover that deferred instantiation makes memory utilization more efficient. However, before you implement a TileList to make use of the deferred instantiation "efficiency", ask yourself: "Is this code technique applicable to my situation?".
By asking the question and thinking about the application you realize an important client requirement is smoothest scrolling of list-based components at all costs. Deferred instantiation takes away from this requirement since the product tiles get instantiated when they first come into view as the user scrolls through the list which in turn causes slight delays the first time a product is shown. These slight delays take away from the smoothness of the scrolling which pretty much lets you know that this code should not be implemented in your application.
By asking the question of applicability, you've thought more thoroughly about application requirements and code. This process of thinking it through has allowed you to weed out unnecessary and potentially incorrect optimizations resulting in an end product that more thoroughly meets requirements and constraints set forth by the stakeholders.
Efficient Code: Reproducibility
Reading that some code technique is an efficient optimization is a lot different than knowing for a fact that the technique is an efficient optimization. When dealing with performance and memory footprint optimizations you need to make sure that any efficient code that you have discovered and deem applicable can be reproduced within your application with full confidence that the technique is truly an efficient optimization.
If you are unsure of a newly discovered technique, the first step of this verification process would be to establish a baseline by timing execution time or gauging metrics on memory utilization before you add the efficient code to your application. The key thing to keep in mind is to run the baseline test at least several times and take an average on the results.
Once you've established your baseline averages, implement the efficient code in your application and re-run your tests in the same manner to obtain another average. Now you can compare the first average to the second to see if the new code was proven truly efficient.
During the process of proving the reproducibility of efficient code optimizations, make sure that you continue to baseline and test additional code efficiencies throughout the development of your application.
Efficient Code: Maintainability
You strive to write perfect code. The unfortunate reality is that no matter how hard you try, there will be bugs in your code, or you will need to change the code you've written to meet new conditions and business requirements. In other words, having to revisit your code is pretty much an inevitability. Whether it be you or someone else needing to revisit the code, you need to make sure that any efficient code optimizations that had been implemented can be maintained in a relatively painless manner.
For instance, you have discovered that bitwise operators will save you a little time when doing mathematical operations in your application. Although bitwise operators are more efficient than standard mathematical operations, they are much harder to read and debug, even with ample comments. If the requirements of your application allow for a less efficient code that is easier to read and debug while keeping end users satisfied with performance and memory footprint, then opt for the less efficient code.
In most applications, readability is king. Although code may have to suffer slightly on efficiency if its makes it more readable and maintainable while still meeting the requirements of stakeholders, you and other future maintenance developers will benefit by saving time and headache when it comes time to debug or add new features.
Summary
There are many different techniques that allow you to make your code more efficient. Some links were mentioned at the top to take you to some of these techniques. However, when working with writing efficient code, there are three things to keep in mind: applicability, reproducibility, and maintainability.
Make sure code efficiencies are applicable to your application requirements. Ensure that the benefits of any new code efficiencies can be reproduced in the context of your application. Most importantly, make sure that your application is still reasonably maintainable after implementing efficient code.
In the next part of this series we'll be talking about display hierarchy and performance considerations to consider when working with the hierarchy.

Comments
Leave a comment