My new programming style

81 views
Skip to first unread message

Edward K. Ream

unread,
Apr 20, 2018, 1:49:49 PM4/20/18
to leo-editor
There have been complains, both gentle and otherwise, about Leo's style. I'm not likely to forget them.

A completely new (for me) style has emerged while working on the keys2 branch. Its main features:

1. I have removed almost all trace statements from complex methods. I had no idea that these we having such a harmful effect on the code.  I thank Vitalije for this. It's big.

2. Guided by pylint warning (still disabled) about 'else' statements following a 'return', I have "flattened" the code in several places.  The result is, to my eyes, simpler and clearer.

3. I have refactored k.masterKeyHandler and filter.toBinding using as many helper methods as possible. This has eliminated quite evil section references and drastically clarified the code.
 
4. At the top level of methods, I now use comments formatted like this:

    #
    # My comment
    code

This may seem like a small thing, but it highlights important comments to a surprising degree.  The empty '#' line also makes such comments immune from Leo's beautify command.

Summary

If you doubt the effectiveness of these improvements, just compare the old and new versions of the eventFilter and masterKeyHandler methods and their respective helpers.

I'll transition to the improved style gradually as I encounter code to be revised for other reasons.

Edward

Edward K. Ream

unread,
Apr 21, 2018, 7:15:38 AM4/21/18
to leo-editor
On Friday, April 20, 2018 at 12:49:49 PM UTC-5, Edward K. Ream wrote:

A few comments:


> 1. I have removed almost all trace statements from complex methods. I had no idea that these we having such a harmful effect on the code.  I thank Vitalije for this. It's big.

I foolishly thought it would be too much work to recreate the traces.  In fact, the traces create extra work when reading the code.  It's easy to add temporary traces as needed.

 
> 3. I have refactored k.masterKeyHandler and filter.toBinding using as many helper methods as possible. This has eliminated quite evil section references and drastically clarified the code.

The << compute vars >> section in k.masterKeyHandler added cruft everywhere.  The new code just passes a single event arg to all helpers.

The helpers recreate data as needed. Sure, this adds a few lines of code to the helpers, but imo these lines are good documentation.

Furthermore, all helpers are now self contained.  This would be useful when creating unit tests for individual helpers.  I don't plan to do this, but now it's not completely out of the question.

Edward
Reply all
Reply to author
Forward
0 new messages