tag:blogger.com,1999:blog-318377542009-06-12T13:44:43.408-06:00Eric Liu's Software Development BlogMy learnings and discoveries around software development.Eric Liuhttp://www.blogger.com/profile/02486986466353329017noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-31837754.post-59662946336415838202008-03-25T21:03:00.000-06:002008-03-25T21:02:00.104-06:00Colorized NAnt Console Output<a href="http://bp0.blogger.com/_8GWGau2-Ph4/R8pz2G5kWEI/AAAAAAAAAAQ/YH-1EYotCF4/s1600-h/ConsoleColorLogger.gif"><img id="BLOGGER_PHOTO_ID_5173074495390636098" style="CURSOR: hand" alt="" src="http://bp0.blogger.com/_8GWGau2-Ph4/R8pz2G5kWEI/AAAAAAAAAAQ/YH-1EYotCF4/s400/ConsoleColorLogger.gif" border="0" /></a><br /><p>If you use NAnt for your build scripts, colored console output may be helpful in quickly spotting warnings or errors. I recently wrote a logger to do that:</p><ol><li>Download the source for the version of <a href="http://nant.sourceforge.net/">NAnt</a> you're using (I tested this against NAnt 0.85).</li><li>Apply <a href="http://twericliu-code.googlecode.com/svn/trunk/nant_console_color_logger/nant-rel-0-85-console-color-logger.patch">this patch </a>to the source tree. If you're on Windows, I believe you can use 'patch.exe' which comes with <a href="http://unxutils.sourceforge.net/">UnxUtils</a>.</li><li>Build NAnt by following the instructions in 'README.txt'. This will produce a bunch of files, but the only one we're interested in is 'NAnt.Core.dll'.</li><li>In the directory where you store your NAnt binaries (eg. on my current project, it's in MyCurrentProject/tools/nant), replace 'NAnt.Core.dll' with the new one.</li><li>The last step is to add an option to your 'nant' call to specify the use of the new logger:<br /><span style="font-family:courier new;">nant -logger:NAnt.Core.ConsoleColorLogger ...</span><br />On my current project, we have a 'build.bat' which invokes NAnt, so I just added the option there.</li></ol><p>Once you have this in place, you should start seeing colors when a warning or error occurs. Feel free to use this <a href="http://twericliu-code.googlecode.com/svn/trunk/nant_console_color_logger/IntegrationTest.build">sample build script</a> to test it out (remember to invoke NAnt with -debug in order to see the debug-level messages!).</p><p><strong>Customizing the colors</strong></p><p>Simply specify additional properties when you invoke NAnt like this:</p><p><span style="font-family:courier new;">nant -logger:NAnt.Core.ConsoleColorLogger -D:ConsoleColorLogger.error=DarkRed -D:ConsoleColorLogger.warning=DarkMagenta ...</span></p><p>The levels you can customize are one of error/warning/info/verbose/debug. The colors you can use are 'default' (which means to use the foreground color you had before invoking NAnt) or one of <a href="http://msdn2.microsoft.com/en-us/library/system.consolecolor.aspx">these</a>.</p><p>Hope that helps!</p><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/31837754-5966294633641583820?l=twericliu.blogspot.com'/></div>Eric Liuhttp://www.blogger.com/profile/02486986466353329017noreply@blogger.com9tag:blogger.com,1999:blog-31837754.post-1155537017682381602006-08-16T03:12:00.000-06:002006-08-21T10:35:38.650-06:00Why visitor pattern is worth the overheadI've observed that many developers tend to shy away from the visitor pattern. I suspect it's because even though they understand the pattern, the double-dispatch and the seemingly out-of-place <code>accept</code> methods seem like too much overhead. So, instead of using visitor, we end up with logic that switches on a simple enumeration or the type of an object. Compared to visitor, this kind of code is arguably simpler to understand.<br /><br />I think that visitors are worth the overhead. What we gain is the ability to easily identify the effects of type changes.<br /><br />Let's use this example: One part of our application captures user feedback. They have two options:<ul><li>Choose a feedback from a standard list (eg. "Good" or "Bad")</li><li>Submit a short sentence describing their usage experience.</li></ul><br /><br /><pre>// C# pseudocode...<br /><br />abstract class Feedback<br />{<br /> void Accept(IFeedbackVisitor visitor);<br />}<br /><br />class StandardChoiceFeedback : Feedback<br />{<br /> int Code;<br /> string Description;<br /> void Accept(IFeedbackVisitor visitor) { visitor.Visit(this); }<br />}<br /><br />class FreeFormFeedback : Feedback<br />{<br /> string Text;<br /> void Accept(IFeedbackVisitor visitor) { visitor.Visit(this); }<br />}<br /><br />interface IFeedbackVisitor<br />{<br /> void Visit(StandardChoiceFeedback feedback);<br /> void Visit(FreeFormFeedback feedback);<br />}</pre><br /><br />Somewhere in the system, we display a grid of data, where each row corresponds to a user and one of the columns shows their feedback:<br /><br /><pre>class FeedbackColumnRenderer : IFeedbackVisitor<br />{<br /> string RenderedText;<br /><br /> void Visit(StandardChoiceFeedback feedback)<br /> {<br /> RenderedText = feedback.Description;<br /> }<br /><br /> void Visit(FreeFormFeedback feedback)<br /> {<br /> RenderedText = feedback.Text;<br /> }<br />}<br /><br />string RenderFeedbackColumn(Feedback feedback)<br />{<br /> FeedbackColumnRenderer renderer = new FeedbackColumnRenderer();<br /> feedback.Accept(renderer);<br /> return renderer.RenderedText;<br />}</pre><br /><br />So far, all we've shown is the overhead incurred by using visitor.<br /><br />However, what happens when the requirements change? For example, our business analyst comes back in 3 weeks to inform us that on top of the two feedback options, there needs to be another option &mdash; the user can choose to leave their phone number so that a customer representative can give them a follow-up call to chat about their experience. We are asked to implement this change in the UI and the persistence layer.<br /><br />Our core types change like this:<br /><br /><pre>class <strong>FollowUpCallFeedback</strong> : Feedback<br />{<br /> PhoneNumber phoneNumber;<br /> void Accept(IFeedbackVisitor visitor) { visitor.Visit(this); }<br />}<br /><br />interface IFeedbackVisitor<br />{<br /> void Visit(StandardChoiceFeedback feedback);<br /> void Visit(FreeFormFeedback feedback);<br /> <strong>void Visit(FollowUpCallFeedback feedback);</strong><br />}</pre><br /><br />With just these changes, let's re-compile. What we find is that compilation isn't successful because all implementors of <code>IFeedbackVisitor</code> now have to implement the new <code>Visit(FollowUpCallFeedback)</code> method. On further examination of the compiler errors, we discover that not only are there feedback visitors in the UI and persistence layers, but there is also one used for an obscure report. We talk to our business analyst again, and find out that this report has been missed in talking about the change. Great! We then talk about what the report should look like when a phone number is involved.<br /><br />I think this illustrates the power of the pattern. When something changes in our type hierarchy, we are actually forced to address all the places which will be affected up-front. If we had chosen to use a simple enumeration or type switching scheme instead <em>and</em> we are not extremely careful, we might not discover all the places that are affected by the requirements change until much later (for instance, when the testers [or worse, the users] visually inspect that particular report).<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/31837754-115553701768238160?l=twericliu.blogspot.com'/></div>Eric Liuhttp://www.blogger.com/profile/02486986466353329017noreply@blogger.com2tag:blogger.com,1999:blog-31837754.post-1154161925652018512006-07-29T00:48:00.000-06:002006-07-30T02:57:29.846-06:00Why closure methods on collections matterI like the new <a href="http://www.martinfowler.com/bliki/CollectionClosureMethod.html">closure methods</a> (like <code>ForEach</code> and <code>ConvertAll</code>) on .NET 2.0 classes such as <code>List&lt;T&gt;</code> and <code>Array</code>, because from an OO standpoint, it really should be the responsibility of a collection to know how to traverse its own elements. However, I've always had a tough time justifying why you'd want to do...<br /><br /><pre><br />employees.ForEach(delegate(Employee e) <br />{<br /> Console.WriteLine(e);<br />});<br /></pre><br /><br />... instead of...<br /><br /><pre><br />foreach (Employee e in employees)<br />{<br /> Console.WriteLine(e);<br />}<br /></pre><br /><br />In this case, it doesn't look like you gain anything.<br /><br />Recently, I came across a situation which made me see the difference more clearly. Let's say you have a list of employees, and now, you want to remove all managers. Happily, we write...<br /><br /><pre><br />foreach (Employee e in employees) <br />{ <br /> if (employee.Manager)<br /> {<br /> employees.Remove(e);<br /> }<br />}<br /></pre><br /><br />This causes an exception because you can't modify a collection while you're iterating through it. OK, how about...<br /><br /><pre><br />for (int i = 0; i < employees.Count; i++) <br />{<br /> if (employees[i].Manager)<br /> {<br /> employees.RemoveAt(i);<br /> }<br />}<br /></pre><br /><br />This doesn't throw an exception, but also doesn't work properly because we're modifying the count in the loop and our index will skip over elements. OK, so the solution is...<br /><br /><pre><br />for (int i = employees.Count - 1; i >= 0; i--) <br />{<br /> if (employees[i].Manager)<br /> {<br /> employees.RemoveAt(i);<br /> }<br />}<br /></pre><br /><br />That's great! However, we now have to worry about remembering that a forward loop through a collection to conditionally remove elements semantically doesn't work. If you forget, no exception will be thrown, and only running unit tests or executing the program will reveal this.<br /><br />On the other hand, we wouldn't have to remember this little gotcha if we simply relied on the <code>RemoveAll</code> method...<br /><br /><pre><br />employees.RemoveAll(delegate(Employee e) { return e.Manager; });<br /></pre><br /><br />In a way, this situation is similar to why we don't hesitate to use the <code>Sort(IComparer)</code> or <code>Sort(Comparison&lt;T&gt;)</code> methods, versus getting the elements and writing our own sorting algorithm. Do we have to care how the collection's sort works? Generally, no, since that's the responsibility of the collection.<br /><br />FYI, check out the <code>List&lt;T&gt;.RemoveAll</code> method in <a href="http://www.aisto.com/roeder/dotnet/">.NET Reflector</a>. It's not using a reverse for-loop.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/31837754-115416192565201851?l=twericliu.blogspot.com'/></div>Eric Liuhttp://www.blogger.com/profile/02486986466353329017noreply@blogger.com4