• Buttons@programming.dev
    ·
    10 months ago

    Shorter code is almost always better.

    Should you use a class? Should you use a Factory pattern or some other pattern? Should you reorganize your code? Whichever results in the least code is probably best.

    A nice thing about code length is it's objective. We can argue all day about which design pattern makes more sense, but we can agree on which of two implementations is shorter.

    It takes a damn good abstraction to beat having shorter code.

    • lysdexic@programming.dev
      ·
      10 months ago

      but we can agree on which of two implementations is shorter.

      Shortness for the sake of being short sounds like optimizing for the wrong metric. Code needs to be easy to read, but it's more important that the code is easy to change and easy to test. Inline code and function calls are renowned to render code untestable, and introducing abstract classes and handles is a renowned technique to stub out dependencies.

    • TanakaAsuka@sh.itjust.works
      ·
      10 months ago

      I mostly agree with this but more than shorter code I value readability, I would rather take 3 lines to be clear to any developer than use some obscure or easy to misunderstand structure to write it in 1.

    • CptKrkIsClmbngThMntn [any]
      ·
      10 months ago

      I think this is accurate on a larger scale, but I'll often do things like breaking up a large chain of methods with an interim variable just for readability. A few lines of simple math is better than one line of bit shifting wizardry that does the same thing but doesn't show the semantic meaning of the operation.

      • Buttons@programming.dev
        ·
        10 months ago

        I agree. 100 lines of code may be 3x better than 300 lines of code, but 1 line of code isn't 3x better than 3 lines of code.