Programming Style
Each person has their own style. And your style evolves over time. More importantly, without constant diligence your style varies constantly.
Before we proceed, it never hurts to give Wikipedia a glance. Here is a neat article on style.
Your style (and the consistency of style) is very important. Consistent style use eases the process of coding (fewer decisions to make hence less opportunity for indecision). More importantly, consistent style is critical for ease of reading code.
Think about it: reading code is not easy. The more variable the coding style the harder it is to read the code (and understand it). The more the style varies from your current style, the more difficult it is to comprehend the code.
Many factors impact code readability:
- Language the code is written in
- Style the code is written in
- Variability of the style
- Data structure use
- Algorithm use
- Modularity of code
- Use of recursion
- Variable/function naming
- Commenting
- Over arching design information
- Understanding of inputs (domain) and output (range)
For the sake of this discussion, lets assume all other factors are static and continue explore the impact of style.
In fact, lets take a step back. As time passes, your knowledge and experiences change (probably advancing). Like everything else, evolves. And just like many other things, that evolution can occur autonomously, managed or somewhere in between. Beginners are almost always unconscious of the style evolution. They eventually notice that the longer ago they wrote something, the harder it is to read. While style is not the only reason for this, it is often a big part of it.
If style evolution goes unmanaged, the impact can be significant. Quite often your style changes to avoid a particular issue. If that issue remains a non-issue for a while, your style may very well regress -- kind of like 1 step forward and 2 steps back. Management of style evolution is necessary to avoid regression (loss of good changes).
How does one go about ensuring that desirable style changes persist? In fact, how do you go about detecting style changes (evolution)?
The general answer to this kind of question is something like this:
- Measure original state (current at this time)
- Allow time to pass
- Measure current state
- Compare state changes to make a definite list of changes that have occurred
Periodic re-evaluation of your style allows you several opportunities:
- Chance to move to a more comfortable style
- Chance to improve readability
- Chance to adjust your style to current language(s) and API(s)
- Chance to reduce mistakes (choosing a style that helps avoid mistakes)
- A chance to notice cycles (where you keep alternating between multiple choices for a style element)
- Is your style evolving positively?
All of these statements should help you understand the importance of knowing your style and being able to track its evolution. That evolution happens as you mature and become a more experienced developer. Clearly you want your style to facilitate your improvement instead of hindering you.
Style Elements
Style can have many components. Here are a few to get you thinking:- Indentation
- Block comments
- Inline comments
- Variable naming
- Case choices (upper, lower, camel, ...)
- Use of underscores or other characters
- Use of parentheses/dependence on order of operation
- Testing order (always try to start with a non-address)
- Language subsetting (which command do you prefer over others: while vs repeat, nested if vs case, ...)
- Use of recursion
- Declaration style (one per line, maybe inline brief descriptions, ...)
- Organization choices (structured, ...)
- Loop control counter variable naming (the Fortran inspire use of i, j, k, l, and m as integer loop counters, ...)
- Alternative expressions (i = i + 1 vs i += 1 vs i++ vs ++i)
- Function naming vs variable naming
In fact, many style choices tend to correlate to preference of language.
You should study your style, the style of others (class mates and published code) and the elements of style sufficiently that you feel like your style is the compromise possible for you at this point of your development.
Reformatting
I typically use highlight to reformat code for html display. I am experimenting with astlye for reformatting code (and keeping it still code).Here are some interesting links: