Comment On The Long Road to Clean Up

One of Joe J.'s first tasks at his new job was to "clean up" some existing code. We always hope for the best in these situations, but since the code has ended up here, you can tell where this story is going. [expand full text]
« PrevPage 1 | Page 2Next »

Re: The Long Road to Clean Up

2007-01-11 09:06 • by SomebodyElse (unregistered)
You mean like this


result = parts[parts.length -1];


~Tim

Captcha: creative -- Not Very!

Re: The Long Road to Clean Up

2007-01-11 09:07 • by joe.edawrds@imaginuity.com (unregistered)
string strPoster = "a/b/c/d/e/f/g/h/fist!";
string retval = "";
foreach( string s in strPoster.Split( "/" ) ) retval = s;
Console.WriteLine( retval );
// CAPTCHA: craptastic

Re: The Long Road to Clean Up

2007-01-11 09:20 • by dave_v (unregistered)
110963 in reply to 110962
joe.edawrds@imaginuity.com:
string strPoster = "a/b/c/d/e/f/g/h/fist!";
string retval = "";
foreach( string s in strPoster.Split( "/" ) ) retval = s;
Console.WriteLine( retval );
// CAPTCHA: craptastic


You need to write shorter code to achieve that goal.

Re: The Long Road to Clean Up

2007-01-11 09:26 • by lorna (unregistered)
Looks like the predecessor was bored at work (we've all been there at least once) and was entertaining him/herself by finding creative ways to do the mundane.

captcha: java (Hey! I resent that, Alex!)

Re: The Long Road to Clean Up

2007-01-11 09:31 • by striker
The .NET framework has a handy getFileName method
Where? All I know is
new System.IO.FileInfo(fullpathname).Name;

Re: The Long Road to Clean Up

2007-01-11 09:32 • by meh (unregistered)
too bad that also / can be used as a path separator...

Re: The Long Road to Clean Up

2007-01-11 09:35 • by IceFreak2000
110969 in reply to 110966
striker:
The .NET framework has a handy getFileName method
Where? All I know is
new System.IO.FileInfo(fullpathname).Name;



System.IO.Path.GetFilename(fullpathname)

Re: The Long Road to Clean Up

2007-01-11 09:48 • by Michael (unregistered)
I've never used .Net, so can someone tell me what the @ in front of the string means?

Re: The Long Road to Clean Up

2007-01-11 09:49 • by wonko (unregistered)
110973 in reply to 110967
well, with .NET it is quite okay to trust on backslashes as path separator I guess...

Re: The Long Road to Clean Up

2007-01-11 09:50 • by D (unregistered)
I don't get the WTF.

Re: The Long Road to Clean Up

2007-01-11 09:51 • by derobins (unregistered)
110975 in reply to 110972
Michael:
I've never used .Net, so can someone tell me what the @ in front of the string means?


It means you don't interpret \ as escaping another character. @"\n" is interpreted as the string containing two characters '\' and 'n' and not a string containing a single newline.

Re: The Long Road to Clean Up

2007-01-11 09:58 • by Fabian (unregistered)
110976 in reply to 110973
wonko:
well, with .NET it is quite okay to trust on backslashes as path separator I guess...


Not if you want to use mono in a linux environment.

Re: The Long Road to Clean Up

2007-01-11 10:00 • by leeg (unregistered)
110977 in reply to 110972
Michael:
I've never used .Net, so can someone tell me what the @ in front of the string means?


It means someone was thinking of Cocoa when they wrote the framework,

Re: The Long Road to Clean Up

2007-01-11 10:05 • by nmx
110978 in reply to 110974
D:
I don't get the WTF.

To name two:
- Reimplementing a system function
- Iterating through an array and repeatedly overwriting a variable, instead of just assigning it once (see the first reply)

Re: The Long Road to Clean Up

2007-01-11 10:12 • by striker
110979 in reply to 110969
IceFreak2000:
System.IO.Path.GetFilename(fullpathname)
Thank you.

Re: The Long Road to Clean Up

2007-01-11 10:12 • by joe (unregistered)
Yeah the loop is a real WTF, but due to the mass of lib functions in .net, I am sure most of us reimplementet at least one of them.

J.

Re: The Long Road to Clean Up

2007-01-11 10:17 • by XIU
Not to mention the use of Regex instead of just fullPathName.Split(Path.DirectorySeperatorChar), the use of ToString() on probably a string and ofcourse the overwriting of the same variable to keep the last one.

Re: The Long Road to Clean Up

2007-01-11 10:21 • by benryves
110982 in reply to 110972
Michael:
I've never used .Net, so can someone tell me what the @ in front of the string means?
To clarify, that's nothing to do with .NET. It's a feature of C# string literals.

Re: The Long Road to Clean Up

2007-01-11 10:36 • by Anonymous (unregistered)
110986 in reply to 110975
derobins:
Michael:
I've never used .Net, so can someone tell me what the @ in front of the string means?


It means you don't interpret \ as escaping another character. @"\n" is interpreted as the string containing two characters '\' and 'n' and not a string containing a single newline.


Why does he use @"\\" then, shouldn't that be either @"\" or "\\" to separate paths? Could be just a typo in the article though?

Re: The Long Road to Clean Up

2007-01-11 10:38 • by Jon Skeet (unregistered)
110987 in reply to 110986
Nope, he wanted two backslashes - this is a regex, don't forget.

Re: The Long Road to Clean Up

2007-01-11 10:42 • by audiedog (unregistered)
I found this in my first weeks at a new job:


While (endCut <> True)
endCut = True
If InStr(fileName, "\") <> 0 Then
endCut = False
fileName= fileName.Substring(InStr(fileName, "\"))
ElseIf InStr(fileName, "/") <> 0 Then
endCut = False
fileName= fileName.Substring(InStr(fileName, "/"))
End If
End While


Which, of course, can be replaced by:

filename = IO.Path.GetFileName(filename)


String processing version of this array wtf...

Re: The Long Road to Clean Up

2007-01-11 10:44 • by Inkstain (unregistered)
110989 in reply to 110986
Anonymous:
derobins:
Michael:
I've never used .Net, so can someone tell me what the @ in front of the string means?


It means you don't interpret \ as escaping another character. @"\n" is interpreted as the string containing two characters '\' and 'n' and not a string containing a single newline.


Why does he use @"\\" then, shouldn't that be either @"\" or "\\" to separate paths? Could be just a typo in the article though?


Because the "\" is an escape character in regular expressions, he was escaping the slash.

Re: The Long Road to Clean Up

2007-01-11 11:05 • by Old Geezer (unregistered)
This isn't a WTF.

In terms of Big-O, his solution is probably equivalent to the array construction routine.

Modern API sets are big, so it may have been faster for him to regex and iterate in thirty seconds rather than research for ten minutes for an equivalent call, especially if it's in another package.

Furthermore, you can readily tell what he's doing in the code. No obfuscation by stupidity.

Re: The Long Road to Clean Up

2007-01-11 11:14 • by Thuktun
It could be worse, they could have offshore developers actively creating and indexing into arrays for no reason.
static final char charArray[] = new char[94];

static {
for (int i = 0; i < 94; i++) {
charArray[i] = (char) (0x21 + i);
}
}

// ...

return charArray[random.nextInt(94)];
versus the shorter but somehow inferior
return (char) ( random.nextInt( 94 ) + 0x21 );
I wish I was kidding.

Re: The Long Road to Clean Up

2007-01-11 11:14 • by snqow (unregistered)
While I understand the function of the @ for string literals, I find this syntax a bit of a clutter... Why not stick to single and double apostrophe convention used in many languages (php, ruby, and others)?

captcha says it's bedtime. But I'm wide awake!

Re: The Long Road to Clean Up

2007-01-11 11:18 • by SpiritOfGrandeur (unregistered)
110999 in reply to 110996
snqow:
While I understand the function of the @ for string literals, I find this syntax a bit of a clutter... Why not stick to single and double apostrophe convention used in many languages (php, ruby, and others)?

captcha says it's bedtime. But I'm wide awake!


Single quotes are for character literals, while double quote are for strings.

CAPTCHA: initech (huh?)

Re: The Long Road to Clean Up

2007-01-11 11:30 • by lynn (unregistered)
Most people probably don't know about the 'Path' class. I found it in a blog 1.5 years ago and started to use it. I makes life so much easier. I have been using .Net since late 2001.

I think in all fairness most people learning .Net have so much to tackle (ASP.net/WebForms/ASMX/ADO.net and now WPF and WCF) that a gem like the Path class is just not on the radar.

Re: The Long Road to Clean Up

2007-01-11 11:32 • by wgh
111002 in reply to 110963
dave_v:
joe.edawrds@imaginuity.com:
string strPoster = "a/b/c/d/e/f/g/h/fist!";
string retval = "";
foreach( string s in strPoster.Split( "/" ) ) retval = s;
Console.WriteLine( retval );
// CAPTCHA: craptastic


You need to write shorter code to achieve that goal.


LOL

Re: The Long Road to Clean Up

2007-01-11 11:45 • by pepethecow (unregistered)
Relying on just the \ is a bug, depending on the source of the path string. Both / and \ are valid path delimiters in Windows. (Maybe not in Win9x) I believe all Windows functions will report paths using the \, however.

Re: The Long Road to Clean Up

2007-01-11 12:04 • by AdT (unregistered)
111006 in reply to 110996
snqow:
While I understand the function of the @ for string literals, I find this syntax a bit of a clutter... Why not stick to single and double apostrophe convention used in many languages (php, ruby, and others)?


Explanation #1: In Cish languages, that syntax is already used for character constants, and might thus lead to confusion.
Explanation #2: That syntax wasn't invented in Redmond and is actively used by child-eating wild-eyed open source communists.

Captcha: billgates (I swear!)

Re: The Long Road to Clean Up

2007-01-11 12:13 • by orangeyoda (unregistered)

The way he's doing string splits is a bit odd.

ignoring the System.IO.Path.GetFileName()

string[] parts = fullPath.Split('\');

CAPTCHA : pizza ... yum

Re: The Long Road to Clean Up

2007-01-11 12:19 • by Prad (unregistered)
There is nothing wrong in not knowing some nitty little function a FCL might have (because it is mostly language dependent) ... and I dont beleive in smiling at somebody's lack of language specific skills (unless ofcourse he's rated himself 10/10 on that particular language)

The "WTF" factor really was this --
foreach (string part in parts)
result=part.ToString();

Re: The Long Road to Clean Up

2007-01-11 12:24 • by wizzard
Wow, this is familiar. I think I can do you one better though.

//find which site we're using: 

$site_path_tmp = getenv("DOCUMENT_ROOT");
$site_path_arr = preg_split("/\//", $site_path_tmp);

$site_path = "";
foreach($site_path_arr as $var)
{
if(preg_match("/site/i", $var) || preg_match("/html/i", $var))
continue;

$site_path .= $var . "/";
}

$site_arr = preg_split("/\//", $site_path);
$site = $site_arr[count($site_arr) - 2];


1. Note that none of these uses of the regular expression engine are even necessary.
2. In fact, you don't even need to use strpos for that inner if condition. You can just compare directly with == .
3. Using "continue" is also totally unnecessary.
4. Just so the readers knows, "count($site_arr) - 2" is always, always, always 2.
5. In fact, the base path is always /var/www.

Enjoy.

Re: The Long Road to Clean Up

2007-01-11 12:31 • by TickleMeElmo (unregistered)
My favourite part is the final string.ToString() call. I can picture the developer pausing to reflect at the end of his job on whether there was any way to make it just a *little* worse.

Re: The Long Road to Clean Up

2007-01-11 12:38 • by Neko (unregistered)
Nice to see he's making .NET even more cross-platform than it was before.

Re: The Long Road to Clean Up

2007-01-11 12:40 • by Neomojo (unregistered)
111023 in reply to 111000
lynn:
Most people probably don't know about the 'Path' class. I found it in a blog 1.5 years ago and started to use it. I makes life so much easier. I have been using .Net since late 2001.

I think in all fairness most people learning .Net have so much to tackle (ASP.net/WebForms/ASMX/ADO.net and now WPF and WCF) that a gem like the Path class is just not on the radar.


Maybe it's just me (A few things are. Peanut butter, ham and ketchup sandwiches, for example.) but when I'm trying to do something in .Net (or PHP), my first thought is "has it already been done?"

I agree that there are a lot of namespaces, objects and functions to browse through, but generally (as with the Path class) they're in intuitive places. If only it were true always, and not just generally.

Re: The Long Road to Clean Up

2007-01-11 13:00 • by spamparranoid (unregistered)
111032 in reply to 110962
Has anybody ever found their own code on here before? Has anybody admitted to it?

Re: The Long Road to Clean Up

2007-01-11 13:41 • by Chris (unregistered)
111050 in reply to 110992
Old Geezer:
This isn't a WTF.

In terms of Big-O, his solution is probably equivalent to the array construction routine.



LOOOOOOOOOOOOOOOOL ! Are you kidding or trolling ? Seems not, so i can not help but quick reply :o)

Even though you he does not know the existence of the getFileName method, any young developer would know that it exists somewhere. You may spend 10 minutes finding it, but then you do not spend 100 times 1 minute to write this code plus 50 times 15 minutes to debug it !

Even more, instead of looking for it, this guy creates a regex. Regex is not such a lightweight object, i don't know in .NET but in java the string would go throug a regex compiler, and anyway a regex interpreter is overkill if what you want is just find the last \.

So, even though you want to develop it by yourself, you can simply reverse scan the string looking for \. Once you find it (or not then you stop at index 0), then you can extrapolate the filename.

This WTF is the result of a massively tired and/or stupid developper. The former can easily be cured by some hours of sleep, i suspect curing the later can take substantially more effort during the course of years, for no guaranteed result...

Your exercise for WTFing over a WTF (WTF^2 seems to be your IT level): you write the corresponding code 10 times ;o)

Chris

PS : Alex, i think you should open a new website namely www.thedailywtfwtf.com in order to take advantage of the numerous WTFs in the forums. Or at least have a special dedicated page, we could even easily vote for comment "promotion", just add a "WTF" button next to the "comment" and "quote" ones, i would loooooooooooooove it ! :o)

Re: The Long Road to Clean Up

2007-01-11 14:10 • by Rick
If you must use a Regex, why not @".*\\" ?

I don't know .NET but if they are like C,Perl,Java,vi,sed regular expressions than match everything up to and including the last backslash.

Re: The Long Road to Clean Up

2007-01-11 15:02 • by Franz Kafka (unregistered)
111070 in reply to 111000
lynn:
Most people probably don't know about the 'Path' class. I found it in a blog 1.5 years ago and started to use it. I makes life so much easier. I have been using .Net since late 2001.

I think in all fairness most people learning .Net have so much to tackle (ASP.net/WebForms/ASMX/ADO.net and now WPF and WCF) that a gem like the Path class is just not on the radar.


The first time I had to do Path processing, I went into the Java libs and dug around and found some stuff that helped, mainly because it's a royal pain. I don't recall whether I wrapped that stuff in a class or not, but why would anyone just go reimplement that?

Re: The Long Road to Clean Up

2007-01-11 15:10 • by Mike (unregistered)
111072 in reply to 111050
Chris:


So, even though you want to develop it by yourself, you can simply reverse scan the string looking for \. Once you find it (or not then you stop at index 0), then you can extrapolate the filename.


Or, just use fullPath.LastIndexOf("\") to return, conveniently enough, the last index of the specified character.

Re: The Long Road to Clean Up

2007-01-11 15:23 • by Chris (unregistered)
111078 in reply to 111072
Mike:
Chris:


So, even though you want to develop it by yourself, you can simply reverse scan the string looking for \. Once you find it (or not then you stop at index 0), then you can extrapolate the filename.


Or, just use fullPath.LastIndexOf("\") to return, conveniently enough, the last index of the specified character.


But the goal is to develop it by yourself, and not to use these library methods that you need to search for 5 minutes in the documentation ;o)

Re: The Long Road to Clean Up

2007-01-11 15:54 • by Lonely Programmer (unregistered)
111085 in reply to 111078
Isn't Intellisense Wonderful? Not only can you see what methods and properties are available, you can get it to type it for you.

And yes, I realize that there are people masochistic enough to write C# code in Emacs, but still!

Ooooh - poprocks? Where's my Coke!?!?

Re: The Long Road to Clean Up

2007-01-11 16:06 • by savar
111091 in reply to 110987
Jon Skeet:
Nope, he wanted two backslashes - this is a regex, don't forget.


Somebody a few posts up claimed that the @ in front of a string means "dont escape any characters in this string" -- completely wrong of course, but it spurred somebody else to ask why the string was @"\\" if @ turns off escaping.

My favorite WTFs are the ones written in these comments.

Re: The Long Road to Clean Up

2007-01-11 17:26 • by Crotchety Old Guy (on a SunOS 4.1.3 box) (unregistered)
You spoiled kids these days with your fancy schmancy library calls.

In my day, we walked 10 miles through the snow and then wrote something like this:

char *ptr = fullPathName;
while( *(ptr++) != 0);
while(*(ptr-1) != '/' && --ptr != fullpath );
result = new String( ptr) ;

Re: The Long Road to Clean Up

2007-01-11 18:01 • by another Steve (unregistered)
111111 in reply to 110992
Agreed - this is one of the least exciting WTFs I've seen. The code isn't laughable or dangerous - just inefficient and shows he doesn't know the libraries well. Slow news day?

Re: The Long Road to Clean Up

2007-01-11 18:20 • by brendan
111114 in reply to 111101
Crotchety Old Guy (on a SunOS 4.1.3 box):
You spoiled kids these days with your fancy schmancy library calls.

In my day, we walked 10 miles through the snow and then wrote something like this:


I would hate to think how many miles you would walk for a complex solution.

Crotchety Old Guy (on a SunOS 4.1.3 box):


char *ptr = fullPathName;
while( *(ptr++) != 0);
while(*(ptr-1) != '/' && --ptr != fullpath );
result = new String( ptr) ;


first

while( *(ptr++) != 0);
while(*(ptr-1) != '/' && --ptr != fullpath );

should be

char *last = NULL;
for(;*ptr;i++)
if (*ptr == '/')
last = ptr;
ptr = last;

Second the C library already gives you a the strrchr function for doing this:

char *ptr = strrchr(fillPathName, '/');
if (ptr)
result = new String(ptr);

@orginal post: striker has the best sollution to the wtf.

Re: The Long Road to Clean Up

2007-01-11 18:28 • by Cowboy Curtis (unregistered)
111115 in reply to 111091
savar:
Jon Skeet:
Nope, he wanted two backslashes - this is a regex, don't forget.


Somebody a few posts up claimed that the @ in front of a string means "dont escape any characters in this string" -- completely wrong of course, but it spurred somebody else to ask why the string was @"\\" if @ turns off escaping.

My favorite WTFs are the ones written in these comments.

Which is exactly the question he answered. @ turns off escaping in a string literal, and the extra slash was to get past the escaping of the regex engine.

Re: The Long Road to Clean Up

2007-01-11 18:29 • by Artemus Harper (unregistered)
111116 in reply to 111019
Although it makes no sense in this case, I've seen it used as a lazy man's way of asserting that the string is not null.

captcha: billgates (my "boss")

Re: The Long Road to Clean Up

2007-01-11 18:44 • by Javier Martín Domínguez (unregistered)
111119 in reply to 110962
joe.edawrds@imaginuity.com:
string strPoster = "a/b/c/d/e/f/g/h/fist!";
string retval = "";
foreach( string s in strPoster.Split( "/" ) ) retval = s;
Console.WriteLine( retval );


Now, that's good humour!
« PrevPage 1 | Page 2Next »

Add Comment