I recently started Mobile Application Development using for Android. The most fascinated thing I learnt so far is that we can afford to spare a single byte while developing a mobile app. Even every single function written is required to test for optimization to avoid memory leakage in fact this is what that makes developers realize importance of better software design.
Mobile Applications usually have limited memory allocated for example an Android Application cannot exceed 16MB of memory usage. I wonder that developing Enterprise Systems where we have Servers of several Cores and Memory available as much as we want, developers don’t pay attention to little memory leakage and bad software design.
Following are useful design techniques that can be used in Enterprise Systems as well and surely will give a boost to the System.
1. Constants Class:
Most of Enterprise Systems are designed on MVC. As View is used to display user everything including messages (Constant Strings), we usually fill out View with String Messages by making decision what to display in Controller. Now rather than using Strings in controller we can make a single Class containing all Constant String and just need to refer particular Constant while making decision what to display. It will also help in future if display String are required a change (which have more than 80% chance after 1st deployment as user only cares about what he sees).
2. Class level Object:
If only 1 Object of a class is enough for usage then there is no need to write “new” every time. For example usually we require only one Object of Database Class. In this case we just need to define Class variable reference at Class Level and initiate it in constructer and use in whatever manner we require.
3. Destroy Resources Immediately After Usage:
As soon as a resource (variable, constant, class etc.) becomes useless destroy it immediately to clear memory. In PHP unset() method will be helpful and in others just set the resource to NULL after it becomes useless and work is done.
4. Only Initialize Where Required:
In many Enterprise Frameworks I have observed that all reusable data is populated before calling Controller for ease but most of times it eats memory without giving any significant benefit. This may include System Level data objects, Global Objects and variables etc. We just need to only initiate an Object when it is required and destroy it immediately after usage unless required Object is actually being used in whole System.
5. Avoid Iterations
In pure MVC Model class returns data resource and after being processed (as required) by Controller it is rendered in View. If you observe most framework’s Model do a complete iteration on DataSet to convert it into Objects or any other form which is required to controller. Then controller do it’s processing (may be complete iteration in some cases) and then provide View The DataSet in View’s required form (array or objects). So a Generic MVC Open Source Framework takes at least 3 complete iterations on DataSet, the max figure will depend how bad a System is designed and most of the time we don’t even care about number of times a single DataSet is being iterated. This can be reduced using good System Design. If Model is written specific to System’s requirements then surely there will be no need to put checks in controller. For a perfect separate View there will be independent DataSet iteration required but we can reduce any checks or if/else conditions (e.g. for different row color, use JS rather putting server side checks). It may not seem much effective but it is indeed very useful.