- 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
Of course, reinventing the wheel is always a good idea. Why use the built-in translate function if you can build it yourself?
Admin
Yes, it passes unit testing and is obvious, so that's a tick. I'm old, I'll take working code over 'perfect' code any day.
If I saw that in code review it'd raise an eyebrow and the offender told that's it's not the best (and you'd look a bit more closely at their other code). If it got flagged in performance testing because you need to process 'War & Peace' 20 times a second, then you'd fix it.
It's just lazy-arse intern-level code, nothing to get excited about.
One job had a senior & junior programmer spend all day writing a simple function that already was in the code library (and was better than what they came up with), plus the DBA's thought it a good idea to format combo box lists in the stored procedures... now there's a real WTF.
Admin
Another Oracle PL/SQL developer here; I'd not use REPLACE or regular expressions here; I'd just go for translate(strText, '0123456789', '0000000000'). Bish, bash, bosh. wipes hands
Admin
It's stated clearly in the article that there are only 'numbers' - digits - in the string. Although I suppose e is a number, so excellent pedantry.
Admin
I remember way back having to edit some VB 5 code. The reference I was using said something about the language's "rich library of string functions", but I couldn't find its Replace() function and ended up writing my own (that's what stuck with me in the years since: a "rich library of string functions" that didn't include search-and-replace - I was sure I was missing something).
Having done that, if I had needed to make multiple replacements in one string I could very well have written something a lot like this (it's too long ago for me to remember).
Admin
That vaguely rings a bell, I think VB6 added a bunch of string stuff. Anyway, I do remember replace() was very slow so writing your own was a good idea.
Admin
The real WTF here is the use of VB.
Admin
For all that Jane or her co-workers know, the original author may have tried a couple of different looping solutions and some kind of regex call, and discovered that the nested replace call technique was significantly faster than anything else.
Talk to me when you've coded a more efficient solution and demonstrated the improvement.
Admin
Eh, I personally prefer the look of:
Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(strText, "9", "8"), "8", "7"), "7", "6"), "6", "5"), "5", "4"), "4", "3"), "3", "2"), "2", "1"), "1", "0")
But as said before: perfectly clear and serviceable code.Admin
Are we doing stupid ways to solve problem X? Function ReplaceNumbersWith0(in as String) as string for i = 1 to 9 in = Replace(in, cstr(i), "0") next Return in end function
Admin
I'm afraid that wouldn't compile.
Admin
Been there & got the t-shirt years ago.
Replace() in VB is very slow, like most of the string functions. When called it makes a copy of the string, processes it and copies it back. (In VB parlance ByValue rather then ByReference aka pointer). With 9 cascaded calls that's a lot of overhead passing string back & forth.
The code examples posted that do Mid$(String, Pos, 1) = "0" modify the string in place and are faster then the native function. For short strings it make not be worth bothering about though.
Admin
In C# strings are immutable types....
This is also efficient for larger strings.
Admin
I'll concede that I forgot about using TRANSLATE when I posted. In this case it'll work fine. Most of our cases we aren't doing a one-to-one replacement, we're replacing a multiple-character pattern, like chr(13) || chr(10) with ",", so usually REPLACE is the simplest way to handle it.
The code in the submission isn't the best, but it's by no means terrible. Just ugly. And let he who has not written ugly code cast the first stone.
Admin
[quote=Dave]It's stated clearly in the article that there are only 'numbers' [/quote]
No it is not! Did you read the article?
Admin
Well Dave, if you want excellent pedantry I'll refer you to the (mercifully short) article that starts:
Note that it doesn't say 'only' numbers. Nor does it say 'positive integers' either, so decimal points, commas & negative signs may be in there, possibly even parentheses & dollar signs.
More wary folk will say "Numbers eh? What sort of numbers?" Call us pedantic if you like, but our code will pass testing better than yours.
Admin
I got code from my working Test Java Project...
The main point was in using Algorithm for the Problem...
Admin
The real WTF is that the correct English isn't "replace ... with", it's "replace ... by".
Admin
The code might be ugly looking, but it is simple. If it's fast enough then I don't see a wtf?