No Comment….

no_commentSo Mr Salopian… How are things going with this ‘No Comment’ policy. Well, says I (looking to the sky and wishing on the long dead bones of Darwin that I actually believed in some higher being) good and bad. I’m not really struggling myself with the concept but I DO struggle with legacy code that is unclear in its intent. Yes, You find me still still battling with the demons of Dicom and converting our Leadtools v14.5 application to the latest and greatest version and in the process coming across some…. challenges. I’m not going to bitch about the parentage of the person who wrote the code or suggest that they should be prevented from accessing a computer ever again or anything like that. The truth is, I worked closely with the guy for about ten years and we had a great working relationship with a lot of mutual respect. We could both rely on each other in a jam and could step in and out of each others code without scratching heads too much.

However, even the best of us somehow take things for granted sometimes. Who’d think that 7 years down the line someone would want to understand or refactor ‘THAT’ piece of code! So I came across the following fairly innocuous lines of code:-

rasterFxd.DrawPenStyle = (short)penStyle;
rasterFxd.DrawPenWidth = (short)penWidth;
rasterFxd.DrawPenColor = (uint)penColor;
rasterFxd.DrawMode = (int)LTRASTERFXDLib.DrawModeConstants.DRAWMODE_COPY_PEN;
rasterFxd.DrawLine(raster, 0, xStart, yStart, xEnd, yEnd);

OK so for a start none of this is pertinent anymore to the Leadtools 19 libraries. The functionality within the LeadRasterFXD library can all be achieved through the GDI+ libraries but that was not my problem. My real  issue was with the penStyle value as looking through the code I could see that it could either be 1 or 0. That meant nothing! Comments in the code indicated that 0 was solid line but no such comments were provided for the 1 value, for all I knew it could be a ‘wibbly wobbly timey wimey thing‘. This was vexing but then as I thought about it even more I realised that the commented value could easily have been incorrect as well. So I now knew that I had precisely nothing to go on. Its fine, the internet is your friend except…. it really wasn’t on this occasion and I could not even find what possible values could be utilised within the 14.5 help files. I was fortunate enough to stumble across a post by some other unfortunate who had experienced different issues in the past. He had posted a nice snippet of code which pointed me in the right direction. There was a set of constants defined in LTRASTERFXDLib.DrawPenStyleConstants that gave me all of the values I could use. I was now able to finish the refactor thus:- using (RasterImageGdiPlusGraphicsContainer rasterGdi = new RasterImageGdiPlusGraphicsContainer(raster)) {
using (Graphics g = rasterGdi.Graphics) {
Color brushColour = Color.FromArgb(penColor);
using (Brush brush = new SolidBrush(brushColour)) {
using (Pen pen = new Pen(brush, penWidth)) {
if (penStyle > 0) {
pen.DashStyle = DashStyle.Dash;
}
g.DrawLine(pen, xStart, yStart, xEnd, yEnd);
}
}
}
I think this example very neatly details the reasoning behind the self documenting ‘No Comment’ code approach. Making use of an enumeration (or even the constant declarations) in the first instance would have prevented the confusion and the commenting (scant as it was) only served to further confuse what was happening in the code. Ah well, you live and learn….Well, you live anyway!

Advertisements