What does a good programmer's code look like?

2019-01-29 15:00发布

I am a hobbyist programmer (started with VBA to make excel quicker) and have been working with VB.NET / C#.NET and am trying to learn ADO.NET.

A facet of programming that has always frustrated me is what does 'good' look like? I am not a professional so have little to compare against. What makes a better programmer? Is it:

  • They have a better understanding of all the objects / classes / methods in a given language?
  • Their programs are more efficient?
  • The design of their programs are much better in terms of better documentation, good choice of names for functions etc.?

Put another way, if I were to look at the code of a professional programmer, what is the first thing that I would notice about their code relative to mine? For example, I read books like 'Professional ASP.NET' by Wrox press. Are the code examples in that book 'world class'? Is that the pinnacle? Would any top-gun programmer look at that code and think it was good code?

30条回答
ら.Afraid
2楼-- · 2019-01-29 15:44

Personally, I'll have to quote "The Zen of Python" by Tim Peters. It tells Python programmers what their code should look like, but I find that it applies to basically all code.

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-29 15:46

Quoting Fowler, summizing readability:

Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.

'nough said.

查看更多
做个烂人
4楼-- · 2019-01-29 15:46

(I use "he" below because this is the person that I aspire to be, sometimes with success).

I believe that the core of a good programmer's philosophy is that he is always thinking "I am coding for myself in the future when I will have forgotten all about this task, why I was working on it, what were the risks and even how this code was supposed to work."

As such, his code has to:

  1. Work (it doesn't matter how fast code gets to the wrong answer. There's no partial credit in the real world).
  2. Explain how he knows that this code works. This is a combination of documentation (javadoc is my tool of choice), exception handling and test code. In a very real sense, I believe that, line for line, test code is more valuable than functional code if for no other reason than it explains "this code works, this is how it should be used, and this is why I should get paid."
  3. Be maintained. Dead code is a nightmare. Legacy code maintenance is a chore but it has to be done (and remember, it's "legacy" the moment that it leaves your desk).

On the other hand, I believe that the good programmer should never do these things:

  1. Obsess over formatting. There are plenty of IDEs, editors and pretty-printers that can format code to exactly the standard or personal preference that you feel is appropriate. I use Netbeans, I set up the format options once and hit alt-shift-F every now and then. Decide how you want the code to look, set up your environment and let the tool do the grunt work.
  2. Obsess over naming conventions at the expense of human communication. If a naming convention is leading you down the road of naming your classes "IElephantProviderSupportAbstractManagerSupport" rather than "Zookeeper", change the standard before you make it harder for the next person.
  3. Forget that he works as a team with actual human beings.
  4. Forget that the primary source of coding errors is sitting at his keyboard right now. If there's a mistake or an error, he should look to himself first.
  5. Forget that what goes around comes around. Any work that he does now to make his code more accessible to future readers will almost certainly benefit him directly (because who's going to be the first person asked to look at his code? He is).
查看更多
放我归山
5楼-- · 2019-01-29 15:47

I second the recommendation of Bob Martin's "Clean Code".

"Beautiful Code" was highly acclaimed a couple of years ago.

Any of McConnell's books are worth reading.

Perhaps "The Pragmatic Programmer" would be helpful, too.

%

查看更多
Ridiculous、
6楼-- · 2019-01-29 15:47

Good code is where you know what the method does from the name. Bad code is where you have to work out what the code does, to make sense of the name.

Good code is where if you read it, you can understand what it's doing in not much more time than it takes to read it. Bad code is where you end up looking at it for ages trying to work out wtf it does.

Good code has things named in such a way as to make trivial comments unnecessary.

Good code tends to be short.

Good code can be reused to do what it does anywhere else, since it doesn't rely on stuff that is really unrelated to its purpose.

Good code is usually a set of simple tools to do simple jobs (put together in well organised ways to do more sophisticated jobs). Bad code tends to be huge multi-purpose tools that are easy to break and difficult to use.

查看更多
聊天终结者
7楼-- · 2019-01-29 15:48

Read the book Code Complete. This explains a lot of ideas about how to structure code and the the reasons for doing so. Reading it should short-circuit your time to aquiring the experience necessary to tell good from bad.

http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1229267173&sr=8-1

查看更多
登录 后发表回答