Question about calling a method with a default parameter

Lubos Lunak l.lunak at suse.cz
Mon Jul 9 04:44:39 PDT 2012


On Sunday 08 of July 2012, Christophe JAILLET wrote:
> Hi,
>
> Sometimes some methods are declared with some default value(s)
> As an example, here is the definition of matchAsciiL for OUString
>
>      sal_Bool matchAsciiL( const sal_Char* asciiStr, sal_Int32
> asciiStrLength, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
>
>
>
> When these functions are called, the optional parameters are sometimes
> passed with the default values.
> As an example, at line 60 of
> /framework/source/lomenubar/MenuItemStatusListener.cxx, we can find :
>
>      if ((Event.State >>= oULabel) &&
>          !oULabel.matchAsciiL ("private:", 8, 0) &&
>          !oULabel.matchAsciiL (".uno:", 5, 0)    &&
>          !oULabel.matchAsciiL ("slot:", 5, 0)    &&
>          !oULabel.matchAsciiL ("service:", 8, 0) &&
>          !oULabel.matchAsciiL (".cmd:", 5, 0)    &&
>          !oULabel.matchAsciiL ("macro:///", 5, 0))
>
>
> Would it be interesting to lightweight the code by removing these
> useless parameters ?

 In general yes, but in this specific case this it would probably rather make 
things worse, because changing

 oULabel.match("private:", 0)

 to

 oULabel.match("private:")

 gives the wrong idea about what it does. The latter looks a lot like

 oULabel == "private:"

 which is not what it is. The first case is also not exactly intuitive, but at 
least the 0 there is a hint.

 IMO the proper fix would be to remove the default value (which we can't do 
until 4.0 because that'd break backwards compatibility) and introduce 
OUString::startsWith() (which would complement the already existing 
endsWith() ).

-- 
 Lubos Lunak
 l.lunak at suse.cz


More information about the LibreOffice mailing list