What makes good code?
Since I'm in the process of shipping out to an uncaring world my new javascript library, one of the nagging questions I have is "is it good"? In the spirit of second-guessing my own choices I did a quick Google survey of opinion on the subject. I've reduced the stuff I found to bullet points, and highlighted things I think are significant to Bindinator.
Bold red items are where I think Bindinator can be significantly improved.
What makes good code good? A digital social research view
Correct
Also demonstrable and testable.Well-Designed
- Modular
- Well-defined interfaces, inputs, and outputs
- Elegant and no more complex than necessary
- Minimal interdependencies
- No hidden dependencies
- Configurable and adaptable
As an aside, one of the problems I perceive with JSON is that it's too tied to Javascript and we should actually further restrict it to not allowing arrays of scalars (i.e. arrays can only be of objects, object properties can be scalars, objects, or arrays), not having both undefined and null, and so on. A few of these restrictions would make dealing with JSON in languages that aren't Javascript less ugly.
Anyway, this restricted subset of JSON would be the basis for my type system, and it would also leverage modern Javascript's syntax sugar for object properties, so a lot of methods could be called like this: foo({bar, baz}) and type-checking in methods would look something like:
Types.verify(arguments, {a: Types.string, b: Types.number, c: Types.number.required, d: Types.number(Math.PI), e: Types.object(…)});
Readable
- Commented
- why not how; the code is how
- up-to-date
- Indented
- Sensible Naming
I have plans on implementing a lot of literate programming ideas, but they're mainly ideas for the moment.
Bindinator's naming is decent, but it's a bit inconsistent in use of underscore_word_separation vs camelCase. I like the way Facebook's coding standards insist on underscore separation for internal symbols and camelCase for exported symbols, so I may move further that way.
Appropriate
- Suitable for intended use
- Approachable for intended users
Robust
- Don't break things
- Fail gracefully
Available
- Can you get it if you need it?
Usable
- Usable, buildable, deployable, runnable
Copyrighted and Licensed
- People need to know where they stand
Under Revision Control
- duh
A good coder is like a good pool player
a good coder writes code that looks like it was easy and straightforward to do. Many of the examples by Brian Kernighan in his books follow this pattern. Part of the "trick" is coming up with a proper conceptualization of the problem and its solution. When we don't understand a problem well enough, we're more likely to over-complicate our solutions, and we will fail to see unifying ideas.
How do you know if you're writing good code?
The biggest clue for me is: When you have to go back and add/modify a feature, is it difficult? Do you constantly break existing functionality when making changes?This seems totally on the money to me, but the problem is, how do you define difficult? I get pretty annoyed if a fairly major feature takes me an evening to implement. Does that mean my code sucks or that I have high standards?
Quality is line one -- 8 Characteristics of Good Software Code
- It works
- It's easy to read
- It's testable
- It's easy to maintain
- It's pleasant to look at
- It's easy to change
- It's simple
- It's efficient
How to you define good code?
From Code Complete:- Comprehensibility
- Reviewability
- Error rate
- Debugging
- Modifiability
- Development time — a consequence of all of the above
- External quality — a consequence of all of the above
- Simplicity
- less code without obfuscation
- Readability
- Modularity
- Layering
- Layers shouldn't know what other layers are up to
- Events should be "blind upward notification"
- Design
- Plan before coding — ideas are cheaper than debugging
- Elegance
- "or realize that you can use recursion — which is almost always elegant" — not sure I agree with this; often find that things that seem like obvious recursive solutions have more elegant/efficient non-recursive solutions and vice versa
- I would suggest elegance = clarity + efficiency + simplicity + correctness
- Clarity
What does a good programmer's code look like?
- well-organized
- comments explain why
- meaningful naming conventions
- well-tested
- not "clever" -- straightforward
- small, easy-to-read, reusable units of computation
I think some of bindinator's source files are uncomfortably large, and one refactoring I've done requires methods to be assigned to b8r, which — among other things — confuses minifiers, which is almost certainly a bad sign.
What makes code great?
The title reminds me of a pithy quotation whose source and exact wording I forget: "The reason that there are no good American writers is that American writers all want to be great." Anyhow…Second response to me is the pithiest:
- It works. (functionality)
- It runs fast. (performance)
- It can be maintained by others. (maintainability)
- It was written quickly. (cost)
The writer also points out that it's almost impossible to achieve all this, so the next step is rewriting.
The third response opens thus:
Maintainability is the goal. Great code can easily be maintained by other developers, including the future you.The top response to me is interesting mostly because I find it bizarre:
- written in the language and style most appropriate to the problem being solved
- solve the problem in a consistent way which is not error-prone
- beautiful to look at and easy to read
- "reusable, and never reinvents the wheel" (DRY!)