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条回答
Viruses.
2楼-- · 2019-01-29 15:40

I have a good example :

Read GWT (google web tookit) Source code, you will see that every fool understand it (some english books are harder to read than this code).

查看更多
别忘想泡老子
3楼-- · 2019-01-29 15:41

The first thing you'd notice is that their code follows a consistent coding-style. They always write their structure blocks the same, indent religiously and comment where appropriate.

The second things you'd notice is that their code is segmented into small methods / functions spanning no more than a couple dozen lines at the most. They also use self describing method names and generally their code is very readable.

The third thing you'd notice, after you messed around with the code a little is that the logic is easy to follow, easy to modify - and therefore easily maintainable.

After that, you'll need some knowledge and experience in software design techniques to understand the specific choices they took constructing their code architecture.

Regarding books, I haven't seen many books where the code could be considered "world-class". In books they try mostly to present simple examples, which might be relevant to solving very simple problems but aren't reflective of more complex situations.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-29 15:41
  1. It works
  2. It has unit tests that prove that it works

The rest is icing...

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

I've been programming for 28 years and I find this a tough question to answer. To me good code is a complete package. The code is cleanly written, with meaningful variable and method names. It has well placed comments that comment the intent of the code and doesn't just regurgitate the code you can already read. The code does what it is supposed to in an efficient manner, without wasting resources. It also has to be written with an eye towards maintainability.

The bottom line though is that it means different things to different people. What I might label as good code someone else might hate. Good code will have some common traits which I think I've identified above.

The best thing you can do is expose yourself to code. Look at other people's code. Open Source projects are a good source for that. You will find good code and bad code. The more you look at it, the better you will recognize what you determine to be good code and bad code.

Ultimately you will be your own judge. When you find styles and techniques you like adopt them, over time you will come up with your own style and that will change over time. There is no person on here that can wave a wand and say what is good and that anything else is bad.

查看更多
再贱就再见
6楼-- · 2019-01-29 15:43

i second the recommendation for uncle bob's "clean code". but you may wish to take a look at http://www.amazon.com/Implementation-Patterns-Addison-Wesley-Signature-Kent/dp/0321413091 as i think this deals with your specific question a bit better. good code should leap off the page and tell you what it does/how it works.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-29 15:43

Good code is easy to understand, easy to maintain, and easy to add to. Ideally, it is also as efficient as possible without sacrificing other indicators.

查看更多
登录 后发表回答