Writing easily modified code
What are some ways you can write code that is easy to modify?
The one I've learned from experience is that I almost always have to write in order to throw it away. As such, I developed an understanding of the knowledge and domain structure required before coding the actual application.
a source to share
General advice is to disable
- High grip, low grip
- Don't repeat yourself.
- Recognize design patterns and implement them.
- Don't recognize design patterns where they don't exist or aren't used.
- Use a coding standard, stick to it
- Comment everything that needs to be commented when in doubt: comment
- Use unit tests
- Write comments and tests before implementation so you know exactly what you want to do
- And when it goes wrong: refactoring, refactoring, refactoring. With good tests, you can be sure nothing will break
And oh yes:
read this: http://www.pragprog.com/the-pragmatic-programmer
Everything (I think) is higher and more in it
a source to share
I think your emphasis on mutability is more important than readability. It's 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 change it in response to changing requirements.
What I am trying to do is assume that changes will be needed, and if it is not entirely clear how to do this, leave the codes in the code how to execute them.
My guess is that I might need to teach the reader of the code a little so that he or she knows how to change the code correctly. It takes energy on my part, and it takes energy on the part of the person reading the code.
So when I admire the idea of good programming that is easy to read and understand, sometimes it's more like math, where the only way to do it is to get the reader to button up, pay close attention, re-read it a few times, and make sure they understand.
a source to share
Hang your code on DRY
I learned this before when I was assigned the task of changing the appearance of the web interface. The code was in C, which I hated, and was compiled into a CGI executable. And worse, it was built on a library that was abandoned without permission, without updates, without support, and too many man-hours that were used to change it. In addition to structure, there was a messy web of code made up of various form and element designers, custom string implementations, and various other cryptic things (for a programmer not using C to commit suicide).
For every change I made, there were several, sometimes many, exceptions from the output HTML. Each of these exceptions required a small change or improvement in the form builder, thanks to the lack of inheritance language and therefore only functions and structures, and instead of imposing hours on the command, these exceptions were often recorded frequently.
In my inexperience, I was forced to change the output of each exception, instead of consolidating the changes in the improved form builder. But, trawling 15,000 lines of code for hours after ineffective changes would have caused the code to burn, and the nebula that took a night's sleep for treatment.
Always run your code through DRY-er.
a source to share
Here is my experience: I am working (Java) with some kind of database schema that can change frequently (fields added / removed, data types changed). My strategy is to parse this schema and generate code at apache speed . The generated one is BaseClass
not modified by the programmer. Else is instantiated MyClass extends BaseClass
, and the logical components of that class (eg toString ()!) Are implemented using the "getters" and "setters" of the superclass.
a source to share
Readability helps a lot: if you're doing something non-obvious or using a shortcut, comment. Comments are places where you can go back and refactor if you have time later. Use sensible names for everything, it makes it easier to understand what's going on.
Continuous revision will allow you to move from that first draft to the best without dropping (too much) work. Every time you rewrite from scratch, you might lose your lessons. As you use your code, use refactoring tools to exclude code representing areas of study that you no longer need and to make things obvious that were unclear. The first reduces the amount needed to maintain; the second decreases the force per square foot. (Actually Sqft has the same meaning as lines of code).
Modulate appropriately and enforce encapsulation and logic separation between your modules. You don't want too many dependencies on any one piece of code, or that piece becomes harder to understand.
Consideration of using tried and tested methods over advanced ones. You are giving up some functionality for predictability.
Finally, if this is code that people will use before and after modification, you need (ed) to have an appropriate API that isolates your code from theirs. Having a strong API allows you to change things behind the scenes without requiring all your consumers to be notified. I think there is a decent article on Coding Horror about this.
a source to share