- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Agreed -- real programmers write bytecode.
Admin
Admin
So, so true.
Admin
Real programmers design their coding languages, and install their programs on self-built computers using dip switches.
Admin
Whoa whoa whoa guys, is it just me or is the real WTF here using Char.Parse to get the characters for the split function?
Admin
Us VB.NET people would just use IsDate(date) anyway.
TryParse? Scoff.
Admin
the new code probably does not fulfill all requirements of the old: The check for dates before 1900 is missing. I am not sure if this check was really part of the original requirements or why it was there in the first place, but I guess .NET does not have the exact same requirement built-in
Admin
"Hoo" is this "u" to whom "euuurghhh..", sorry, "u" refer?
Admin
Oh really, try generating a graphic on the screen by pixel. No nice picturebox with pset.
Admin
And what if they wanted to mandate particular date format?
Isn't .Net's function trying to be smart, locale-dependent, etc? If so, I wouldn't rely on that (change of system language breaks dates? Ugh, that's WTF!)
Admin
You don't need your old compiler, MSDN clearly lists DateTime.Parse as 1.0 compatible. Since the code is written in C# there's really no excuse for having crap like this in your code base, unless it's some kind of automated port. Even then, manually refactoring is rarely a waste of time if you can get rid of huge chunks of cryptic crap that has no comments and is using RegEx's to boot.
protected bool CheckDateTime(string dateTime) { try { DateTime.Parse(dateTime);
}
... is 1.0 compatible.
Never? Are you serious? Do you know the overhead in maintaining code that deliberately departs from the simple and obvious? Write a few lines of code, write a comment, and move on. Unless you're programming the navigation software for the next mission to Mars, the overhead of try/catch is not a conversation 99% of programmers would ever need to waste time on.
Admin
I see that the "Char.Parse()" calls and the misplaced else in February (which I'd call a dangling else if it wasn't for those explicit braces in there) have already been mentioned...
There's also the
line, which looks broken due to the order of operations (&& groups tighter than ||) but, by luck, the code will actually still work (of course, if the same bug was on the check for the 30-day months, it'd fail).Also, matching the string with a regex, but not using captures within that regex, and then parsing it by hand.
I was also about to mention the overelaborate way of building chr[], but then I remembered, this is C#, not C++... I guess cstrings aren't available?
Admin
Then, of course, they go from forum to forum bitching that everyone in the world is dumber than them.
Admin
Yeah, let's save a few clock cycles at the expense of writing far less maintainable code. That's economical.
Unless you're developing apps that are CPU bottlenecked, where response time is critical, AND are parsing vast numbers of datetimes of which a significant proportion are invalid then using TryParse is utterly pointless.
The reality is that when you're parsing input like this you're almost never going to be CPU bound, so just parse the damn thing and catch the exception, it makes for much clearer and cleaner code and you won't get shot in the face by a maintenance developer a year from now.
Admin
Everyone in the world IS dumber than me. Still, I like to be productive.
Here's a nice example showing the performance gains from using TryParse. http://blogs.msdn.com/ianhu/archive/2005/12/19/505702.aspx
Admin
WTF? Are you seriously saying that TryParse is in any way unmaintainable? The least that you can expect from future maintainers of your code is that they know how to program and have read an API or two in their lives. Using Parse and unneccessarily catching an exception is not good practice. Exceptions are supposed to be exceptional. Sometimes, it would be exceptional for the parse to fail so use Parse and handle the exception appropriately and sometimes, it is likely that the parse will fail (for any number of reasons) so use TryParse and write much cleaner code. I'm not concerned about the performance of exceptions here as I don't think it applies in most cases (as you pointed out).
Admin
It obviously should have been call TryParseCatchEndTry, at least for the VB.Net guys here :-)
Admin
I'm saying that using "If" over try/catch leads to code that is less well factored and hence less maintainable.
Ignoring the clock cycles we both agree don't need saving - what's the difference between my unnecessary "catch" and your unnecessary "else"?
My input error handling code is nicely separated from the rest without many levels of nesting, it handles errors from attempts to parse all 8 dates in the BookingObject or whatever consistently and I have a nice "finally" block to cleanup any resources I might want to (.NET does have finally right?).
Admin
Gawd, reminds me of this hacked together VB6 timesheet system I had to extend. Not only did it do all the math manually instead of using the standard functions it had a ton of functions to do the same thing (half of it inline where it was needed instead of in separate functions), but slight differences (some versions taking leap years into account, others not doing, etc).
Kept wanting to rewrite or otherwise replace the thing but kept getting turned down but must have spent over a hundred hours over a few years making all kinds of changes that were requested and fixing bugs.
Admin
So unless you are misusing exceptions for non-exceptional things or doing premature optimization, there is no problem.
Admin
Admin
I never thought I'd end up defending a WTF, but the replacement code does not do the same thing as the original code. This might be a good or a bad thing depending on context, but it's a fact. For one, the original code is culture invariant and always expects dates to be in a specific format (ISTM en-US) and in accordance with the Gregorian calendar. Even if the TryParse, which is called without specifying a CultureInfo object, could be assumed to always accept dates in the given format, TryParse will also accept dates in other formats (and possibly calendars, e.g. Islamic) depending on the CurrentCulture. Some might be surprised to learn that there are cultures other than en-US.
So TryParse would return true where the original code returns false. If this was an actual parsing method, this might be benign. But since this can be used for validation, and the actual parsing might be done somewhere further down the line, this can be dangerous or at least annoying. A simple example: This method might be used to validate value dates entered into a system. The transactions are then passed on to an accounting system but the accounting system might not accept the dates in this format, yielding an error.
All that said, the method is still excessively verbose. The same functionality could be achieved by combining a call to TryParse with a suitable CultureInfo object && Regex.IsMatch if necessary. That might increase the LOCs for the replacement from 2 to 3 or 4, but that would still be no match for the original implementation.
Admin
Look at it this way: they will keep hacking at it, and at each other's work, until they get it right. Then 30 developers will go away educated and enlightened, and they will have had fun in the process. The disdain you see comes across that way due to the nature of the internet, which doesn't allow for facial expression or vocal inflection. Think of it as being like neighborhood pals playing some weekend football in their front yards and shouting friendly insults at each other in the typical male one-upmanship routine.
Admin
And this is why stuff like this exists:
Voila! Force your application to run in the locale you're expecting, if you really need it to.
Ofcourse, you could also build a customized DateTimeFormatProvider and pass that to the DateTime parsing methods if that's the only localized feature of the framework you want to force to a specific format.
Admin
Or you could just pass the desired CultureInfo's DateTimeFormat to DateTime.TryParse. That would be too easy, though.
Admin
We have some $45 million consultants to the same, except when they did DaysInMonth the remembered the poem "30 days hath September, April, May, and November." (read it carefully...one of these things does not belong...).
Admin
Good that you are not a .NET programmer
Admin
That's not the WTF. The WTF is smart alecks like yourself who think that code written in .NET 1.x when ported to .NET 2.0 should not be refactored.
If I saw such code, I would have outed the people who wrote it or those who ported it. Even in .NET 1.x, one could use the ParseExact. Parse has always been on the DateTime object since 1.0.
Admin
I find myself using the following pattern in a lot of code:
bool Action(sometype argument, bool reallyDoIt)
The boolean argument tells the function whether to actually proceed, should the action be possible; the boolean result tells the caller whether the action was possible. This is for cases where testing whether the action is possible requires the same code as actually doing it, except that testing bails out as soon as it notices a problem.
It's not very elegant but I haven't found a better general pattern yet.
Admin
Admin
Silly you, there's only 12 months on a year, not 99.
Admin
probably he just did a fast convertion of old codebase into hi-buzz super-nova .Net
and then "don't fix what is not broken" is quite a good path, if you realy do need a fast conversion, rather than rewriting app from scratch :-)
Admin
This is why we don't re-invent the wheel, I gather.. :P
Admin
1 - Everyone can answer here, professionals and amateurs, so you're mixing apples with oranges. 2 - The same task can be accomplished in many different ways, depending on the context. The less you know, the more clear you see the solutions (does it sounds good to you?) 3 - I'm sure that if I start to look into a website showing errors of accountants (and not beeing accountant myself, that's why I wouldn't understand all the options for the same task)... Do you ensure me that I'll find less incongruences? ;-)
Admin
Isn't it more straightforward to avoid calling a function if it may not do something? You realize that somewhere in each function you apply this pattern to there's a line to the effect of: "if(reallyDoIt) { ...[Actually do something]... }".
It seems a little more sane to me to do this: if( ActionIsSafe(argument) ) { Action(argument); }
If you really have that many places where you don't know if the code is safe to run until you've run it, you might consider reevaluating most of it.
Admin
I did something similar recently, in a JavaScript library.
Original:
Simplified:
The library that contains the original code is included in each and every page as part of a series of enterprise applications used by thousands of people around the world. Forget P2P; think of all the bandwidth bad code like this uses up! This is just one function in one library among many.
Let's not forget that this code isn't ready for i18n/L10n... None of it is, despite that most of this company's clients don't speak English as a frist language. When I brought up the idea of i18n with some decision-makers here, they looked dumbfounded (why would we want to do that?) or shocked (we can do stuff like that?).
Amazing...
Admin
Admin