Programming Pitfalls - 002 - Structure your code logically
Second of the biggest trap new programmers (and some experienced ones) can fall into is not structuring their code logically. This might save you a few minutes when you are building something but should really be avoided. Not only you will save a lot of time in future but will also love yourself that you did something better. Things to keep in mind when programming to a better structure are:
Comment, Comment and Comment
As somebody said before, comment your code as if your program will be maintained by a serial killer who knows your address. You would not mess with such a person so why risk it, comment all the things.
Your program should read better
Your code should be written with your very junior colleague in mind. If you do not have a junior now just hope you will get a junior to help you out and your code should be structured easy enough for a student to read. Big hint: read comment, comment and comment again
Structured code makes it easier to find mistakes
Your mind is a great optimiser and data compressor. Amazingly your mind has the the ability to skip information based on assumptions. For most of you, the brain would have assumed there is only one "the" in last sentence and skipped the second one. Similarly when you have very small but crucial spelling or syntax mistakes your mind can easily skip it unless you spot it due to character mis-alignment.
Quick example:
user_permissions = 0; user_permissoins = data; if(user_permissions<10) exit;
user_permissions = 12;
user_permissoins = data;
if(user_permissions<10) exit;
Which one you found easier to spot mistake in?
Another example in Android SDK
Structured code leaves no place to hide for that evil semi-colon
It is one of the most time wasting issue when you can not figure out why your loop is not running.
Quick example:
int_i = 0; for(i=0;i<10;i++);{ printf("%d",i);};
int_i = 0;
for(i=0;i<10;i++);
{
printf("%d",i);
};
Indentation makes code more obvious to read
If you are going to logically analyse your code for mistakes you should better indent it logically. This is another one of benefits that are explained better with an example.
Quick example:
user_permissions = 10
printf("Checking permissions");
if (user_permissions>3) {
printf("Welcome user");
}
Look out for false indentation
Carrying on with above logic, make sure you do not have false indentation on code that should not be indented. This is another one of benefits that are explained better with an example.
Quick example:
user_permissions = 10
printf("Checking permissions");
if (user_permissions>3)
printf("Welcome user");
printf("Your permission level is good at: %d",user_permissions);
Use indentation with blocks of code and use comments to specify start & finish of a block
Generally the consensus is that each code block gets indentation, and a sub code block gets another indentation. If you think that it could get worse to read with 6 levels of sub-blocks then you should better worry about re-structuring your code logic to lesser sub-blocks. Never forget to comment the end of block. Specially in IDEs that do not auto highlight the ending of a block when you select the start of a block
Use a helpful IDE/Editor
A lot of above issues can be reduced if you use an intelligent IDE/Editors. Some IDEs/Editors give your variable highlighted colour when you select a variable name. i.e. Notepad++, IntelliJ. Some of IDEs/Editors highlight block of code you are in or simply highlight the end of block when your cursor is on start of block. i.e. Notepad++ with plugins, IntelliJ, Dreamweaver, Eclipse with plugins
As always, if I remember anything else I reserve the right to edit my post later. If you think I have forgotten something please do message me.