Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Other than saving some lines of code, does approach 2 have any merits in terms of performance and/or code space?

Approach 1
HRESULT FinalConstruct()
{
    HRESULT hr;
    hr = CoCreateInstance(CLSID_C1, ...);
    if(FAILED(hr))
    {
        return hr;
    }
    hr = CoCreateInstance(CLSID_C2, ...);
    if(FAILED(hr))
    {
        return hr;
    }		
    return S_OK;
}



Approach 2
HRESULT FinalConstruct()
{
    HRESULT hr;
    hr = CoCreateInstance(CLSID_C1, ...);
    if(SUCCEEDED(hr))
    {
        hr = CoCreateInstance(CLSID_C2, ...);
    }
    return hr;
}
Posted

1 solution

If you want to save lines, why do you declare hr seperate to giving it a value ?

The second one obviously does less checks and so is faster. But you'd never notice the difference, or even be able to measure it, so it's not worth worrying about. Write what seems the most legible to you, readable code is more maintainable and that matters more than saving the odd clock cycle.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900