Modulo? No thanks, I don't need any



  • Not the worst WTF I've seen in my current project, but it is a bit facepalm worthy:

    var recordYear = int.Parse(DateTime.Now.Year.ToString().Substring(2));

    This got replaced with a more sensible

    var recordYear = DateTime.Now.Year % 100;



  • Did the conspiracy to slowly introduce Y2k1 problems already start?



  • Well, when you have to interoperate with legacy mainframe applications, you do what you gotta do. Would I like to replace it with a DateTime.UtcNow and call it a day? Yes. Yes I would.



  • @TGV said:

    Did the conspiracy to slowly introduce Y2k1 problems already start?
    Well, it's not a huge problem, considering Y2k1 was twelve years ago.



  • @Snowyowl said:

    @TGV said:
    Did the conspiracy to slowly introduce Y2k1 problems already start?
    Well, it's not a huge problem, considering Y2k1 was twelve years ago.
    I'm not sending you down the shops to buy a 4k7 resistor for me.

     



  • @DaveK said:

    @Snowyowl said:

    @TGV said:
    Did the conspiracy to slowly introduce Y2k1 problems already start?
    Well, it's not a huge problem, considering Y2k1 was twelve years ago.
    I'm not sending you down the shops to buy a 4k7 resistor for me.

     


    But what if he comes from the future? Maybe for him, it really was 12 years ago!

    Mind blown...


  • @Algorythmics said:

    But what if he comes from the future? Maybe for him, it really was 12 years ago!




    Mind blown...

    Damn those Timepods!!!



  • @lscharen said:

    Not the worst WTF I've seen in my current project, but it is a bit facepalm worthy:

    var recordYear = int.Parse(DateTime.Now.Year.ToString().Substring(2));

    This got replaced with a more sensible

    var recordYear = DateTime.Now.Year % 100;

    Meh, I've been writing code for a decade and I would've probably used the first one. Assuming speed isn't an issue here (and if it is, you've got bigger problems) then it's a case of six of one/half a dozen of the other.



  • @The_Assimilator said:

    Meh, I've been writing code for a decade and I would've probably used the first one. Assuming speed isn't an issue here (and if it is, you've got bigger problems) then it's a case of six of one/half a dozen of the other.
     

    Stop writing code. Programming is not for you.



  • @dhromed said:

    @The_Assimilator said:

    Meh, I've been writing code for a decade and I would've probably used the first one. Assuming speed isn't an issue here (and if it is, you've got bigger problems) then it's a case of six of one/half a dozen of the other.
     

    Stop writing code. Programming is not for you.


    Concur. The point is not the speed of the thing; it's the readability. It takes a long time to figure out what the first one is doing; the second one takes a fraction of a second.

    Even better would have been a method "currentYearAsTwoDigits()" that computes that modulo; then the code would be



    public int currentYearAsTwoDigits() { return DateTime.Now.Year % 100 };

    var recordYear = currentYearAsTwoDigits();

    No thinking required at all.



  • @dhromed said:

    @The_Assimilator said:

    Meh, I've been writing code for a decade and I would've probably used the first one. Assuming speed isn't an issue here (and if it is, you've got bigger problems) then it's a case of six of one/half a dozen of the other.
     

    Stop writing code. Programming is not for you.

    If it was me, I would have just used the date library's "format as two-digit year" option. It's very clear what it does. Using substring on the 4-digit year does seem hacky, but doing modulo also feels wrong.



  • @DrPepper said:

    Concur. The point is not the speed of the thing; it's the readability. It takes a long time to figure out what the first one is doing; the second one takes a fraction of a second.

    The most readable would just be using the date library's "format as 2-digit year" option..



  • @The_Assimilator said:

    @lscharen said:

    Not the worst WTF I've seen in my current project, but it is a bit facepalm worthy:

    var recordYear = int.Parse(DateTime.Now.Year.ToString().Substring(2));

    This got replaced with a more sensible

    var recordYear = DateTime.Now.Year % 100;

    Meh, I've been writing code for a decade and I would've probably used the first one. Assuming speed isn't an issue here (and if it is, you've got bigger problems) then it's a case of six of one/half a dozen of the other.

    Sorry, you fail the Y10k compliance test.



  • @morbiuswilters said:

    @DrPepper said:
    Concur. The point is not the speed of the thing; it's the readability. It takes a long time to figure out what the first one is doing; the second one takes a fraction of a second.

    The most readable would just be using the date library's "format as 2-digit year" option..

     

    What date library do you use that has a built in function to return an integer value representing the 2-digit year?




  • @The_Assimilator said:

    Meh, I've been writing code for a decade and I would've probably used the first one. Assuming speed isn't an issue here (and if it is, you've got bigger problems) then it's a case of six of one/half a dozen of the other.

     

    Well, since I only have 230% of your experience, I will take this comment under advisement.....

     



  • @lscharen said:

    What date library do you use that has a built in function to return an integer value representing the 2-digit year?

    In C#, it's "yy" in the format string. JavaScript alas does not have one.



  • @lscharen said:

    @morbiuswilters said:

    @DrPepper said:
    Concur. The point is not the speed of the thing; it's the readability. It takes a long time to figure out what the first one is doing; the second one takes a fraction of a second.

    The most readable would just be using the date library's "format as 2-digit year" option..

     

    What date library do you use that has a built in function to return an integer value representing the 2-digit year?

    Why would it have to return an integer? Most languages easily convert strings to integers..



  • @morbiuswilters said:

    @lscharen said:

    @morbiuswilters said:

    @DrPepper said:
    Concur. The point is not the speed of the thing; it's the readability. It takes a long time to figure out what the first one is doing; the second one takes a fraction of a second.

    The most readable would just be using the date library's "format as 2-digit year" option..

     

    What date library do you use that has a built in function to return an integer value representing the 2-digit year?

    Why would it have to return an integer? Most languages easily convert strings to integers..

    Because that's what this code needed

      int.Parse(DateTime.Now.Year.ToString().Substring(2));

    It was converting an integer to a string, chopping off the last two charaters and then parsing the string back to an int.

     



  • @lscharen said:

    Because that's what this code needed

      int.Parse(DateTime.Now.Year.ToString().Substring(2));

    It was converting an integer to a string, chopping off the last two charaters and then parsing the string back to an int.

    Ok. What does that have to do with what I said? Are you just talking to yourself now?



  • @dhromed said:

    @The_Assimilator said:
    Meh, I've been writing code for a decade and I would've probably used the first one. Assuming speed isn't an issue here (and if it is, you've got bigger problems) then it's a case of six of one/half a dozen of the other.
    Stop writing code. Programming is not for you.
    Amen, or in modern lingo: +1 (can you imagine a prayer ending with the congregation saying +1?).

     



  • @TGV said:

    can you imagine a prayer ending with the congregation saying +1?

    uncontrollable eye twitch



  • @Planar said:

    Sorry, you fail the Y10k compliance test.

    This also recalls not so fond memories of a number truncation function that converted the number to a string, truncated the string before the decimal point, and back to a numeric type. Guess what happens if the conversion to string decides to use scientific notation. Guess who managed to do some other shit which ultimately caused said function to be called with something like 1e-9.

    Shit, whenever you convert an object of arbitrary type T to string, manipulate the string, and convert back to T, you should, dunno, at least read the wikipedia page about that type before trying again.



  • @morbiuswilters said:

    Why would it have to return an integer? Most languages easily convert strings to integers..

    If you are starting with an integer and you want an integer at the end then why the heck would you use a string at all?



  • @havokk said:

    @morbiuswilters said:
    Why would it have to return an integer? Most languages easily convert strings to integers..

    If you are starting with an integer and you want an integer at the end then why the heck would you use a string at all?

    Do you people really not understand how date formatting works? It outputs a string based on the format you specify. You can convert that to an integer if you need it as an int. It's not "starting as an integer" (albeit, the internal representation is surely using an integer, but that's irrelevant to what we are trying to accomplish..)



  • @TGV said:

    @dhromed said:

    @The_Assimilator said:
    Meh, I've been writing code for a decade and I would've probably used the first one. Assuming speed isn't an issue here (and if it is, you've got bigger problems) then it's a case of six of one/half a dozen of the other.
    Stop writing code. Programming is not for you.
    Amen, or in modern lingo: +1 (can you imagine a prayer ending with the congregation saying +1?).

     

    -1 to both of yous. If your basis to judge that guy's skill is a single statement where he was merely indicating a preference between two suboptimal solutions, then hopefully hiring or managing developers is not in your job description.




  • @lscharen said:

    @morbiuswilters said:

    @lscharen said:

    @morbiuswilters said:

    @DrPepper said:
    Concur. The point is not the speed of the thing; it's the readability. It takes a long time to figure out what the first one is doing; the second one takes a fraction of a second.

    The most readable would just be using the date library's "format as 2-digit year" option..

     

    What date library do you use that has a built in function to return an integer value representing the 2-digit year?

    Why would it have to return an integer? Most languages easily convert strings to integers..

    Because that's what this code needed

      int.Parse(DateTime.Now.Year.ToString().Substring(2));

    It was converting an integer to a string, chopping off the last two charaters and then parsing the string back to an int.

     

    Clearly the right solution is:



    DateTime.Now.Year.AddYears(-2000)



    If "Now" happens to be in the past for some reason, open a ticket. Same goes for distant future.

    Edit: the year's in the wrong place but you get the idea</b


  • Discourse touched me in a no-no place

    @lscharen said:

    @morbiuswilters said:

    @DrPepper said:
    Concur. The point is not the speed of the thing; it's the readability. It takes a long time to figure out what the first one is doing; the second one takes a fraction of a second.

    The most readable would just be using the date library's "format as 2-digit year" option..

     

    What date library do you use that has a built in function to return an integer value representing the 2-digit year?


    %y. Do you really not know this stuff?



  • @Ronald said:

    -1 to both of yous. If your basis to judge that guy's skill is a single statement where he was merely indicating a preference between two suboptimal solutions, then hopefully hiring or managing developers is not in your job description.
     

    It's not, but I'm allowed to use hyperbole to express my disdain for his willful indifference to this bad code.



  • @TGV said:

    Did the conspiracy to slowly introduce Y2k1 problems already start?
    At a guess, all of use would be either dead or at the very least retired, so it's not a huge problem. For us at least.

    "Gramps! Gramps! You have to fix the Y2k1 problem! The world will come to an end!"

    "Huh? What's that? You have to speak up, junior. My ears are not what they used to be."

    [Cue:horrified looks as outside, mushroom clouds start rising into the sky]



  • @lscharen said:

    @The_Assimilator said:

    Meh, I've been writing code for a decade and I would've probably used the first one. Assuming speed isn't an issue here (and if it is, you've got bigger problems) then it's a case of six of one/half a dozen of the other.

     

    Well, since I only have 230% of your experience, I will take this comment under advisement.....

     

    Given I have 174% of your experience, I would advise you not to!!!  [Grin]



  • @TheCPUWizard said:

    @lscharen said:

    @The_Assimilator said:

    Meh, I've been writing code for a decade and I would've probably used the first one. Assuming speed isn't an issue here (and if it is, you've got bigger problems) then it's a case of six of one/half a dozen of the other.

     

    Well, since I only have 230% of your experience, I will take this comment under advisement.....

     

    Given I have 174% of your experience, I would advise you not to!!!  [Grin]


Log in to reply