[.net] Over abused methods

Started by
1 comment, last by JvdWulp 14 years, 3 months ago
Hello everybody, What the most commonly over abused methods you came a cross? For me it woulde be String.Format, I have never seen propper use of that function it always seems to get abused. One of the worst uses I have ever seen was in an asp.net project:

String.Format(datastring, errorstring);
Where datastring would contain various information from the client concatenated and even the data from some textboxes. datastring would always contain an {0} to which was supposed to get replaced by errorstring. errorstring was just a general error message. Now some of you may have already noticed the problem. Because various other strings were pasted together there could contain special symbols cousing the Format method to throw exceptions. Another bad case of String.Format I came across was:


static void Main(string[] args)
{
string root=args[0];
Directory.GetFiles(String.Format("{0}SomeFolder/Blah", root);
}
root contains a string that got fetched from the command arguments. A good case for Path.Combine.
Advertisement
Quote:Original post by JvdWulp
Another bad case of String.Format I came across was:

static void Main(string[] args){string root=args[0];Directory.GetFiles(String.Format("{0}SomeFolder/Blah", root);}


root contains a string that got fetched from the command arguments. A good case for Path.Combine.
The real problem there is that args[0] is not your executable's location. Doesn't matter whether you use Path.Combine or not since args[0] isn't even guaranteed to exist and doesn't automatically contain your assembly's location. Heck, even if someone was passing in the path, it'd just be better to use the Assembly class to find it to avoid errors.

Though not a method, the thing I still cringe over is anyone using non-generic collections. There is, in my opinion, no reason to use a non-generic collection unless you're specifically targeting .NET 1.1 (which I also don't understand since .NET 2.0 is substantially better and supported on, as far as I know, every operating system that supports .NET 1.1).
@NickGravelyn: Maybe I should have stated that I left those checks out of the example to keep it short.

I like generic collections too. :-)

This topic is closed to new replies.

Advertisement