Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Programming styles for C++

Options
  • 26-08-2015 11:55am
    #1
    Registered Users Posts: 8,090 ✭✭✭


    Is it standard and expected to declare all variables of a similar type on the same row? I ask because I sometimes found it difficult to read.

    i.e.
    int var1 = 1, var2 = 2, var3 = 3, count = 0;
    
    int var1 = 1,
        var2 = 2,
        var3 = 3,
        count = 0;
    
    int var1 = 1;
    int var2 = 2;
    int var3 = 3;
    int count = 0;
    
    I appreciate that this will increase line count, but IMO the 2nd method is more readable - providing that there is not a large amount of state being declared. Must I persevere with the top structure – as in will this be expected?

    Also, the C++ course seems to mandate the K&R Style (even though it is more correct to call it the Stroustrup style. How widespread is this in industry? I ask because I find aligned brackets (Allman Style) more readable as I don’t have to search line ends for the {.

    Is this something to worry about at this stage – i.e. can I carry on with the style I prefer?


Comments

  • Registered Users Posts: 8,090 ✭✭✭funkey_monkey


    float temp[31][24];
    float max = -100.0;
    
    for (int day = 0; day < 31; day++)
       for (int hour = 0; hour < 24; day++)
          if(temp[day][hour] > max)
             max = temp[day][hour];
    cout << "The highest temperature was " << max << endl;
    

    Would it not be a 'better' program to initialise max to the first element in the array and then search for a higher number? I acknowledge this would waste an interation of the loop comparing max with itself though.
    My current understanding is that hour is created/destroyed within the for loop structure so it cannot be manipulated outside - correct?

    I can't see how to simplify the hour counter:
    float temp[31][24];
    float max = temp[0][0];
    int hour = 1;
    
    for (int day = 0; day < 31; day++)
       if(temp[day][hour] > max)
          max = temp[day][hour];
       hour += 1;
       if (hour = 24)
          hour = 0;
    
    cout << "The highest temperature was " << max << endl;
    


    Are there any constants or similar that can be used to represent -inf?


  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,240 Mod ✭✭✭✭L.Jenkins


    Instantly thought of this. http://homepages.inf.ed.ac.uk/dts/pm/Papers/nasa-c-style.pdf NASA released it in '94 and it seems like they have it down to a fine art. I believe Linus Torvalds also has his own thoughts on it as well.


  • Registered Users Posts: 964 ✭✭✭Greyian


    Would it not be a 'better' program to initialise max to the first element in the array and then search for a higher number?
    Yes, definitely, because you could have a scenario where the values in your array are...-150, -200 and -250, which means the actual max is -150, but you'd return -100.
    My current understanding is that hour is created/destroyed within the for loop structure so it cannot be manipulated outside - correct?
    Correct, so you wouldn't be able to say "The maximum temperature was *max* at *hour* o'clock", without changing the code a bit. You could declare variables outside of the for loops (so where you declare max), called "hourMax" and "dayMax" if you wanted, to store those values.
    I can't see how to simplify the hour counter:
    I do most of my programming in C#, so I might mess up the syntax a bit on C++, but here goes an example:
    float temp[31][24];
    float max = temp[0][0];
    
    for (int day = 0; day < 31; day++)
    {
       for(int hour=0; hour < 24; hour++)
       {
          if(temp[day][hour] > max)
             max = temp[day][hour];
       }
    }
    
    cout << "The highest temperature was " << max << endl;
    
    This would start at Day 0, and hour 0. It would then go to Day 0, Hour 1 (because it's running through the hour loop). After it runs hour 23, it would then go to Day 1, Hour 0, and continue on as such, until you reach Day 30, Hour 23, at which point it would output whatever the max was.


  • Registered Users Posts: 8,090 ✭✭✭funkey_monkey


    Thanks - that what I initially was thinking about. However, on the first iteration of the loop you are comparing temp[0][0] with temp[0][0] (as max has been initlaised to temp[0][0]. This was the scenario I was trying to avoid in my second code section above. Although I couldn't figure out how to simplify the counter.

    Which implementation - my second one or GreyIan's would be the most expensive in terms processor utilisation? Although I increment my counter over three commands, would the loop used in GreyIan's example not require a similar amount to service the update of 'hour'?

    Although, I do appreciate that running the redundant iteration of the loop structure make for more simpler and easier to maintain code. I'm just curious as to which is the most/least efficient.


    Thanks.


  • Registered Users Posts: 2,018 ✭✭✭Colonel Panic


    Just use the nested for loops. It's likely the compiler will unroll them anyway. Don't try to second guess the compiler or sweat it about "minor" optimisations like this unless the code becomes a problem.

    Unless you're allocating and destroying objects or doing lots of work in a tight loop, you're fine.

    As for style. Find your own style, one you like and stick to it consistently. If you let it evolve over time, fine, but stick to it. When or if you work on other people's code but it open source or in a job, be consistent with what's there already (if it's consistent at all). Chances are you'll hate some or all of your job's style guidelines. I do, but consistency wins over my feelings on the matter.


  • Advertisement
  • Registered Users Posts: 7,517 ✭✭✭matrim


    Is it standard and expected to declare all variables of a similar type on the same row? I ask because I sometimes found it difficult to read.

    i.e.
    int var1 = 1, var2 = 2, var3 = 3, count = 0;
    
    int var1 = 1,
        var2 = 2,
        var3 = 3,
        count = 0;
    
    int var1 = 1;
    int var2 = 2;
    int var3 = 3;
    int count = 0;
    
    I appreciate that this will increase line count, but IMO the 2nd method is more readable - providing that there is not a large amount of state being declared. Must I persevere with the top structure – as in will this be expected?

    Also, the C++ course seems to mandate the K&R Style (even though it is more correct to call it the Stroustrup style. How widespread is this in industry? I ask because I find aligned brackets (Allman Style) more readable as I don’t have to search line ends for the {.

    Is this something to worry about at this stage – i.e. can I carry on with the style I prefer?

    I've seen people do all three and there is no definitive style. It can depend on the company / project or just person. From my experience it would usually be the first or third. The second style would be fairly rare

    Personally I prefer the third one. I find it can be hard to easily spot what's happening with the first.
    With the second it's annoying to have to tab out and then easily broken if someone comes into the project with different editor settings (tabs instead of spaces). Also you then have to scan up the lines to find what the type is.
    The third is a few extra key presses but easy to see what a variable is and what it is set to all from a quick glance.


Advertisement