Priorities for writing code

I've been following SO for a while now, and every so often I see people asking about the fastest way to do something. I certainly acknowledge that code must be well written and even tuned every now and then, but in my everyday work I am much more concerned about maintainability of code and only occasionally do I actually have to tune code to make it go faster.

Therefore I would like to know what priorities others are following when writing code. So in other words: What are the most important attributes of the code? And as a follow-up questions I would like to know who made this decision (management or developers)?


Code must perform its intended task in a time period short enough to be usable. This varies widely between applications. Striving to make your code "fast" when you fail to understand how fast it needs to be is a waste of time; at the same time, striving to make your code "maintainable" when you lack a clear, high-level idea of what your code is meant to accomplish is doomed to failure. Therefore, top priority must be given to understanding the problem you wish to solve; everything else follows from this understanding.

See also: When is optimisation premature?


mdbritt's answer is pretty close to my attitude, but a few other things:

  • I personally value readability over correctness. It's easier (IME) to go from readable but incorrect code to readable and correct code than to get there from correct but unreadable code.
  • I know it sounds like a given, but I think it's hugely important to understand what you're actually trying to do before you start coding. I'm not saying you need to know everything about your whole application before you start - and you can certainly improve the design of some areas as you go; but when it comes to a single class, you should at least know what results you're looking for and why you're doing it.
  • Performance is important for architecture but much less so for implementation. If you get the architecture wrong to start with so that it doesn't scale, you're in trouble - but if the architecture's right, you can fix an implementation later on. I know it's a well-worn adage, but it really does make sense to write the simplest working code first, then profile the application and optimise just the bottlenecks until you're satisfied. (This puts simplicity above efficiency in my version of mdbritt's answer - for most of the code.)

  • Software Quality should be on the top of every code-writing priority list...

  • Maintainability
  • Flexibility
  • Portability
  • Reusability
  • Readability
  • Testability
  • Understandability
  • 链接地址: http://www.djcxy.com/p/39576.html

    上一篇: 何时使用Enum / Int常量

    下一篇: 编写代码的优先级