Hey guys. I was wondering what are some ways programmers, especially beginners, write code that are "syntactically" inefficient. I am doing some research for my college program and have found only a couple of these inefficiencies. Such as:
1. Not using variables that were defined.
2. Not using functions that were defined.
3. Declaring variables as public variables.
4. Trying to access private member data using a global function
just to name a few. I would however, like to know more. I myself am not that experienced a programmer, but looking back on my earlier programming days, these were some of the common mistakes that I personally made. So if you guys could share some of your opinioins on how programmers today write code in inefficient ways then that would be great. Thank you :)
one basic thing I forget almost every-time is incrementing the value of a counter without initializing it....
Well, the golden rule is to always initialise everything ...... Preferably delay until there is a sensible value to assign to it, that way there can be declaration and assignment in one statement :+)
I reckon a good concept is to always think about what can possibly go wrong. Common problems include: using the wrong type; int instead of double; mixing signed and unsigned types; Integer division, and division by zero. Lack of validating input data is another big problem, as is not initialising all class members, and not maintaining class invariants. Not compiling with sufficient warning levels is something I see all the time. Bad variable names: not using meaningful variable names, over abbreviating. Using inefficient algorithms, try to imagine that you always have at least 1 million items - how will the performance be? Test the performance by timing the cpu usage.
Using complicated structures like this directly (well above is a slight exaggeration) was a big source of syntax inefficiency for me, always bogging me down and causing me confusion. Now I use more custom classes and structs, wrappers, typefefs, auto, etc.