Indentation

Heavy indentation does not hurt! No, in fact it is a great help in finding out the control flow quickly. Let us start with a good example this time, Example 3-1.

Example 3-1. Function whocat

        function s = whocat(cat)
        // return all local variables, functions,
        // etc. that are in category cat.

        s = [];
        nl = who('local');

        for i = 1:size(nl, 1)
                execstr( 'typ=type(' + nl(i) + ')' );
                if typ == cat then
                        s = [s; nl(i)];
                end
        end
    

The for loop and the if branch are immediately recognizable. There are blank lines between the logical blocks of the function. They too aid the reader's comprehension of whocat's inner workings.

In longer functions the indentation becomes essential for the orientation of the maintainer. Here is a excerpt of a longer function, that would be terribly hard to understand if not massively indented.

    i = 1;
    j = 1;
    while i <= n1 & j <= n2
            while i <= n1 & j <= n2
                    if ~equ(lst1(i), lst2(j)), break, end
                    i = i + 1;
                    j = j + 1;
            end
            if i >= n1 | j >= n2, break, end

            icurs = i;
            while icurs <= min(n1, i+fuzz) 
                    if equ(lst1(icurs), lst2(j)), break, end
                    icurs = icurs + 1;
            end
            if icurs <= n1 then
                    if equ(lst1(icurs), lst2(j)) then
                            // record element(s) missing from lst1
                            for p = i : icurs-1
                                    this_diff = [lst1(p), string(-p)];
                                    diff = [diff; this_diff];
                            end
                            // re-sync
                            i = icurs;
                    end
            end
            ...
    end // while

The complete listing of this function can be found in Chapter 9.

The last example also shows that we are switching between several style paradigms:

The Golden Rule is that there are no golden rules... This is best known under the term `freedom'.