Issue 123862

Summary: O*String's isEmpty() should be used instead of its older alternatives
Product: General Reporter: hdu <hdu>
Component: codeAssignee: AOO issues mailing list <issues>
Status: ACCEPTED --- QA Contact:
Severity: Normal    
Priority: P4 CC: hdu, issues, j.nitschke, orw
Version: 4.1.0-dev   
Target Milestone: AOO Later   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Latest Confirmation in: 4.0.1
Developer Difficulty: Simple
Attachments:
Description Flags
use O*String isEmpty() in modules starting with a
hdu: review+
use O*String isEmpty() in modules starting with b
hdu: review+
use O*String isEmpty() in module chart2
none
use O*String isEmpty() in module codemaker
none
use O*String isEmpty() in module comphelper
none
use O*String isEmpty() in module configmgr
none
use O*String isEmpty() in module connectivity
none
use O*String isEmpty() in module cppu and cppuhelper
none
use O*String isEmpty() in module cui
none
use O*String isEmpty() in small changes in modules stating with b and c
none
use O*String isEmpty() in module dbaccess none

Description hdu@apache.org 2013-12-16 12:40:42 UTC
In our current codebase there are all kinds of variants for checking whether a string is empty:
    aString == OUString()
    aString.equalsAscii("")
    aString.equals( OUString())
    aString.compareTo( OUString(""))
    aString.compareToAscii( "")
    aString.getLength() == 0
    aString.getLength() < 1
and the related checks to check whether it is not empty:
    aString.getLength() > 0
    aString.getLength() >= 1

The O*String method isEmpty() allows cleaner code and is as fast or faster than all its alternatives, so this method should be used.
Comment 1 j.nitschke 2014-01-31 10:24:35 UTC
Hi, I would like to work on this simple task to get an overview of the various modules.

I plan to use eclipse cdt
search the variants with regexp
check the type in the ide and replace with regexp groups.

first analysis:
regexp for first 5 variants
"(\s*(!|=)=\s*OU?String\s*\(\s*\)|\.(equals(Ascii)?|compareTo(Ascii)?)\s*\(\s*(""|OU?String\s*\((\s*""\s*)?\))?\s*\))"
72 occurrences

last 4 variants:
"(\s*|\.)getLength\s*\(\s*\)\s*(>\s*0|>=\s*1|==\s*0|!=\s*0|<\s*1)"
>0, >=1, !=0
<1, <=0, ==0
3432 occurrences
* findings include build generated files

common types with getLenght() method
most are O*String
Sequence <T> ( has similar method hasElements() )
O*StringBuffer


in most cases it would be a simple replacement
some cases need fix for brackets
if ( a && (b.getLenght() == 0) ) -> if ( a && b.isEmpty() )
few cases where neatly indentations need fixes

How would you like negations formated?
a) !a.isEmpty()
b) ! a.isEmpty()
c) ( !a.isEmpty() )
d) ?

I planned to make one patch file per module, maybe bundle small modules in one

please comment
Comment 2 hdu@apache.org 2014-01-31 17:17:17 UTC
(In reply to j.nitschke from comment #1)
> Hi, I would like to work on this simple task to get an overview of the
> various modules.

Thanks for working on this! Your approach and e.g. the regular expression looks fine. You know what you're doing ;-)

With the many locations and the variations to check for emptiness before the obviously missing method became available an almost automatic conversion with many manual checks is the only reasonable way to go.

> How would you like negations formated?
> a) !a.isEmpty()
> b) ! a.isEmpty()
> c) ( !a.isEmpty() )
> d) ?

I personnaly prefer a.

> I planned to make one patch file per module, maybe bundle small modules in
> one

Sounds good. Have fun!
Comment 3 hdu@apache.org 2014-02-01 12:52:02 UTC
(In reply to hdu@apache.org from comment #2)
> (In reply to j.nitschke from comment #1)
> > How would you like negations formated?
> > a) !a.isEmpty()
> > b) ! a.isEmpty()
> > c) ( !a.isEmpty() )
> > d) ?
> 
> I personnaly prefer a.

To add to this: if the 'a' means something lengthy like
   getFoo()->getBar()->getMore()->getDetails()->getName()
then I actually prefer something like
   a.isEmpty() == false
to keep the logic close together so it is easier to read. Not using the super-long and hardly debugable stuff would be even better, but for this half-automatic fix we shouldn't change such innards.
Comment 4 j.nitschke 2014-02-03 09:21:39 UTC
almost missed the very common implicit casts
in conditions:
if (a.getLength())
a.getLength() ? b : c
while (a.getLength())

and via logical operators !,|| and &&:
a.getLength() && b
a.getLength() || b
!a.getLength()

explicit casts:
(bool)a.getLength()
(sal_Bool)...
(??_Bool)...

it might be easier to generate an expression which excludes all legitimate usages of getLength(), instead of including all variants

but priority is to build expressions which can't break the logic
Comment 5 j.nitschke 2014-02-03 09:42:49 UTC
Created attachment 82479 [details]
use O*String isEmpty() in modules starting with a
Comment 6 j.nitschke 2014-02-03 09:43:51 UTC
Created attachment 82480 [details]
use O*String isEmpty() in modules starting with b
Comment 7 SVN Robot 2014-02-04 08:44:06 UTC
"hdu" committed SVN revision 1564228 into trunk:
#i123862# use O*String's isEmpty() method to check for emptiness in modules s...
Comment 8 SVN Robot 2014-02-04 08:48:47 UTC
"hdu" committed SVN revision 1564230 into trunk:
#i123862# use O*String's isEmpty() method to check for emptiness in modules s...
Comment 9 j.nitschke 2014-02-07 19:32:47 UTC
Created attachment 82535 [details]
use O*String isEmpty() in module chart2
Comment 10 j.nitschke 2014-02-07 19:33:36 UTC
Created attachment 82536 [details]
use O*String isEmpty() in module codemaker
Comment 11 j.nitschke 2014-02-07 19:34:43 UTC
Created attachment 82537 [details]
use O*String isEmpty() in module comphelper
Comment 12 j.nitschke 2014-02-07 19:35:20 UTC
Created attachment 82538 [details]
use O*String isEmpty() in module configmgr
Comment 13 j.nitschke 2014-02-07 19:35:56 UTC
Created attachment 82539 [details]
use O*String isEmpty() in module connectivity
Comment 14 j.nitschke 2014-02-07 19:36:46 UTC
Created attachment 82540 [details]
use O*String isEmpty() in module cppu and cppuhelper
Comment 15 j.nitschke 2014-02-07 19:37:14 UTC
Created attachment 82541 [details]
use O*String isEmpty() in module cui
Comment 16 j.nitschke 2014-02-07 19:38:44 UTC
Created attachment 82542 [details]
use O*String isEmpty() in small changes in modules stating with b and c
Comment 17 j.nitschke 2014-02-09 16:58:53 UTC
Created attachment 82553 [details]
use O*String isEmpty() in module dbaccess
Comment 18 SVN Robot 2014-02-10 09:09:33 UTC
"hdu" committed SVN revision 1566533 into trunk:
#i123862# use O*String's isEmpty() method to check for emptiness in chart2 mo...
Comment 19 SVN Robot 2014-02-12 10:41:05 UTC
"hdu" committed SVN revision 1567593 into trunk:
#i123862# use O*String's isEmpty() method to check for emptiness in the codem...
Comment 20 SVN Robot 2014-02-12 10:42:38 UTC
"hdu" committed SVN revision 1567594 into trunk:
#i123862# use O*String's isEmpty() method to check for emptiness in the comph...
Comment 21 hdu@apache.org 2014-02-14 10:24:53 UTC
@j.nitschke: All patches are good so far, its a wonderful cleanup. Thanks for working on it!

But with AOO 4.1 beta coming soon I'd like to wait with further integrations until the release branch for AOO 4.1 has been split off.

Looking at these masses of emptiness-checks I thought that having another method isNotEmpty() would improve the readability even more.

In the commits so far I added Patch-by attributions as good as possible with SVN. If you want your full name for the Patch-by please provide it.
Comment 22 j.nitschke 2014-02-16 23:09:10 UTC
(In reply to hdu@apache.org from comment #21)
> @j.nitschke: All patches are good so far, its a wonderful cleanup. Thanks
> for working on it!
Welcome

> But with AOO 4.1 beta coming soon I'd like to wait with further integrations
> until the release branch for AOO 4.1 has been split off.
Yes, that's only reasonable. I try to work with great caution, but with the number of changes, errors get more likely.
 
> Looking at these masses of emptiness-checks I thought that having another
> method isNotEmpty() would improve the readability even more.
My current regexp finds about 7500 potential replace candidates. I expect 1/5 false positives, leaves 6000 replacements.
If half of these are checks for not empty, an other method for 3000 calls is a good idea.


btw. is there a standard encoding for the codebase? I use eclipse with standard setting UTF-8 and sometimes the german comments get changed. It seems at least some files use ISO-8859-x encoding. I did a standard checkout and diff with svn command line tool on linux.
I read the patches anyway so it's not a big deal to remove these changes but if you got a solution for this it's welcome.
Comment 23 hdu@apache.org 2014-02-18 13:10:44 UTC
> btw. is there a standard encoding for the codebase?

I personally prefer UTF-8, but as you saw many files are in iso8859-1. And some people don't use UTF-8 enabled dev environments. It s***s but that's how it is. Both encodings overlap in the ASCII space, so if in doubt please transcribe to ASCII.

> I read the patches anyway so it's not a big deal to remove these changes but
> if you got a solution for this it's welcome.

Speaking of patches and their review: I suggest to provide the script that you developed to let it do the bulk of the work. And then we'll do fixups where needed as patches. This makes the review much easier...

Just for the laughs: a colleague just found a new variant of an isEmpty() predecessor: main/svx/source/sidebar/possize/PosSizePropertyPanel.cxx line 1203...