- 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
I'm amazed anyone thought I was serious.
Admin
What if there were no hypothetical questions?
Admin
Nope, the dev was absolutely right, because ISNUMERIC, in the case of a simple decimal string, will return FILENOTFOUND.
Admin
The quirky stuff will really get to you. Unless you're doing a forklift upgrade, you have to bring the quirks forward or risk^H^H^H^Hguarantee integration problems.
Note: I'm talking biggish projects here. If it's a small standalone, then maybe it's fine. It depends on the scale (and resources, importance, and all that other stuff too).
Admin
The Real WTF is, of course, asking the question. JFDI.
Admin
Despite the sheer number of comments, I'm disappointed to see only a few folks mentioning unit tests.
I once was tasked with translating large amounts of TSQL to PL/SQL and (like an idiot) started to do just that - translate. After a few "successful" translations had produced PL/SQL which was a pile of steaming ordure, I learned better. Line by line translation is the kiss of death, as it prevents the use of best methods in the target language (in my case PL/SQL) by simply duplicating what is efficient in TSQL. For example, always opening explicit cursors when an implicit will do, is an excellent way to waste memory and CPU, and remove the inherent checking of an implicit cursor.
Can someone tell me how to resolve this one? My translated code is right, and I corrected a bug in the original in the translation - see attached output from unit tests, demonstrating original bug and new corrected version. Since nobody has unit tests for the original, who (from a position of information and knowledge) is correct? Or, as the supposedly shrewd manager, do you want me to deliberately write buggy code, and are you willing to sign off on any and all liability arising therefrom? Since when has translating a bug been deemed "good programming"?
Answers on a post card to a British postal code involving both letters and digits.
Admin
Alas, this was not correctly coded if you are at all interested in efficiency. I have needed to perform similar numeric filtering in the past, and this works out to be the fastest, most efficient way:
-- If any character is not in zero to nine IF @barcodeValueSequence LIKE '%[^0-9]%' -- it is WRONG, WRONG, WRONG! BEGIN Select 'Non-Numeric Sequence Provided' Return End
Admin
Please, allow me.
Your comment seems to assume that the SQL Server is only running labels. If it is performing anything more than this trivial task, then optimization is called for.
Sloppy code kills servers when they should still be capable of handling the load.
Admin
Good post. On this point I'd say probably never, but it has often made good business sense. As an example, Wintel was stuck with 16bit code for a long time. If they had gone through and cleaned up all that crusty code, it would have caused all kinds of legacy apps to quit working.
Companies tend to re-evaluate platforms when they are required to replace all their apps on an upgrade. While the buggy code is and was incredibly frustrating, Wintel was able to retain market share.
In your or any particular case, though, the answer is "it depends".
Admin
Casting a varchar to int is not so straightforward in sql2000 see http://stackoverflow.com/questions/2088642/sql2000-safely-cast-a-varchar256-to-int
excepted solution still jas edge case where it crashes.
i had thought someone would mention the ISNUMERIC(@str+'.0e0') trick to check for integers only.
Admin
Unless budget or time contraints say otherwise, I'd propose those steps: a) create a 1:1 translation, stick as close as possible to the original (even when you think this really suuuucks) b) create unit tests for the translated version. For unit tests that fail, use the original program as a reference to verify which one is wrong: the translation or the unit test. c) when all unit tests are satisfied, run the acceptance tests. make sure the new program behaves like it should, i.e. like the old one d) once c) is done, start to refactor the code to make it better (i.e. more like "real" PL/SQL). Of course, the new code must satisfy the unit tests, too e) re-run the acceptance tests to make sure you haven't broken something that the unit tests didn't notice
Admin
Here's an improved implementation in TSQL.
IF @barcodeValueSequence LIKE '%[^0-9]%' BEGIN SELECT 'Non-Numeric Sequence Provided' RETURN END
Admin
Shouldn't the original coder have added comments such as "isNumeric isn't precise enough since ...." or "we have to iterate through the individual digits because ...."
That makes me think of....: I know a girl who was a developer for a company. She told me that comments in their code were FORBIDDEN. Why? Probably because some developer might modify the code and not the comments, and thus the comments would confuse more than they'd help.
Admin
There is no WTF here. This is good practice on behalf of the manager, and highlights Andy's inexperience in the real world. To minimize risk, the code should be ported as close as possible. This validates that SQL Server behaves the same as Oracle. Improvements/refactoring should then be made, and that version compared to the originally ported SQL Server version - this proves the refactoring has worked. If Andy ports and refactors simultaneously and the code doesn't work, there is nothing for the project to fall back on if time constraints dictate.