views:

127

answers:

6

What are some ways in which I can write code that is easily modified?

The one I have learned from experience is that I almost always need to write one to throw away. That way I have developed a sense of the domain knowledge and program structure required before coding the actual application.

A: 

The easiest way to modify a code is NOT to write code. Write pseudo code not just for algo but how your code should be structured if you are unsure.

Designing while writing code never works...for me :-)

Aditya Sehgal
+5  A: 

The general guidelines are offcourse

  • High cohesion, low coupling
  • Dont repeat yourself
  • Recognize design patterns and implement them
  • Dont recognize design patterns where they are not existing or necassary
  • Use a coding standard, stick to it
  • Comment everyting that should be commented, when in doubt : comment
  • Use unit tests
  • Write comments and tests before implementation, that way you know exactly what you want to do
  • And when it goes wrong : refactor, refactor, refactor. With good tests you can be sure nothing breaks

And oh yeah:

read this : http://www.pragprog.com/the-pragmatic-programmer

Everything (i think) above and more is in it

Peter
A: 

Here is my current experience: I'm working (Java) with a kind of database schema that might often change (fields added/removed, data types modified). My strategy is to parse this schema and to generate the code with apache velocity. The BaseClass generated is never modified by the programmer. Else, a MyClass extends BaseClass is created and the logical components of this class (e.g. toString() ! )are implemented using the 'getters' and the 'setters' of the super class.

Pierre
+1  A: 

I think your emphasis on modifiability is more important than readability. It is not hard to make something easy to read, but the real test of how well it is understood comes when someone else (or you) has to modify it in repsonse to changing requirements.

What I try to do is assume that modifications will be necessary, and if it is not really clear how to do them, leave explicit directions in the code for how to do them.

I assume that I may have to do some educating of the reader of the code to get him or her to know how to modify the code properly. This requires energy on my part, and it requires energy on the part of the person reading the code.

So while I admire the idea of literate programming, that can be easily read and understood, sometimes it is more like math, where the only way to do it is for the reader to buckle down, pay close attention, re-read it a few times, and make sure they understand.

Mike Dunlavey
A: 

Readability helps a lot: If you do something non-obvious, or you are taking a shortcut, comment. Comments are places where you can go back and refactor if you have time later. Use sensible names for everything, makes it easier to understand what is going on.

Continuous revision will let you move from that first draft to a better one without throwing away (too much) work. Any time you rewrite from scratch you may lose lessons learned. As you code, use refactoring tools to eliminate code representing areas of exploration that are no longer needed, and to make obvious things that were obscure. The first one reduces the amount that you need to maintain; the second reduces the effort per square foot. (Sqft makes about as much sense as lines of code, really.)

Modularize appropriately and enforce encapsulation and separation of logic between your modules. You don't want too many dependencies on any one part of the code or that part becomes inherently harder to understand.

Considering using tried and true methods over cutting edge ones. You give up some functionality for predictability.

Finally, if this is code that people will be using before and after modification, you need(ed) to have an appropriate API insulating your code from theirs. Having a strong API lets you change things behind the scenes without needing to alert all your consumers. I think there's a decent article on Coding Horror about this.

Alex Feinman
+1  A: 
The Wicked Flea