Keige inherited some code which seems to be part of a drawing application. It can load brush textures from image files- at least, sometimes it can.
static public Brush GetImageBrush(string serviceCode, string imageName, string language)
{
Brush BorderChannelGroupBrush;
BitmapImage image = null;
int point = imageName.LastIndexOf('.');
string languageImagename = imageName.Substring(0, point) + "-" + language + imageName.Substring(point);
try
{
image = FrameWork.ServicePageImageUrlOnContentServer(serviceCode, languageImagename);
}
catch { }
if (image == null)
{
try
{
image = FrameWork.ServicePageImageUrlOnContentServer(serviceCode, imageName);
}
catch { }
}
if (image != null)
{
BorderChannelGroupBrush = new ImageBrush(image);
}
else
{
BorderChannelGroupBrush = Brushes.White;
}
return BorderChannelGroupBrush;
}
There are a lot of interesting “choices” made in this code. First, there’s the old “find the last ‘.'” approach of grabbing the file extension. Which is fine, but there’s a built-in which handles cases like when there isn’t an extension better. I think, in this case, it probably doesn’t hurt anything.
But the real fun starts with our first attempt at loading our image. We jam a localized language string in the middle of the file name (foo-en.jpg
), and try and fetch that from the server. If this fails, it throws an exception… which we ignore.
But we don’t fully ignore it! If the exception was thrown, image
doesn’t get set, so it’s still null. So we do a null check, and repeat our empty exception handler. If the image is still null after that, we default to a “Brushes.White” image.
It’s all a very awkward and weird way to handle errors. The null checks bring with them the whiff of a C programmer checking return codes, but I don’t actually think that’s what happened here. I think this was just someone not fully understanding the problem they were trying to solve or the tools available to them. Or maybe they just really didn’t want to deal with nesting.
It’s hardly the worst code, but it does just leave me feeling weird when I look at it.
Keep all your packages and Docker containers in one place, scan for vulnerabilities, and control who can access different feeds. ProGet installs in minutes and has a powerful free version with a lot of great features that you can upgrade when ready.Learn more.
Source: Read MoreÂ