Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As a graduate fresh out of college with a clean slate, how can one go about being a good programmer who writes good maintainable code? How does one learn good coding practices and apply them? Can one get these skills by reading any specific books on Software development or is it something that comes purely from experience? (I don't like to believe that it's purely experience based since I see people here complaining about bright co-workers with years of experience writing bad code). What are some suggestions on getting good at writing clean code?

Passion and ability are assumed.

share|improve this question

closed as not constructive by Yannis, Glenn Nelson, Jarrod Roberson, ChrisF Jan 1 '12 at 19:30

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

As per:… my question seems valid to be asked here without being closed as off topic. – aml90 Jan 1 '12 at 15:39
A good mentor and peer review will be good for you. – user1249 Jan 1 '12 at 16:01

In addition to what others already suggested, though quite unpopular: work, at least partially, on maintenance tasks, if that is an option. And yes, that's maintenance for code written by someone else. Often no fun, but quite an experience. There you see why and how things done by smart programmers go wrong, don't scale, whatever. You'll get to see those parts of the code which are actually run in the real world at customer sites and which need improvement and you may get the opportunity for direct customer contact.

It is my experience that colleagues who come from university/college and start immediately in development projects without working on maintenance task quite often write code which can not be reasonably maintained.

share|improve this answer
+1 Yep, having to extend less than perfect code (even your own from a couple of months back) is a good way to learn what not to do. – Marjan Venema Jan 1 '12 at 19:25

You've got to have the desire to write really good code. If you have that desire, it will come with experience. I think the key is to surround yourself with other good programmers. If you're lucky, you'll find a job that has the sort of programmers you want to become yourself. If not, be active in a programming community (SO, USENET newsgroups, local SIGs, etc).

Most importantly, make sure other people see your code. If you work in a company, ask senior developers whom you respect to do code reviews. Also, work on an open source project or create one yourself. When you write a line of code knowing that many smart people will read it, it can act as an incentive to write better code.

Finally, don't equate "clever" with "good". Really good programmers rarely write clever code. The goal is to write code that a) works, and b) is as understandable as possible. "clever" usually runs counter to that second point, so don't try to show off with fancy bit manipulations or obscure constructs, or saving an extra byte or two in a data structure. Stick to the fundamentals and write code any good programmer can read.

share|improve this answer
+1 for clever != good – Mike Nakis Jan 1 '12 at 16:42
+1 for "Really good programmers rarely write clever code." – Marjan Venema Jan 1 '12 at 19:24

The #1 thing either you have it or you don't have it, and that's brains.

The #2 thing also either you have it or you don't have it, and that's passion.

The rest are in no particular order:

  • Work together with experienced people
  • Find books widely acknowledged as good and read them
  • Experiment with programming on your spare time (comes with passion)
  • Do not let girls, drugs, alcohol, and generally 'being cool' distract you.

(Well, I take back the thing about the girls.)

EDIT I just thought of one more thing to write, which addresses the issue of writing clean code in a more direct way than my other recommendations; who knows, it might appease our anonymous downvoter, in case their downvote was for being off-topic.

Try to write software systems whose complexity lies not within the code, but within the design. What do I mean by this: any program represents a solution to a problem. The complexity of the solution usually (granted, not always, but usually) depends on the complexity of the problem. An overly complicated solution would constitute overengineering, but an overly simple solution would be inadequate. (As Einstein said, things should be as simple as possible; but not simpler.)

Now, there are two ways of putting all the necessary complexity into a program: you can either have a monolithic (very simple) design which "just does it" with lots of complex code and tricks and hacks, or you can have an elaborate (complex) design which closely models the problem and consists of lots of bits of very simple, very straightforward, I could even say boring, code. Always prefer the second. That's what we call clean. (This was just an elaboration on @BryanOakley's statement about clever != good.)

share|improve this answer
Mike, points 1 and 2 are assumed. Thanks for your other suggestions. – aml90 Jan 1 '12 at 15:58
C-:= Oh, and one more thing: stay away from Cobol, it is bad for the brain, and Basic, it is bad for the stomach. Learn some real language like Java, C#, C++, that kind of stuff. – Mike Nakis Jan 1 '12 at 16:08
@downvoter: Your downvote without an explanation does not help me become a better person. Love you too! – Mike Nakis Jan 1 '12 at 16:40
Mike, your answer is constructive, like the other two. +1 , I just got the upvote privilege. – aml90 Jan 1 '12 at 16:50
Anonymous downvoting is not a bad thing. It usually means "I don't have a constructive explanation". And seeing how often down vote comments lead to comment wars, I'd say that no downvote comment is actually a good thing... (and peer reviewed as everything, see how it was balanced out almost immediately?) - Now although I didn't downvote, I can't upvote. Bad advice on avoiding alcohol, you're clearly unaware of the Ballmer Peak. – Yannis Jan 1 '12 at 17:05

Learn how to listen, communicate, learn, and keep learning.

The reasons why many intelligent programmers write bad code are legion (misunderstood requirements, misunderstood technology, poor focus, just having a bad day, ...).

The best way to learn how to be a good programmer is to be able to listen to others, communicate back to them to learn from the experience and not stop learning.

Now, some of the things you need to learn are technical, but many will not be technical but softer skills about how to deal with peers, customers, marketing, and managers (etc. etc.).

share|improve this answer

If nothing else, learn to write lots and lots of good unit tests. If you can prove that your code (good or bad) does what it's supposed to, you're 90% of the way there. Tests document what you code is supposed to be doing. Moreover, they allow you to safely refactor your code when you discover a better way to do the same thing. The tests will show that you didn't actually screw things up.

So start learning about unit testing in whatever language you'll be coding in.

You should probably take a look at Code Complete as well.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.