• ray10k (unregistered)

    Did this potatohead of a developer think that the whole npm leftpad debacle also extended to C#/java? Or is it just plain ignorance?

    Eh, probably ignorance.

  • Ulysses (unregistered)

    If you said, "It's a pad left function," then you win…

    Mangled chars in a string article. Did Remy decide to poke fun or did the CMS hiccup?

  • PWolff (nodebb)

    Ah, teh good ol' "Never Trust a Library" school.

    In VB6/VBA6 I've seen several functions called StrToLen() and similar, but that would be too sane to be fun, especially when they make use of the built-in String(Integer, String) (which uses only the first character of its String argument - single characters don't exist as a type in VB6/VBA6)

  • Remy Porter (google) in reply to Ulysses

    I switched markdown processors, and the one in Visual Studio code chokes on "…" apparently.

  • Thurfir (unregistered)

    The real WTF is the lack of a StringBuilder.

  • Ron (unregistered)

    If I guessed it's a pad function doesn't say anything about the way my mind works, it's more a comment on how many of this type of thing I've had to debug..........

  • Vilx- (unregistered)

    I couldn't have guessed it, but in retrospect perhaps the reason for the name is this - the function ensures that the string is at least of a specific LENGTH? Also, the code does seem to work fine with multi-character paddings, although it is indeed inefficient.

  • F (unregistered)

    What if "repl" is an empty string?

  • Brian Boorman (google) in reply to Vilx-
    Also, the code does seem to work fine with multi-character paddings

    If by work fine you mean that you could end up with a string that is longer than the length parameter. If you want length and only length for the size of the output string, then it's broken.

  • Melnorme (unregistered)

    "CUI.Calendar.GetWeekOfYear" takes arguments that are also located on CUI. This could have been a method on CultureInfo.

  • Pista (unregistered) in reply to F

    Then it will take a long time for this function to pad the input string :D

  • mrth (unregistered)

    I was afraid this day might come… This code seems awfully familiar. Can’t be 100% sure but still..

    Seems like one of my first encounters with C# abt. 8 years ago, I was a lone developer and no senior to guide me. If I recall correctly, then this is some sort of reporting process that creates some files. It seems CW stands for current week and DateUtil.cs was not included there at all, it even might be copy/paste from stack overflow. StrLength.. If it was me then I have no idea why I named it like that and most likely I didn’t know that there was such thing as String.PadLeft and it was used to force some order numbers into predefined format, something like 234->0000345. I still see nightmares about some of the XML parsing solutions I managed to vomit to one of my EDI integration utility. Unfortunately, I don’t work there anymore, but if this is really from my code, it is just a matter of time before Hannes uploads some more of my ingenuity.

  • Alex (unregistered) in reply to mrth

    Hooray! Maybe this is the first time TDWTF can tar and feather the guilty! Do you volunteer?

  • chreng (unregistered) in reply to Pista

    Only if len is a positive number

  • mrth (unregistered) in reply to mrth

    I asked around a bit.

    1. It was 6 years ago, which is logical bc we used VS 2010

    2. I did have the exact CW construction but without the comment

    3. For the prefix I had this masterpiece instead:

       private string AddPrefix(string s, int len, char pref)
       {
           return String.Format("{0}{1}", new String(pref, len - s.Length), s);
       }
      

    So i either the CW for current week is direct copy paste from some public forum or StrLength is yet to be found.

  • Peter (unregistered)

    Obviously the programmer was thinking of "strengh" als the function name: it exists to strengthen poor famined strings that have too few characters in their stomach. A food staple of several padding characters instead of only one is all for the best in this case, because variety and balanced diet. Granted, this might make the string too long, in which case a "slimming" function would be in order …

    What do you say, they put "strLength", not "strength"? Well, in their zeal and haste to get food to the famished string as soon as possible, they made a typo. :-P

  • Derpface (unregistered)

    As a HPC, I get to dig into plenty of lovely bile inspiring legacy code. Currently, I am elbows deep in a tiny data pump written in java that takes request, fetches data from a database and sends it to a service supplier for validation. At least, that's what it should do, and do in about 4-5 classes worth of code. There are thousands upon thousands of lines of code in that horrible mess, and most of it is severe NIH. Like the homebrew data pool, database driver, database connection and everything else database, that I was tasked with fixing a minor bug in that if the database was unavailable, for whatever reason, it could not get connections to it. I spent about two weeks digging through the code, getting to understand what it does and how. And then I ripped out 6 of the classes regarding databases and replaced them with the DataSource system from java. 6 classes of about a couple of thousand lines of broken and horrible code (seriously, even the indentation is fucked up) replaced with 10 lines of code that does a better job. If I am allowed to keep wailing at the code with my mighty Axe of Source Codery I'll soon have the bit's of homebrew database cache (that saves files do disk, and gets out of sync surprisingly often) on the chopping block. Together with the god awful mess of a home made TCP-protocol. Not to mention the various ugly cancerous growths "utilities" supposedly to interact with the data in various ways...

  • Hannes (unregistered) in reply to mrth

    If, by any chance, you happened to work in Hamburg, Germany 6 years ago then yes, this is indeed YOUR code. If not, then you're off the hook. :P

    The project I took those code examples from is full of similar "reinventing the wheel" methods, like an extra method to find out the last position of a character in a given string (instead of using the built-in "LastIndexOf"). Directory names are not found via "Path.GetDirectoryName" but with another method, which chops away every character until it found the last backslash.

    And sometimes, methods are in classes where they simply don't belong if you'd follow any logic at all. The "Email.cs" contains a method called "WriteReportToFile", which at least does exactly this.

    I think the developer read the guidelines of "How to write unmaintainable code" and thought the author was serious.

  • Mrth (unregistered) in reply to Hannes

    Phew, then it wasn't me!

  • Scarlet_Manuka (nodebb)

    I mean, what does CW even mean anyway? (in comments) Current Week, I know, but seriously, stop playing keyboard golf.

    Based on the code snippet, I think it's more likely to be Calendar Week. The fact that we can't be certain only reinforces your point, of course.

  • owlstead (unregistered)

    I loathe it when people assign values to variables (null, empty or whatnot) just because they declare them. This is "how to spot a junior coder" in one single step.

    final int readBytes; try (final InputStream is = new FileInputStream(file)) { readBytes = fis.read(buffer); } catch (final IOException e) { throw new IllegalStateException("Could not read file", e); } System.out.println(readBytes);

    works just fine. It's a junior mistake to have to assign 0 to "readBytes" or to use a separate variable for "is" (not knowing Java's try-with-resources).

Leave a comment on “Recycled Code”

Log In or post as a guest

Replying to comment #:

« Return to Article