[try Beta version]
Not logged in

 
How to make code syntactically efficient.

Jul 10, 2016 at 1:27pm
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 :)
Jul 10, 2016 at 2:05pm
"syntactically" inefficient.

This is a nonsense phrase.
Jul 10, 2016 at 2:24pm
closed account (48T7M4Gy)
Yeah there are syntax errors and poor programming practice.
Syntax errors could be described as fatally inefficient I suppose, at a pinch and in the face of otherwise complete humiliation. :)
Jul 10, 2016 at 3:01pm
+1 kemort

one basic thing I forget almost every-time is incrementing the value of a counter without initializing it....
Jul 11, 2016 at 1:26am
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 :+)



@nz881

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.

Some links:

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html#main
https://akrzemi1.wordpress.com/

Compiler warnings:
http://www.cplusplus.com/forum/general/183731/#msg899203
Jul 11, 2016 at 12:31pm
You mean synthetically inefficient such as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::unordered_map< 
    std::string, 
    std::pair< 
        std::string, 
        std::unordered_map< 
            std::string 
            std::unique_ptr< 
                std::vector< 
                    std::pair< int, float> > > > > > > > >::iterator it1 = m.find( "?" ); 

if( it1 != m.end() )
{
    std::pair< 
        std::string, 
        std::unordered_map< 
             std::string 
             std::unique_ptr< 
                    std::vector< 
                        std::pair< int, float> > > > > > > > & fu = it->second;
//...    
}

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.
Last edited on Jul 11, 2016 at 12:40pm
Jul 11, 2016 at 3:15pm
You can run cppcheck on your code, and see what it suggests as 'performance' warnings
Jul 11, 2016 at 4:42pm
is there cppcheck for some other languages too?
Last edited on Jul 11, 2016 at 4:42pm
Topic archived. No new replies allowed.