- 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
Ah!, the wonderful benefits of knowing the system you're running on! :-)
Admin
Haha, that's hilarious. Doesn't even check the os to see if it's unix first.
Admin
Whew it's a good thing that every operating system has a command-line interface that supports the internationally standardized "mkdir" command.
I hear that there are talks to standardize the command frequently used in movies known as "PASSWORD OVERRIDE". I use it all the time.
Admin
of course!! its the way i ensure my code runs on the intended operating system i design it for. who wants portability when theres functionality that works?
Admin
Well, if the original author of this code bothered to read the Java API, they'd realize how stupid this code really is.
Admin
Perfect example of the ways that a programmer can totally break the framework and intentions of something like Java. You would think that it would have been easier to use Java's IO classes to achieve this, but I guess not.
Admin
might as well have writen a short shell script.
Admin
Inability to write portable code seems quite common among unixoids. Typical WTFs include:
Admin
If he had made it completely portable then there would be one less job created to make this usable on other systems. He was just protecting the interests of all the coders that do upgrades for a living.
Admin
This kind of stuff happens a lot in PERL too.
but sometimes its just so easy to
cat tmpfile| sort
Admin
If that's the worst violation he has to deal with, this "port" will be trivial, and will run better on the original platform to boot!
Admin
Wow. The portability of Java, minus the stupidity of the programmer, equals useless code.
Then again, how can we expect him to know that Windows and Unix require different commands? Honest mistake, right?
[:P]
Admin
Well, that is the main WTF right there. Why would you port something from Unix to Windows? Unix to Linux makes sense. If the server is connected to a public network, Unix to OpenBSD makes even more sense. But why Unix to Windows?
At least just install cygwin and call it a day.
Admin
Just because Java is extremely portable, doesn't mean the software being developed is going to be used on every platform.
Sure, it's a best practice to use more abstract functions, but this WILL work as long as the 'mkdir' command exists.
This is a very minor WTF.
Admin
I'd say it's a minor wtf, but not because the person "knew" ahead of time that it was only going to be used on one platform. I mean, creating the temp directory is kind of stupid, but I can see a valid reason that you'd want to do it.
Maybe he was just concerned that it would take too long for java create a directory, thanks to the performance boogeyman, or it would take him too long to look at the docs and figure out how to do that. I'd consider this a hack, and not a particularly ugly one (homely, perhaps). This reeks of the bottom of the barrel of wtfs; is the end near?
Admin
That's the quick-and-dirty solution for people who don't like to RTFM. I admit that todays WTF could almost be mine. (Of course it isn't, I would not have a "Constants" class, but rather write
Runtime.getRuntime().exec("mkdir -p /var/temp/foo/bar/uploads") or omit it at all, since the directory must exist.
Sidenote: mkdir -p creates all the necessary directories on the way.
Anyway, I've just checked the docs and IMO it's pretty hard to find the right way, because of its name. (For those who wonder, it's File.mkdirs(). I've looked for something in the line of createDirecory() first. No wonder people are tempted to go the easy WTF way.
Admin
I just reread my post. Need more coffee for lucidity :(
Admin
I would say that his way was highly optimised, BUT he uses a constant to hold the UPLOAD_PATH instead of putting it in XML!
OTOH, look how convienient it is to get the static member out Constants "class". OOP at its best. Anyways, for those who don't know, the "correct" way to do this is:
Admin
Presumably, the original author wrote more than just these lines in Java. So it's not like they were unfamiliar with the language and how to use the javadoc to find something you're looking for.. I mean, they had to lookup Runtime.getRuntime().exec(), File.mkdirs() doesn't seem much harder...
Admin
Maybe the original developer was told the app would only be run on Unix.
Admin
Even though I spend much of my time developing on unix servers, I can think of plenty of scenerioes in which it is reasonable to port to Windows:
Third-party software that relies on COM for interop.
Hardware drivers that are only available under Windows.
Customer requests version to work on their servers.
Support team prefers Windows.
And, of course, the most likely reason: the boss said so, because Windows' TCO is x% lower, and...
Since we're on the subject, I'll tell a similar, awful story on myself. Back when I was a wee developer just leaving windows for the first time, I was writing some program or another and needed the current system's ip address.
Difficulty: I didn't know about the gethostname, gethostbyname, etc, functions. Nor did I even know about using ioctl to grab the data. I came up with something like
ifconfig |grep -A2 eth0 |grep inet|cut -d":" -f2 |cut -d" " -f1
Erm. Yeah. I know better now.
Admin
I've done something like this before.....
I needed to do a sort unique on a hundred odd meg file.
Admin
But this is portable code. Just ship it with the UNIX source included. Isn't that why they wrote it in C?
--Rank
Admin
The problem is that interacting with the file system is such a basic and intergral part of any programming language. If he had to RTFM to know that the java.io.File class could be used to create a directory then he should not have been using Java to code this. Quick, think of your "favorite" language that you consider yourself skilled in. Can't you just say off the top of your head how to create a directory or file?
Admin
Another milestone in the gowth from tadpole to frog is the first time you work on a system with more than one network adapter (excluding the loopback device). Two IP addresses? Two "host" names? Boggle!
Admin
Get out of academic life and come to real life
Admin
Isn't that what Perl hashes are for?
I've actually done this for some "quick & dirty" tasks, and it worked fine.
Admin
Ha! Me? Academic? You must be kidding!
Admin
But Runtime.getRuntime().exec() is easier to find (really!) and it's also likely that it has been abused before for similar operations, like moving files, for the folks who haven't found File.renameTo().
Admin
Create a file: yes. Create a directory: no.
Admin
Plus, the same technique can be used for things outside the common operations like creating a symlink in windows.
Admin
Portability aside, you should always avoid shelling whenever possible because the error-handling is so poor. I consider this a major WTF. If you are going to do this, why not just call a shell script to do everything? Do you open files with shell scripts?
Admin
Nice...There's no sort function available in Java either.
Admin
<font size="2">I am sure Nick Smith has his own reasons to hate Jave if his piece of art does not work</font> :'(
Admin
<font size="1" style="font-family: verdana;">
<font size="2">Agreed. Gotta love the fact that the "portable" solution involves locating a method which is named after a particular OS's command for the operation. JavaDoc is only helpful if you already have a good idea of what you're looking for.
</font></font>
Admin
Maybe if you don't know alphabetical order and worked in shell scripts for 10 years before you began using Java?
Admin
java.util.SortedSet no good for you?
Admin
In Ruby, it's not too much more difficult than the shell version:
File.readlines('tmpfile').sort
Probably the same exists in Perl.
Admin
Frequently? Can you mention more than one movie where you can see that command being written (or shown or whatever)?
Admin
Well... SortedSet is an interface, so he'd probably want to use TreeSet, unless he wanted to write his own data structure that implemented SortedSet.
There's also Collections#sort and Arrays#sort functions that would be useful.
But I'd almost be willing to bet that, portability aside, spawning off the Unix "sort" command to sort a large file would be a great deal faster then doing it in Java.
Admin
Well, at least this one is easily fixed. What if he had redirected the output of ls to a text file and parsed the file to retrieve the contents of a folder? I don't think this programmer is being the Prince of Darkness, so a mild "What The Heck" should suffice.
Admin
How much?
I need some money - Christmas is killing me.
Admin
Oh, another problem with SortedSet (and the classes that implement it) is that it might be perfectly OK for his file to have multiple identical lines, which a Set-implementing class would clobber.
Admin
From the post to which I replied...
How often does one do a 'sort unique' where multiple identical lines are perfectly ok?
Admin
As often as one uses an interface to sort a file. ;)
<font size="1">OK, didn't see that line.</font>
Admin
I think it would depend on the size of the file and exactly how you implemented it in Java. If you went with a quick & dirty implementation using java.io.* and TreeSet, and you were only sorting and uniqueing the file (and not loading into some data structure in the JVM) I stand by my guess that Unix sort would likely be faster. Shouldn't be hard to test...
Admin
You would think the person who shelled out to execute the mkdir command would be able to find File.mkdir. But I think another WTF here is W(hy)TF would Sun create a method names mkdir (or mkdirs)??? Wouldn't following their own conventions require the method name to be: makeDirectory() or makeDirectories() ? or something similar. I don't think you should have to read past the method name in the docs to get a high level understanding of what the method did. (Assuming you are coming from a non-unix world and are not familiar with mkdir)
Admin
Ha-Ha!
Actually, a class implementing SortedSet could work fine for identical records. A primary key could be added to each record in order to provide uniqueness, but the Comparator is free to only use that key to perform arbitrary distinction amongst equivalent records when doing the sorting.
Admin
Since the DOS/Windows command is also mkdir, I don't see much of an issue. It does put out the VMS user who want File.CREATE /DIR(), but they're already pissed that there is no File.PURGE() method.
Admin
This is a great trick for moderately sized files. Memory constraints become a problem, of course, since the hash is completely in memory. If you run into this problem, try it with a hash TIE'd to gdmb or some other file-based medium. It's slower, but at least your program won't die. SQLite is also a handy option.
I'm sure you're aware, but I mention this for the sake of less seasoned perl coders on the list.