What would be the best programming guideline/tip to follow to become a better programmer?

My favorite one is from the book The Pragmatic Programmer: From Journeyman to Master

Don't Live with Broken Windows

Their case is: One broken window, left unrepaired for any substantial length of time, instills in the inhabitants of the building a sense of abandonment. People start littering. Graffiti appears.

This theory inspired police department in New York and other cities to crack down on the small stuff in order to keep out the big stuff. It works: keeping on top of broken windows, graffiti, and other small infractions has reduced the serious crime level.

So, don't leave "broken windows" (bad design, wrong decisions, or poor code) unrepaired. Fix each one as soon as it is discovered. Neglect accelerates the software rot faster than any other factor.

Suggest your programming guidelines/tips.

+3  A: 

Do not reinvent the wheel. Code to reuse.

I prefer the DRY -- don't repeat yourself -- acronym. Coding for reusability sometimes leaves you open to violating YAGNI - you aren't going to need it -- if you try to start developing for reuse before you actually have a second use for it.

Test your code (and code against tests)

Eran Galperin
+2  A: 

hmm... without going into anything language specific, I can only think about these few generally sound principles about the process of programming:

  • always write code with the #1 goal that it can be read easily.
  • fix bugs before adding further behaviour to programs.
  • premature optimization is the root of all evil (Knuth).
Not sure I always agree with #2 -- what if the customer can live with an obscure bug, but needs a critical feature? If you can't do both, which should you do first? +1 for #1, though.
Hmm.. Your reasoning is right, IMHO. I always tell my boss that I must fix my bugs first, because I found that it takes me longer to fix bugs if more time has passed. And if it takes me less time to fix bugs, I look better. :)
+3  A: 

Don't let serendipity be your copilot!

Understand very clearly what every line of code that you write is doing. If you can't explain it, you can't debug it.

Agreed. Too many people code by permutation: they make random changes and guess at syntax.
Bill Karwin
+10  A: 

Build the simplest solution that could possibly work.

Or KISS: keep it simple stupid.

+1  A: 

When you find a bug, take the time to fix it now while you can still find it and remember how to fix it!

Brian Knoblauch

Do your best to read about patterns. Try to absorb where patterns should exist, when you can recognize anti-patterns, and what the purpose of patterns are to begin with. It was years before I realized that all I really coded were anti-patterns.

+3  A: 

For web development...

Develop in Firefox* first, then test in IE.

* (Firefox, Safari, Opera, Chrome, Pogo, (e.g. any other standards based browser))


When trying to solve a problem with software, first at least consider the "right way" to solve it--given whatever definition of "right" you have. Only after considering this, account for the feasibility and practicality of using that solution, and adjust as necessary to come up with a reasonable solution given the project's constraints.

Although this approach might seem overly naive or idealistic, time and time again I've seen others and myself go with a sub-optimal solution that really isn't much easier or more beneficial than a "better", more principled solution. (And yes, it does end up biting us in the end... although that "end" might be a year or more later.) The main reason seems to be that we get into such a single-minded mode of finding the "simplest fix" or the "least risky fix"--which is necessary for some projects--that we sometimes forget to consider some of the most important tenets of software design.

+7  A: 

DRY -- Don't Repeat Yourself. Don't duplicate any piece of information as it runs the risk of becoming out of date. For instance, if you have a "Rectangle" class (or DB table), and you have a "length" and "width" variable, then you don't an "area" variable. Make it a function instead.

On another note, too many programmers focus on what the customer asks for, rather than what they really want. When you get requirements from a customer, try to understand what problems they are trying to solve. Your knowledge of technology combined with their knowledge of the business practices can provide better solutions than either of you could have found on your own.

Try to continually learn new or better methods. New technologies, pitfalls to avoid, best practices, etc. I think that the desire to learn more is one of the things that separates average programmer from good programmers. That desire to always learn more is probably something that all of us who are reading this thread have in common.

Mitchell Gilman
Shape is a bad example in this case, as having a length and width only works if you know what kind of shape it is, whereas area is always correct. This isn't so bad with classes, as area can be an abstract function, but in a DB table...
R. Bemrose
You're right. I'll make it a "Rectangle"
Mitchell Gilman
+2  A: 

Clean, readable code matters. If you can't read it you can't maintain it. Get rid of long, rambling or pointless comments. Write small well named functions that do only one thing. Choose variable names wisely and use whitespace liberally to make your code more readable.

+3  A: 

Document your code. I mean by that :

  • 'public API documentation'
  • internal comments if really needed

The goals is two answer the two age-old questions:

  • how do I use this API ? (=> external public API documentation)
  • If I revisit this code in 2 weeks, will I still be able to know what it does by just looking at it (=> few well-placed internal comments)
+1  A: 

Establish some metrics.

Use the code static analysis tools you are comfortable with, and do not forget that you need to analyze their trends, not just their raw results.

That advice can be duplicated for other populations than just developers: project leaders and stakeholders will benefits from this analysis.

+4  A: 

Don't build in support for features you don't need right now.

It can be hard to stop thinking of ways to make something awesome an architect it perfectly for them all to work together seamlessly, but stop daydreaming and do useful work now.

+2  A: 

Never code drunk. Remember Windows ME? :-)

On a serious note: build what the customer wants, not what the customer tells you they want. Do this using small, frequent releases to get feedback on real, implemented (or needed, but not implemented) features.


I have always liked the quote that scrolls by in the About screen in Sharp Develop

"The most successful method of programming is to begin a program as simply as possible, test it, and then add to the program until it performs the required job"

PDP 8 Handbook, Pg 9-64

IMHO this is KISS in a nutshell :)

Andy Webb
+5  A: 

"The secret to write good code is to write it as if the next one that will read your code is a serial killer that knows where you live."

<sarcasm>Yes, fear is such a wonderful motivator.</sarcasm> Why not just write good, readable code because we ought to write good code? Or, maybe, serial killers only care about features and not code quality. If it's just a joke, you might want to include a smiley, otherwise it's not very helpful.
its a joke. why so serious?
I think the sentiment is wrong -- you should write good code because its better than bad code, not because someone else might read it. There is a serious principle hiding behind the laughs that I disagree with.
Having someone standing behind you with an axe in his hand is a great motivator. Not that I would like it, but it is!
+3  A: 

The book Prefactoring has a wonderful collection of maxims.

However, here are a few of my favorites (from a variety of sources):

  • Code against interfaces, not implementation.

  • Don't Repeat Yourself.

  • If you don't know how to test it, you don't know enough to write it.

  • Do the simplest thing that could possibly work. (As a friend remarked, that doesn't mean, "Do the stupidest thing that appears to work.")

  • Code unto others as you would have them code unto you.


Study best people's code.

+1  A: 

Work on open-source projects with really great and experienced developers. See what they do.

On the other hand, I've worked with some Ph.D.'s who had horrible habits as software engineers. One such "expert" told me he didn't have to write tests for his code because Gottfried Leibniz (1646-1716) already proved it all correct.
Bill Karwin
That's because Leibniz is really Jon Skeet.

I found it very helpful to debug (step by step) into any code I just wrote. Several times I found potentially nasty bugs before it becomes an issue. It's also a good way to get you thinking about writing asserts.

+1  A: 

Where it is breaking is why it is breaking. That's my mantra for debugging.

Also, if you are trying to tweak something into the right spot with a range, try doubling your values or halving them, depending on which way you are going (up or down). This works much better than guessing what the "right" value is. Just keep going up or down by doubles or halves and you will arrive at the correct value much faster than any other method I've found.

This applies mostly to game tuning (for me) but I've used it successfully elsewhere.


My favorite maxim this days is probably Don't Repeat Yourself, which is to say, make sure there is a single point of truth for every piece of information in the program. A good example is the layout of a wire format. A good trivial example is the length of a C array in initialized data. Closely related, but much harder to understand and abide by: Every Module Hides a Secret.

Norman Ramsey

It is best to start by writing something small that works. It is almost always easier to start by building a working system according to a good small subset of your requirements, then add functionality later. Do not try to get most of your requirements mostly met; instead, try to get a few of your requirements met and working well and proceed from there.

Patrick Szalapski

YAGNI - You Ain't Gonna Need it.

Build what you need right now, not what you think you might need later. You probably won't and if you do, the requirements will be completely different anyhow.