I wrote a blog on debug or logger class in Objective-C about couple months ago. Actually, that class was based on my prevous .net debug class. It has a feature to print debug information in a nice indention layout, which was extended to my Objective-C class. I think my .Net is very useful, at least for me, and it is more generic, with an additional feature to calculate a value between paired messages(indent and un-indent).
Background Story
I was assigned to a task to resolve a painful slow performance issue of a .Net Windows application project. I did not spend much time to understand the complete business logic of the application. I went to the issue directly. What I did was to create a debug class to print out time and duration of each method or event call. Based on that information, I then focused on the longest duration calls to find out ways to improve the performance issue. The task was finished in very short period of time and the performance was greatly improved. My debug class is a very generic and useful tool. I have used this in my other projects with great success. With this experience and design, I created a similar logger class in Objective-C when I started iPhone development.
The intension to create this debug class is mainly based on three considerations. First is to generate debug message with duration information for each method or event call. This is the main reason and it is very easy to do. This could be done without a class. However, I would like to have debug message in a consistent format or indention. A class can encapsulate the implementation and provide unified interface. This second consideration actually was trigged by System.Diagnostics.Debug class, which has a nice property IndentLevel and a method Indent.
The third consideration is the maintenance. Since the debug calls are spread out all over the places in a project, it will be really hard to clean up them when the project is finally released. I would like to keep the debug feature or codes in my project without removing them, but also have least impact on its performance. As you will see, I have introduced delegates and early checks to avoid unnecessary calls if debug is disabled.
Delegates Used in the class
I like to use delegates as a way to pass custom implementation to my class. The first advantage is that it follows Open/closed-principle. It opens for extension or plug-in, but close my class for any modifications. The second advantage is to improve performance. Delegates normally used as parameters. They are pointers to functions or methods. If a delegate is not called for execution, its internal codes will never be evaluated. The following delegates are defined:
public delegate T GetValue<T>(); public delegate string GetCalculatedValue<T>(T value1, T value2);
The first one is a delegate to get a value. The delegate provides a way to let client to define his/her implementation to get a value. For example, for duration case, a delegate can be defined just returning a value of
DateTime.Now
.The second delegate is for calculate a value based two parameter values, from value1 to value2.
0 comments:
Post a Comment