• Barry Etter (unregistered)

    Thank goodness he's only working with 3-character file extentions!

  • Matthew W. Jackson (unregistered)

    So he knows of the substring function, but decides to only use it for a character at a time.

    Once he learns of the toUpperCase function, I'm sure he'll apply it one character at a time as well.

  • Matthew W. Jackson (unregistered)

    Barry: Maybe for strings longer than 3 characters he has a code-generator that builds a function in a string and then calls eval().

  • TheF0o1 (unregistered)

    Unbelievable! The "programmer" actually used the substring method in a for loop that emulates the substring method. If you don't trust the string manipulation functions, then why use them at all, especially the ones you don't trust?

    I know it's not really a matter of trust. The original post makes a good point about being too lazy to look up the appropriate functions. Ironically, it's actually more work to reinvent the wheel.

  • Jesus (unregistered)

    At some point in doing this you would have to say to yourself...man there has to be an easier way let me google. If this point never comes then you are destined for management.

  • Eric Wise (unregistered)

    I'd just like to point out that this guy wasn't a VB coder. No snide anti-vb coder comments on this thread!

  • [email protected] (Hassan Voyeau) (unregistered)

    This function should be called isCSVorXLS or he/she should have created a generic function that has the extension as a second parameter. Poor dude/dudette!

  • · (unregistered)

    At least the snippet illustrates good naming conventions. "test1" is so much clearer (and more concise) than "fileExtension," and the name of the argument ("s") makes it clear what should be passed in to the function.

  • Tim Cartwright (unregistered)

    Heres how I would write it :

    function isExcel(sFileName){
    var sFileExt = sFileName.toUpperCase().substring(sFileName.lastIndexOf('.'));
    return sFileExt == '.XLS' || sFileExt == '.CSV';
    }

    But thats just me..... call me crazy...

  • Matthew W. Jackson (unregistered)

    Crazy. :-)

  • Craig (unregistered)

    I'm going thru the past few days of Daily WTFs that I've missed. It is truly insane. If other industries were as bad as software development, America would become a third world nation over night. Imagine CPAs, doctors, architects, mathematicians, physicists and scores of other engineering disciplines doing their jobs as hit & miss and haphazard as we do. Doctor’s would use penicillin to cure sprains. Architects would draw with crayons. Mathematicians would each invent their own order of operations.

    I think part of it is that anyone can become a software developer. It doesn’t require a degree, certification or a test. Of course, these things help, but are no means required to work for a company or even the government. This is coming from a guy who has been developing software for 6 years and still doesn’t have his degree. We need something in place to weed out the people that will just do whatever works rather than what is really needed, and to give a solid foundation for those who really want to learn.

  • Matthew W. Jackson (unregistered)

    "Imagine ... doctors ... doing their jobs as hit & miss and haphazard as we do."

    Dear God. That reminds me...

    "Oh yeah, people often overlook the alternative medical care. Like that blue guy that was shot in the foot during the battle? All I did was rub his neck with some aloe vera. He was fine!"

    The code posted on this website is just like that.

  • Mr.Prakash (unregistered)

    kill this guy, but then wait there is a law not to kill people.

  • Sven (unregistered)

    I my god, the invocation of the substring WTF!

  • Uwe (unregistered)

    People that use braces for the return statement are really really strange...

  • Matthew W. Jackson (unregistered)

    Uwe:

    I use them in some situations, such as when an expression is involved.

    return (x == 3);

    For whatever reason, whenever I write it this way:

    return x == 3;

    My mind reads it so fast that I see "return x" and then I wonder how in the world you can compare that to 3. I really don't know why. It makes absolutely no sense why I feel I should do this. Please help me.

    But I never use them when directly returning something.

    return false;

    That's always good enough.

  • CS' (unregistered)

    at least he knew that there is only 2^3 possible combination to check.

  • Tim Smith (unregistered)

    The lastIndexOf isn't needed.

    function isExcel(sFileName){
    var sFileExt = sFileName.toUpperCase().substring(sFileName.length-4);
    return sFileExt == '.XLS' || sFileExt == '.CSV';
    }

    But I still don't like that we are depending on substring clamping negative indices to zero. Even though this is documented to be so, it still makes me want to take a shower after writing that. :)

  • Eric Newton (unregistered)

    I gotta stop reading this blog. lol

  • Rich Gibson (unregistered)

    Craig,

    Software development as a discipline is going great! And this is a perfect example. Being charitible we have a new lamb in the fold who has been able to externalize his vision of the world in code.

    Now, this code is really ugly, but it works well enough. And presumably this novice has had the opportunity to advance in his or her undertanding of the process of development.

    This 19 or so line function could be replaced with 2-3 lines (well, or one tight line). But really, so what? In what possible way would the world be better if this code were done 'right?'

    The page would load slighly faster, since it would be smaller, but it's presumably an upload script, so it is going to be relatively slow anyway.

    I mean...this is really stupid code, but it isn't on par with a Doctor not diagnosng a disease or anything.

    Software advances as quickly as it does precisly because the iteration cost is usually so low that it encourages us to try new things.

    Of course...this person is stupid, but it is possible for stupid people to learn.

  • josh (unregistered)

    Programmers need to be precise, so I don't feel so bad about being pedantic here. Those things around the value to be returned? They're not braces. They're parentheses.

  • [email protected] (Don Newman) (unregistered)

    I am just new to following this blog, but I have learned a few things from it so far. First is I really deserve a raise and that I would never have a coder under me who didn't receive periodic code reviews. That aside, stuff like this makes me wonder how such a lack of either common sense or curiousity (to google) managed to learn as little as they know about code. My only way of sleeping at night now is to rationalize that people like this have a manager who lives by "get the thing working, now!" and believes lines of code = productivity.

  • Uwe (unregistered)

    Josh, you're my man :-)

    Since I'm native german speaking, I forgot about the correct word for those things: "(" and ")".

    So this is correct?:

    () - parenthesis
    [] - brackets
    {} - braces

    Correct?

  • asdfs (unregistered)

    Rich Gibson:
    You said
    ....This 19 or so line function could be replaced with 2-3 lines (well, or one tight line). But really, so what? In what possible way would the world be better if this code were done 'right?' ....

    And what about the number of comparison operations? Here you've got 2^|extension| comparisons, however, if the code was done "right" it'd have 1 comparison operation and 1 strtolower() or whatever operation.
    This code just makes me wonder if "modern programmers" ever think of efficient alogrithms (?).

  • josh (unregistered)

    Uwe: Ahh, I see. Yeah, that's right. Usually I refer to [] as "square brackets" and {} as "curly braces" for added clarity. (I guess it helps remembering too) And <> (greater than/less than if this place strips them) when misused properly become "angle brackets."

  • Duff (unregistered)

    Amazing... it pisses me off when smart friends of mine are unemployed and dolts like whomever wrote this drivel have jobs.

  • Simon (unregistered)

    Oh my god

  • Ray S (unregistered)

    I'd love to see this guy's code for checking if a certain person placed the order.

    if (test2=="AlbERt EInstEIn"||test2...
    ...

    ...


    Ye Gods!

  • Mike R. (unregistered)

    > Amazing... it pisses me off when smart friends of mine are unemployed and dolts like whomever wrote this drivel have jobs.


    Heh... try being the one that has been out of a job for 18 months, and started working again a year ago. It makes me sick.

  • Tim Cartwright (unregistered)

    "The lastIndexOf isn't needed.

    function isExcel(sFileName){
    var sFileExt = sFileName.toUpperCase().substring(sFileName.length-4);
    return sFileExt == '.XLS' || sFileExt == '.CSV';
    }"

    Tim, I was writing the code so it would work for other longer extensions, can you imagine what the guy above would do to that code, if he had to mod if for say : ".config"?

  • Arf arf arf (unregistered)

    function isExcel( file )
    return -1 != (''+file).search(/.(csv|xls)$/i);
    }

    Hot News: Pattern-matching in strings is a well-understood problem, and it's been solved for decades.

    Modern interpreted languages like JS support regular expressions. Regexes are almost certainly faster, always more concise, and generally more readable than this BASIC-style substring stuff.

    They're also enormously harder to screw up: You're expressing what you mean directly, in one bite, instead of sneaking up on it sideways. You want to check for ".config" and ".pl" too? Okay: /.(xls|csv|config|pl)$/i.

    Of course, 90% of the JS "experts" in the world know exactly two functions: substring() and eval().

    Anyhow, if we must write 1960s BASIC code in a 1990s language, we can (just for form's sake) at least call toUpperCase() only on the substring we're actually looking at, not on the rest. And what does that code do if lastIndexOf() returns -1?

    Worse is the assumption that sFileName is a string. Users are going to see a little alert box reading "BLAMMO! Object don't play that," or "you can't call member functions on null, Beavis", or something along those lines.

  • Ryan Heath (unregistered)

    >Worse is the assumption that sFileName is a
    >string. Users are going to see a little alert
    >box reading "BLAMMO! Object don't play that,"
    >or "you can't call member functions on null,
    >Beavis", or something along those lines.

    You had to do some more testing :)
    Now with that ( '' + file) you are silently
    eating a bug in your script...

  • Aarrgghh (unregistered)

    Ryan: Fair enough :)

  • Eric Hodel (unregistered)

    You guys all do way too much work.

    function isCSV(file_name) {
    return (file_name.match(/.(?:csv|xls)$/i) != null)
    }

    #match returns the matched portion of the String or null if there was no match.

  • Justin Boswell (unregistered)

    Argh, Eric beat me to it. Any way you slice it, that's some terribly troubling code :(

  • Robert Johnston (unregistered)

    Of course, you're all forgetting that RegExp's weren't implemented 'til JS1.2 (Or IE5.5).

    So if this is to support NS4/IE5, you can't use RegExp's...

    Sorry to burst a bubble there.

  • Aarrgghh (unregistered)

    Robert Johnston: MSDN claims RegExp was supported in JScript v3/IE4. I do seem to remember using those in stuff that worked in IE4. Lots of weird stuff (Array.push()?!) was missing for far too long, of course, so who knows. MSDN is hardly infallible.

    http://msdn.microsoft.com/library/en-us/jscript7/html/jsoriversioninformation.asp

  • Wyatt T. Fox (unregistered)

    I can't beleive no one noticed the extremely poor grammar displayed in the title of this thread:

    you mean there's A SUCH thing as...

    instead of

    you mean there's SUCH A thing as...

    how am I supposed to trust the quality of code on this site, when such simple errors are left to slip by? Really...

  • Distilled Software Hate (unregistered)

    "Imagine ... doctors ... doing their jobs as hit & miss and haphazard as we do."

    Google for malpractice sites.

  • Dom (unregistered)

    What, you mean there actually is such a thing a toUpperCase()?

    ;o)

  • Richard Wan (unregistered)

    Ummm... the suggested "improvements" blow up rather than return false if the filename has no extension (using the indexOf improvement) and the filename length is less than four characters (when using length-4)

  • Baktru (unregistered)

    A cart implementation... So this is web developer code.

    Way back when dot.com was bubbling, in my first job, the company I worked for landed a joint deal (i.e. the workload was shared between two companies, our small small small consultancy firm and a then behemoth that went bankrupt two years later in a big scandal) to train new Web Developers. The target audience were chronically unemployed who were pretty much 'forced' to follow a course, or they would lose their unemployment benefits. And the course would take three months. I did three days or so to teach basic SQL.

    A third of them were just there wasting time, there was no point engaging them since they were just sitting in that chair to NOT lose their benefits.

    Another third did actually try for a while, but were just not fit to be programmers. After a while, they just gave up trying to understand and joined group 1.

    The final third did grasp some of it all and of those, about half would probably eventually write code like that. Mind you, we had to keep the exam simple since a too-low pass rate would have indicated 'bad teaching'...

    Now I have to admit, one guy there was actually good, got hired by our firm and did well. But most of them... Blah.

    Way back when, everyone and their dog wanted to get websites and what not and the number of properly trained programmers just didn't exist. I'm not actually surprised to see code like this made it to production status.

  • (cs) in reply to Uwe
    Uwe:
    Josh, you're my man :-)

    Since I'm native german speaking, I forgot about the correct word for those things: "(" and ")".

    So this is correct?:

    () - parenthesis [] - brackets {} - braces

    Correct?

    () are parentheses, otherwise that's correct.

Leave a comment on “You mean there's a such thing as "toUpperCase()"?”

Log In or post as a guest

Replying to comment #23284:

« Return to Article