Coding Standards / Coding Best practices in C++

前端 未结 17 1623
醉话见心
醉话见心 2021-01-02 09:06

Consider the two code segments below. Which one is better and Why? If you have any other idea, please do mention. Where can I find answers to coding p

相关标签:
17条回答
  • 2021-01-02 09:18

    My habit is to avoid if-blocks this way:

    bool MyApplication::ReportGenerator::GenerateReport(){
        bool report = !( isAdmin()        || isConditionOne() || 
                         isConditionTwo() || isConditionThree() );
        return report ? generateReport() : false; 
    }
    
    0 讨论(0)
  • 2021-01-02 09:20

    Code 1 is, IMO, worst as it does not immediately convey the intendend meaning which is to generate the repoort only in certain circumstances.

    Using:

       if (condition_1) return false;
       if (condition_2) return false;
       ...
    

    would be better.

    Also, what I dislike in code 1 is the fact that it try to mask gotos using a while and breaks (which are gotos). I would then prefer to use directly a goto, at least it would have been easier to see where the landing point is.

    Code 2 might be formatted to look nicely, I guess:

     bool MyApplication::ReportGenerator::GenerateReport(){
         if (!isAdmin()        || !isConditionOne() ||
             !isConditionTwo() || !isConditionThree()) {
            return false;
         }
         return generateReport();
     }
    

    Or something similar.

    0 讨论(0)
  • 2021-01-02 09:21

    I've used similar to both in different circumstances but you've overcomplicated the first IMO:

    bool MyApplication::ReportGenerator::GenerateReport(){
        bool retval = false;
        if (!isAdmin()){
        }
        else if (!isConditionOne()){
        }
        else if (!isConditionTwo()){
        }
        else if (!isConditionThree()){
        }
        else
            retval = generateReport();
        return retval;
    }
    
    0 讨论(0)
  • 2021-01-02 09:27

    Code Complete is a commonly recommended book that goes into some detail about those kinds of stylistic issues. Also consider taking a look at some of the published organization style guides to see if they have any opinions on the issue; Google's Style Guide, for instance.

    As to what I prefer, the second example is much better to my mind. The first is essentially an abuse of the do {} while construct to avoid using a goto, dogmatically sticking to the letter of "avoid gotos at all cost" while missing its spirit of "code for clarity, don't use unobvious language tricks".

    In fact, the only reason to even use a goto at all would be to dogmatically stick to a "only one return statement per function" approach when you could get away with a simple, readable

    if (!isAdmin()){
        return false;
    }
    else if (!isConditionOne()){
        return false;    }
    else if (!isConditionTwo()){
        return false;    }
    else if (!isConditionThree()){
        return false;    }
    else
        return generateReport();
    

    Other thoughts?

    Don't name local variables that are used to hold a computed success state "returnValue" or similar. Obviously whatever you return is the return value, any one who can read C can see what is being returned. Tell me what computation it holds. The name "returnValue" gives me no information as to what it means when it is true or false. In this example "couldGenerateReports" or similar would be far more preferable.

    0 讨论(0)
  • 2021-01-02 09:29

    I don't really like using a do/while loop in this way. One other way to do this would be to break your conditional in Code2 into separate if checks. These are sometimes referred to as "guard clauses."

    bool MyApplication::ReportGenerator::GenerateReport()
    {
        if (!isAdmin())
            return false;
    
        if (!isConditionOne())
            return false;
    
        // etc.
    
        return generateReport();
    
    }
    
    0 讨论(0)
  • 2021-01-02 09:29
    bool MyApplication::ReportGenerator::GenerateReport(){
        if ( ! isAdmin         () ) return false ;
        if ( ! isConditionOne  () ) return false ;
        if ( ! isConditionTwo  () ) return false ;
        if ( ! isConditionThree() ) return false ;
        return generateReport() ;
    }
    
    0 讨论(0)
提交回复
热议问题