A question for the peoples..

Is compact, efficient code better?

Or is having a lot of small files with easily readable code better?

(I'm writing c++ for a robot)

  • 10
    I usually go with small files approach and don't like putting multiple things in one file, for example ItemModel + Item in one file, each goes to its own file. As for login I prefer that each file handles its task only, for example: UserRepository
  • 8
    I'm speaking from js experience here but I prefer small files separated by functionality. Afterall, your transpilers/compiler packs it all together anyway so there's no downside to that. And it's easier to make sense of your project
  • 4
    I'll give my two cents here...
    From my JS experience, I would suggest writing more files to separate your logic in files, as @Froot suggested. But use abstractions to write less code.
    On the other hand, having a lot of experience in Haskell (and other FP languages), I would never suggest writing more code. Your time is almost always more valuable than a CPU's time. You should create good abstractions and use them as much as possible. In the Haskell and JS ecosystem this is trivial. In C++ not so much.
    I have some C++ experience (I have written some video rendering software in the last 4 years) but I do not consider myself a C++ expert, so I can not suggest a certain style to follow there.
  • 16
    Readable code is always better. You will read your code a thousand times, so don't try to be too smart.

    People tend to believe that less code is more efficient, but that's not true. A modern compiler will do the optimization way better than most devs ever.
    So don't try to optimize by yourself if you haven't measured it and are 100% sure you need and really have the chance to optimize a piece of code.

    That doesn't mean that compact code is always bad, sometimes its readability is even better. But that may also depend on personal preference, e.g. I would prefer

    int x = isValid() ? 5 : 10;


    int x;
    if( isValid() )
    x = 5;
    x = 10;

    But I know a bunch of people who don't like the ternary operator and would rather use the latter.
  • 5
    This is the best answer.

    If you have no performance issues, write readable code. If you notice that it's running too slow, start reducing lines.
  • 5

    The actual system is very slow. There is like a 20ms wait between line execution and the main cpu clock speed is around 50mhz
  • 4
    Unless you are compromising on the time and/or space complexity of your code, good readable code always trumps less code.

    @ddephor I am the guy who prefers if over ternery, because the cases where a ternery actualy is useful and results in a readable code are one in a million. So if else always.
  • 5
    @irene many folders of tiny files vs. two small files.

    (The many folders version is way more code than the 2 files one)
  • 2
    Always write your code like the next guy that reads it is a serial killer. That's the mantra I live by 😎
  • 2
    Readable or perfect documentation, you will write a lot anyway so you might as well code
  • 5

    I am deciding between command and arcade.

    Currently I have fully functional robot code in arcade
  • 2
    Depends on your memory space if it's embedded. Efficient is best. Optimizations however get it damn close to both
  • 3
    @ramen ok. It looks like we are doing command now.
Add Comment