Geeks With Blogs

News Hi, my name is Vincent Grondin and I'm a senior consultant at Fujitsu, a consulting firm in Montreal, Québec. I'm starting that blog to share some of my thoughts and knowledge on .NET architectures and code. Being a consultant in the .NET world I come across many different things. Some good, some bad and others that are worth a blog post here whether they be good, or hmmm... shall I say, less good :) I hope you enjoy yourself while learning new stuff. Feel free to leave a comment or contact me anytime.
Vincent Grondin


So I’m looking at an application that I need to update for a client and I stumble on a piece of code that works (no denial there it does work) but shouldn’t be so obscure… I know we all have written code that was sub-par at some point in our life and to some extend we sometimes still write some for various reasons, laziness being the most common IMO. This functionality is a classic… being able to tell if a description starts with a particular string, more precisely in this case there is a list of potential search patterns to validate. Now there is a catch to this… if a certain flag is raised then the search must return TRUE even if the description does not start with any of the search patterns in the list…. I know it’s weird but that’s the business of this app… So most devs would have either used LinQ or wrote a simple loop that iterates through the list of search patterns and do a StartsWith() on the description… Don’t forget the special flag tho! Easy we’ll simply skip the whole loop and return true if the flag is raised… It’s exactly what they did, a loop but I couldn’t find where the flag was managed… It wasn’t inside the function around the loop and it wasn’t around the only call to the function to skip it… At first I thought the dev forgot the flag but when I tested the app I saw it really behaved correctly when I clicked the checkbox associated with that flag…. So I searched for that flag (find all usage). Sure enough I found it a few hundred lines of code north of the call to the search method and it was the only place in the code base where the flag was actually used. I knew I was at the right place… but what I saw baffled me… Remember that list of search patterns? Well, when the flag was raised, the dev simply added an empty string to the list of search patterns… Yall should have seen my face at that moment, I was flabbergasted. There was no comments in the code so instinctively jumped to msdn on the StartsWith method and sure enough, in the remark section there was this piece of information stating that StartsWith would return true if an Empty string was used... I’m still in shock as I’m writing this… Being a big fan of the “If it ain’t broke, don’t fix it” principle, I did not correct that “intentional code obfuscation” but I commented it and later read a nice piece of info on SO regarding this

Happy coding all and remember, when that urge to use an obscure coding hack arise in the future, think about poor ol’Vince having to debug this code a few years from now and…. resist!

Posted on Sunday, July 6, 2014 10:10 PM | Back to top

Comments on this post: .NET hacks you shouldn’t be using in your code

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Vincent Grondin | Powered by: