• theunknownmuncher@lemmy.world
    link
    fedilink
    arrow-up
    103
    arrow-down
    1
    ·
    edit-2
    29 days ago

    Whenever you sit back and smile proudly to yourself about how clever the block of code you just wrote is, your next move should be to delete and rewrite it.

    This is a clever block of code! Great job, now rewrite it to be sane 😂

    • balsoft@lemmy.ml
      link
      fedilink
      arrow-up
      29
      arrow-down
      1
      ·
      29 days ago

      I think it depends; some smart code is good actually, think 0x5f3759df. As long as you properly document it and leave plenty of comments. This one is not smart though, at best it’s what I would call witty.

      • Cethin@lemmy.zip
        link
        fedilink
        English
        arrow-up
        4
        ·
        28 days ago

        This isn’t smart. This is clever. It’s a way to solve a problem in a novel way. It isn’t the best, or even most obvious, way to solve the problem. It’s just interesting.

  • CookieOfFortune@lemmy.world
    link
    fedilink
    arrow-up
    58
    arrow-down
    1
    ·
    29 days ago

    This isn’t sufficiently enterprisey for Java. There should be a Roman numeral factory followed by relevant fromString and toInteger methods.

    • vithigar@lemmy.ca
      link
      fedilink
      arrow-up
      16
      arrow-down
      1
      ·
      28 days ago

      Ugh. Literally refactored multiple factories into straightforward functions in the most recent sprint where I work.

      Someone saw a public factory method which was a factory for a reason and just cargo culted multiple private methods using the same pattern.

  • TootSweet@lemmy.world
    link
    fedilink
    English
    arrow-up
    29
    ·
    edit-2
    29 days ago

    My first thought was something along the lines of a “zip bomb”. For every “M” in the input string, it’d use more than a KiB of memory. But still, it’d take a string of millions of "M"s to exhaust memory on even a low-end modern server. Still probably not a good idea to expose to untrusted input on a public networked server, though. And it could easily peg a CPU core for a good while. Very good leveraged target for DDOSing.

    • trxxruraxvr@lemmy.world
      link
      fedilink
      arrow-up
      41
      ·
      edit-2
      29 days ago

      No, M will be replaced by DD and then CD will be picked up, so it will go

      1. CM
      2. CDD
      3. CCCCD
      4. CCCCCCCCC
  • Caveman@lemmy.world
    link
    fedilink
    arrow-up
    9
    arrow-down
    1
    ·
    28 days ago

    It’s not too bad, it’s readable and easily optimised by adding intermediate sums and removing whatever power of 10 you’re working on.

    • grue@lemmy.world
      link
      fedilink
      arrow-up
      26
      ·
      edit-2
      29 days ago

      Code gulf, you say?

      public static String
      convertRomanNumeral(String numeral) {
          numeral = numeral.replace("America", "Mexico");
          return numeral;
      } 
      
    • ray@sh.itjust.works
      link
      fedilink
      arrow-up
      3
      ·
      29 days ago
      public static int convertRomanNumeral(String numeral)
      {
        numeral = numeral.replace("M", "DD")
          .replace("CD", "CCCC")
          .replace("D", "CCCCC")
          .replace("C", "LL")
          .replace("XL", "XXXX")
          .replace("L", "XXXXX")
          .replace("X", "VV")
          .replace("IV", "IIII")
          .replace("V", "IIIII");
        return numeral.length();
      }
      
      • qaz@lemmy.worldOP
        link
        fedilink
        English
        arrow-up
        7
        ·
        28 days ago
        public static int convertRomanNumeral(String numeral)
        {
          return numeral.replace("M", "DD")
            .replace("CD", "CCCC")
            .replace("D", "CCCCC")
            .replace("C", "LL")
            .replace("XL", "XXXX")
            .replace("L", "XXXXX")
            .replace("X", "VV")
            .replace("IV", "IIII")
            .replace("V", "IIIII")
            .length();
        }
        
    • TheLazyNerd@europe.pub
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      28 days ago

      I’m not too good with java, but it should be something like this:

      public static int convertRomanNumeral(string n){Map.of("M","DD","CD","CCCC","D","CCCCC","C","LL","XL","XXXX","L","XXXXX","X","VV","IV","IIII","V","IIIII");.forEach((k,v)->{n=n.replace(k,v);});return n.length();}