[mythtv] API suggestions

Henrik (Mauritz) Johnson hpj at underscore.se
Fri Feb 6 12:16:04 EST 2004


Renchi Raju wrote:

>On Fri, 6 Feb 2004, Joseph A. Caputo wrote:
>
>  
>
>>On Friday 06 February 2004 06:26, Henrik (Mauritz) Johnson wrote:
>>    
>>
>>>Hi, a few questions and suggestions regarding the API. These are not
>>>in any particular order and I'll probably find more stuff as I keep
>>>working with it.
>>>
>>>1. The use of QString and const is totally messed up. For instance
>>>why use the "const QString" datatype... ever? The correct usage
>>>should be "const QString &" which will save one extra copy. I know
>>>that QStrings are shared and therefor quickly copied, but that
>>>doesn't make it right. Someone should go through the API and clean
>>>that up. (I volunteer if someone give me CVS access).
>>>      
>>>
>>Just to clarify, the proper usage of QString when calling functions is
>>as follows:
>>
>>-As a value-based argument to a function (read-only):  "const QString &"
>>(you could also pass "QString" or "const QString" by value without
>>incurring too much overhead, since the class is implicitly shared, but
>>if you don't need write access to the argument, then pass-by-reference
>>is preferred)
>>    
>>
>
>you are right. but const in "const QString" is redudant. but then
>again, its a trivial issue.
>
>  
>
>>-As a reference-based argument to a function (caller expects argument to
>>be modified inside the call):  "QString &"
>>(I imagine this is not encouraged)
>>    
>>
>
>its useful sometimes like when you want to do an assigment
>A.text() = "some string";
>but i usually prefer a set/get style.
>
>  
>
>>-As the return value of a function: "QString"
>>Most definitely *never* return a "QString &" or "const QString &" from a
>>function unless you are very sure of what you are doing!
>>    
>>
>
>"const QString& function()" never hurts. in fact i would recommend it over
>just plain  "QString function()"
>  
>
I beg to differ. Take the current example:

const QString &returnString(void)
{
   return "Here we go much to fast. Watch out boy were gonna craash.";
}

This will result in corrupted stack and a crash three function calls 
later and a couple of hours debugging. Still, sometimes it's ok as long 
as the returned string isn't garbage collected on return from the 
function but then were back to "you are very sure of what you are doing".

/Mauritz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mythtv.org/pipermail/mythtv-dev/attachments/20040206/e093bcf2/attachment.html


More information about the mythtv-dev mailing list