On Tue, Jan 7, 2020 at 7:58 AM Edward K. Ream <edream...@gmail.com> wrote:

> While working on leoAst.py, I have come to understand more fully several
> of Vitalije's suggested code patterns:
>
> *Prefer functions to classes/methods*
>

The TokenOrderTraverser class looks to me like a good opportunity to follow
the above principle. AFAICT, the way it is written, that class is ONLY
useful if it is subclassed. For me, subclassing is usually extra friction
and the resulting code is less clear.

If the TokenOrderTraverser.traverse method were written as a function (say
'traverse_in_token_order(tree)') which is exactly the same except in place
of calling self.visit(node), it will 'yield node'. Then if you really think
you still need to have the TokenOrderTraverser class, you could still
implement then the traverse method:

def traverse(self, tree):
    for node in traverse_in_token_order(tree):
        self.visit(node)


IMO, the fstringify code would be clearer if it were not written as a
subclass of TOT.

With the subclass way, this is the minimum I could imagine to print all the
nodes:

class TokenOrderPrinter(TokenOrderTraverser):
    def print(tree):
        self.traverse(tree)

    def visit(node):
        print(str(node))

TokenOrderPrinter().print(tree)


The function/generator way has so much less friction for the developer
using it:

for node in traverse_in_token_order(tree):
    print(str(node))


Brian

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/CAO5X8CzULeNHG5NnfmtqhiBdNz34LWJg4yJOEBOBR_nDx8_7cA%40mail.gmail.com.

Reply via email to