Coding Standards / Coding Best practices in C++

前端 未结 17 1628
醉话见心
醉话见心 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: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();
    
    }
    

提交回复
热议问题