Krista's Coding Corner

18.03.2012

Good code? -style and modularity

Consistent style

Consistent style is quite big topic but shortly said, it means keeping same style: use same method of creating variable names, use same amount of spaces and so on.

I prefer writing my if-sentences like: if( all_is_ok ) but some might use if(all_is_ok) and some would write the variable as allIsOk (really bad example… but this is why I prefer using _). Just use the same style all the time. If doing co-operation with others, there needs to be some sort of consensus how the code should look alike. Otherwise you code will look like crap and it is A LOT harder to read.

There are multiple standards for languages using {} on how to use them. Pick one. (Search “indent style” for example).

Consistent style just makes things more clear and simple, reader knows what to except and the attention doesn’t go into unnecessary things. In writing similar thing would be grammar, ever read text full of grammar and spelling mistakes? It is just easier if the text is well written according the rules (==grammar).

Modularity

When creating code, slice our code into small pieces. Use more functions, just shorter. This makes your code a lot easier to debug! It also gives you a change to use same functions in variable cases. It is just silly to write many similar clips of code, if you could just write one little function for that. Sure, copy-paste is easy but if you need to use it, there is something wrong and it is a clear signal that you could do things more correctly and elegantly.

Cut-and-paste is the only right way :)

blog comments powered by Disqus