] > Re: [Python-projects] branches in function: do branches in sub-functions also count? (Logilab.org)

Email Re: [Python-projects] branches in function: do branches in sub-functions also count?

from
subject
Re: [Python-projects] branches in function: do branches in sub-functions also count?
date
2005/05/11 09:57
On Monday 09 May à 10:01, Sylvain Thénault wrote:
> On Friday 06 May à 11:22, Pierre Hanser wrote:
> > this morning, I tried to refactor some code to lower
> > the number of branches in a function. I separated
> > the body of that function in sub-functions. I was
> > surprised that the number of branches didn't fall.
> > 
> > could you explain how this branch number is counted please?

> Hum, the rule is quite simple: each function/method has its own branch 
> counter which is basically increased on new blocks. For instance:

> def function(arg): # branchs = 0
>     a = subfunction(arg)
>     for i in range(10):
>         a += i # branchs = 1
>     if not a:
>         print 'duh?' # branchs = 2
>     else:
>         print 'ok' # branchs = 3
>     return a
> # => function has 3 branchs

> So spliting a function into subfunctions should make fall its branch
> counter... If you think you've hit a bug, please post a more detailed
> example.

Ok, Pierre sent me a sample of the offending code :

class essai:

    def expandBitmaps(self):

        def bitmapAppendColumn(bitmap, xMin, xMax):
            """add a full column of white pixels to the bitmap"""
            for dummy in range(xMin, xMax):
                for y in range(self.fontSize):
                    bitmap[y] += "0"
            return

        def bitmapBefore():
            """empty columns before bitmap"""
            xCur = 0
            xMax = caract.bbox[2]
            if xMax < 0:
                xMax = 0 # le caractere repart sur la gauche!
            bitmapAppendColumn(bitmap, xCur, xMax)
            return
        # [snip more code below]
 
The point is that the number of branchs for a function includes the
branchs of locally defined subfunctions. As I said it's currently more a
blocks counter than a branch counter. However I agree this is arguable,
so please give your opinion on this if you have one.

-- 
Sylvain Thénault                               LOGILAB, Paris (France).

http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org

_______________________________________________
Python-Projects mailing list
Python-Projects@lists.logilab.org
http://lists.logilab.org/mailman/listinfo/python-projects