Jun 29, 2015

PHP strftime

It took me hours to debug why a template return null, only when using Japanese locale.
Finally I found out.
strftime often used to format nicely date in Japanese, but it will easily broken in production.

There is a workaround to solve this:

function ensureUtf8($text) {
    return iconv(mb_detect_encoding(
        $text, mb_detect_order(), true), "UTF-8", $text    
    );
}


function _strftime($format, $time = null) {
    return ensureUtf8(strftime($format, $time));
}

Jun 19, 2015

New tools

https://github.com/Thibaut/devdocs
I often write code while on commuting train. Local documents are very precious to develop locally without Internet connection.
Days before, I often collect manually by downloading HTML versions (mostly from Readthedocs).
devdocs tool provides much more elegant ways to download and search documents.


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!

Mar 18, 2015

IntelliJ disable clipboard handling

If you use a clipboard manager like ClipMenu, it will be mal-functional in IntelliJ (Idea, PyCharm, ...)
IntelliJ has it own clipboard handling. To disable it, it's tricky.

http://stackoverflow.com/questions/27949064/how-to-disable-android-studio-intellij-idea-clipboard-management-on-osx

To solve this:

- Open *.app/Contents/bin/idea.properties

- Add a line: ide.mac.useNativeClipboard=True

- Enjoy!