Yvan Seth's Hole in the Internet

Further Internetual randomness courtesy of Yvan Seth, software developer.
/Entries/Technology/Code/ <rss>

More Than One Quarter Iffy

Fri 2007-09-14 14:59

Sometimes I'm subjected to code that is a joy to read… alas, such times are very few and very far between. I was deciphering some code today and thought the "signature" of the nesting could be interesting, I'm sure there must be tools that give some pointless metric on such things (It should be a web tool/competition: "Congratulations! Your code has a nesting factor of one million! You'll receive your DIY-Euthanasia kit in the mail shortly!"). Maybe we need an "iffy factor", ratio of ifs (counting if, else if, and else lines) to lines of code (the "simple iffy metric") multiplied by average if statement nest depth (full iffy metric), or something. Anyway, no time to think about it further right now but I thought I'd share the love:

For consistency and ease of processing in this measurement I think the code should be put through a formatter first, such as indent. Then I extracted/anonymised flow control lines:

    indent -kr -l1000 -nut < nestmania.c | \
        grep '^ *\(if\|else\|while\|for\|}\|{\)' | \
        sed 's/[[:alnum:]_]*([^()]*)/()/g'

And saw this:

{
    while () {
        if (() && ()) {
            if () {
                if () {
                } else {
                    if () {
                        if () {
                            if (() == -1) {
                            }
                            if () {
                            }
                        }
                    }
                    if () {
                    if () {
                        if () {
                        }
                    } else {
                    }
                }
            }
        }
        if (() && ()) {
            if () {
                if () {
                } else {
                    if () {
                        if (() == -1) {
                        }
                        if () {
                        }
                    }
                    if () {
                    if () {
                        if () {
                        }
                    } else {
                    }
                }
            }
        }
        if (() && ()) {
            if () {
                if () {
                } else {
                    if () {
                        if () {
                    if () {
                        if () {
                            if (() == -1) {
                            }
                        }
                        if () {
                        }
                    }
                }
                    } else {
                    }
                }
            }
        }
        if (() && ()) {
            if () {
                if () {
                } else {
                    if () {
                    } else {
                    }
                }
            }
        }
        if (() && ()) {
            if () {
                if (() == -1) {
                }
                if () {
                    if () {
                    }
                } else {
                }
            }
        }
        if (() && ()) {
            if () {
                if (() == -1) {
                }
                if () {
                }
            }
        }
    }
}

That's 50 if/else lines (lines with just } aren't counted). The whole function is 191 lines long (again not counting } and also not counting comments). So this function is more than one quarter iffy by the simple iffy metric. For the full iffy metric I'll take the average of the indent depth (counting "indents" not "spaces") of if lines, this is 4.41463, and calculate: (50/191) x 4.41463 = 1.16

So maybe anything over 0.5 needs to be thought through again, and anything over 1.0 needs serious reconsideration. It probably makes more sense to apply to single functions, or maybe even just single top-level if statements.

This is not the most iffy feeling code I've come across.

The entire metric is certainly flawed :) but I'm not going to waste time thinking about it any more, I've got iffy code to get back to!

[[[ BTW: The colouring was done with the suitably horrific:

  indent -kr -l1000 -nut < nestmania.c | \
    grep '^ *\(if\|else\|while\|for\|}\|{\)' | \
    sed 's/[[:alnum:]_]*([^()]*)/()/g' | \
    perl -ne 'if(/^(\s*)([^\s].*)$/){printf "<span style=\"color:#%02x0000\"\>%s%s</span>\n", length($1)*9,$1,$2}'
]]]

[[[ Further ugly:

    indent -kr -l1000 -nut < nestmania.c | \
        grep '^ *\(if\|else\|while\|for\|}\|{\)' | \
        grep 'if\|else' | \
        wc -l

divided by

    indent -kr -l1000 -nut < nestmania.c | \
        grep -v '^[{} ]*$\|^ */\?\*' | \
        wc -l

multiplied by

    indent -kr -l1000 -nut < nestmania.c | \
        grep '^ *\(if\|else\|while\|for\|}\|{\)' | \
        grep 'if\|else' | \
        perl -ne 'if(/^(\s*)if/){$i++; $l += length($1)}END{print $l/$i/4, "\n"}'
]]]

No Responses

Name:
Email: (You must register an email address!)
Url: (optional)
Title: (optional)
Response:
All HTML will be escaped. Paragraphs and new-lines are honoured and you can use *word* for bold and _word_ for underline.
Save my Name, URL, and Email for next time
/Entries/Technology/Code/More Than One Quarter Iffy

© 2005-2009 Yvan Seth — EMail Yvan | XHTML 1.0 Strict | Add to GoogleSubscribe with Bloglines | Creative Commons License

    follow me on Twitter

    Categories

    Badgers

    Protecting your bits. Open Rights Group