May 31, 2015

Interesting Python style



LBYL
Look before you leap. This coding style explicitly tests for pre-conditions before making calls or lookups. This style contrasts with the EAFPapproach and is characterized by the presence of many if statements.
In a multi-threaded environment, the LBYL approach can risk introducing a race condition between “the looking” and “the leaping”. For example, the code, if key in mapping: return mapping[key] can fail if another thread removes key from mapping after the test, but before the lookup. This issue can be solved with locks or by using the EAFP approach.

EAFP
Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.
Use EAFP is the best for Python and programming pleasure :D

May 17, 2015

Experience with RiotJS

RiotJS is really nice. Small and do the job!
https://github.com/muut/riotjs

There are some pitfall.

* To mount other tags (defined somewhere outside), I need to put it inside an event:
this.on('mount', function() { riot.mount('other-tag', ...) }

* If you see "onXYZ" on a rendered tag, it meant the event handler wasn't binded correctly. Most of the time, it might because of unresolved handeler.

I don't know when yourHandler is enough, or I must write parent.yourHandler

<ul each={ items}> <li onclick={ parent.yourHandler } ...
* Remember the scope by set self = this first in the script. It's really helpful!