Hacker News new | ask | show | jobs
by moe 4860 days ago
A good program is 500 lines or less.
4 comments

I don't think that's true, even if you look at most open source software. On one extreme, Mediawiki is a pretty complicated piece of software, approximately 900k lines of "core" PHP/JS code plus another 1.5 million lines worth of extensions, many of which are pretty mandatory for basic functionality.

Is it "good" or "bad"? I'd argue that it's both. It's an important piece of software but a bad architecture, and it's grown in complexity over the years. I think you could write a piece of functional wiki software in about 500 lines of code, but complexity and features win in the marketplace, even if it's just for "mindshare".

I type at least 60,000 characters before inserting a line break.

I joke, but I've seen some php before that was at least 600 chars before line breaks, I have no idea how they wrote it like that.

Broken 'enter' key; it's the only way I can imagine while clinging to sanity.
They didn't write it: it happened, by which time it was too late...
Editors that wrap long lines for display.
Wow, it seems rather arbitrary. Can you elaborate?
In 500 lines you can write a self-hosting compiler, a checkers AI, a usable text editor, a compiling-to-DFA regex matcher, Bayes net inference, or a ray tracer, to mention the first examples that come to mind. (Ordinary lines, not golfed.) Most programs aren't so polished, but when yours gets 10 times longer than a Lisp compiler, it's worth asking why.
Yes, it's arbitrary and there are of course exceptions and perhaps domains where it may not apply at all. It's just a recurring observation that I've made (mostly but not only) in dynamic languages.

Pretty much all good code that I've read or written was compartmentalized into units of roughly 500 LOC. A big program may be composed of many such units, but it was almost always a bad sign when a divisible part would exceed the "magic" number.

What comprises a divisible part of course also varies by language; at the least it'd be the LOC-per-file, but usually it'd be a self-contained and separately tested module.

In a moment someone will probably come up with a great piece of software where this doesn't hold true, I'd actually be curious to see it.

And a program is usually made of many units so is much larger then 500 LOC
So, you actually mean functions instead of programs?
No.

I mean an isolated unit that could be ripped out at any time and would be immediately useful on its own.

Anyway, abecedarius (below) has phrased it better than I could.

Those 'isolated units' are commonly called libraries then.
No. They are called "module".
I'm not sure I agree. How about: "there's no such thing as a bad short program".
Depends on the problem. Sometimes a "short program" means omitted error handling, lax input validation, not covering necessary edge cases and generally the sort of thing that leads to bugs and security holes. I prefer the word "concise"- brief but comprehensive.
At least if the program is short then you can work out that this is the case.