Writing Success

"He would usually rise every morning at 5.30 to get his writing done before he -went into the office. 'Three hours a day,' he declared, 'will produce as much as a man ought to write."-- About Anthony Trollope [1] Writing  Success
If you adhere strictly to a daily quota of of doing X, will you become more skillful in accomplishing X? The brute force strategy of getting better at something by going for quantity rather than quality. As they said, fail fast, fail early, and fail often. Where can you find the source of willpower and discipline to become prolific at X?

MySQL Gotcha : Where 1,000,000 = 1.00

As I always said, when come to open source databases, don't use MySQL with default settings for anything involves money. If you're stuck with MySQL and can't switch to a real database system like PostgreSQL , set the default MySQL server sql mode to traditional.

Yesterday, I was hit by the default MySQL very forgiving auto data truncation when inserting data to a table. The bloody database suppose to save one million but store one instead ! Let's illustrate this with a simple example.

1. Create a sample table in your MySQL console.
mysql > CREATE TABLE foo (total DECIMAL(15,2) NULL) COLLATE='utf8_general_ci' ENGINE=InnoDB;

mysql > desc foo;
| Field | Type          | Null | Key | Default | Extra |
| total | decimal(15,2) | YES  |     | NULL    |       |
1 row in set (0.01 sec)

2. Insert 1 million into the sample table.
mysql > INSERT INTO foo values('1,000,000');
Query OK, 1 row affected, 1 warning (0.09 sec)

3. Check what have we inserted. The value of 1 million has became 1.
| total |
|  1.00 |
1 row in set (0.00 sec)

4. What is going on here ? Let's check the default MySQL sql mode for this session. No mode was set, which is the default behaviour.
mysql > SELECT @@SESSION.sql_mode;
|                    |
1 row in set (0.00 sec)

5. To prevent this, we need to change the default MySQL mode so it will behave like a traditional database system by switching to traditional mode.
mysql > SET sql_mode = 'TRADITIONAL';                                                                                                  
Query OK, 0 rows affected (0.00 sec)

6. Try to reinsert 1 million again.
mysql > INSERT INTO foo values('1,000,000');
ERROR 1366 (HY000): Incorrect decimal value: '1,000,000' for column 'total' at row 1

7. To make the traditional as default mode permanently, add this line to the my.cnf configuration file under the [mysqld] section. In Ubuntu 13.04,
$ sudo vi /etc/mysql/my.cnf

sql_mode = TRADITIONAL

$ sudo service mysql restart

Should you switch your MySQL server to traditional mode ? Yes and no. If you're running an existing legacy application, leave it as it, otherwise, it will break the application. But on your local development machine, go ahead as you can catch the bug earlier.

Read all the time.

"Read all the time.  Don’t just do it because you’re curious about something, read actively. Have a point of view before you start a book or article and see if what you think is confirmed or refuted by the author.  If you do that, you will read faster and comprehend more."-- Byron Wien, emphasis added
Important insight on reading a book. My typical mindset before reading a book is what can I learn from this book? I should ask myself these questions before I before I start reading any new books. Examples are:

1. What do you know or understand about X ?
2. How do you feel about X ?
3. Why you want to read about X ?
4. What you want to learn about X ?

Debugging Secure Socket Layer (SSL)

Can't sleep. Cleaning up my old notes. Jotted these down last time while writing a PHP script to make secure connection to remote server.

1. Check your PHP's SSL extension infomation.
$ php -r "echo phpinfo();" | grep SSL

2. Check and test remote server SSL using openssl client.
$ openssl s_client -state -quiet -connect google.com:443

3. Check and test remote server ssl using curl.
$ curl --sslv3 https://www.google.com

CodeIgniter Seeking New Owner

Via Reddit. CodeIgniter , the PHP  framework is currently looking for new owner. Can we safely assume is going to be slowly deprecated and phased out? What's next for any new project done in PHP ? Still pondering between Symfony and Laravel.

Learn X Programming Languages in Y Minutes ?

Is it that easy to learn any new X programming languages in Y minutes ? Maybe. If you already know how to code and the new X share the similar syntax and programming paradigm. For example, is easy for a PHP programmer to pick up Java or vice versa as both are C-based programming language with very similar syntax. Not so with learning Erlang as a PHP programmer. Not only you're moving to a different programming paradigm (functional in this case), the syntax itself is way different and not beginner friendly.

Even if you can code and pick up the syntax fast within Y minutes, you will need many years before you can truly master the language itself. Initially, you can read and understand the code but still can't do anything useful with it. As you progress, you will start to write many small programs as well as experience (unfortunately the hard way) the quirks and gotchas of X. After years of continuous usage, you may (some may not) reach to a phase to write in X idiomatically.

In my case, I am having a tough time (myself to blame) switching to Python from PHP. Experience is a double-edge sword. When you code in Python, you have to rewire you brain to think differently, in a Pythonic way. Imagine the frustration when you felt slow, clumsy, and constantly want to revert to the old ways. Unlearning any habits are never easy.

How to solve this? Stop learning syntax and write small program. Build something, build something relevant to your interest.

Google PageSpeed

"PageSpeed Service is an online service to automatically speed up loading of your web pages. PageSpeed Service fetches content from your servers, rewrites your pages by applying web performance best practices and serves them to end users via Google's servers across the globe."
-- Google's PageSpeed Service FAQ, emphasis added

Now I understand why and how certain major alternative news portal can handle all the traffic during the last general election.

Disable Google Ads in AdBlock

No wonder I kept seeing Google Ads on my search result for the past few weeks. It seemed Google paid Adblock to white list its ads.

For Google Chrome,
1. Go to Settings
2. On the left, select Extensions
3. Find AdBlock
4. Click Options
5. Click Filter Lists tab
6. Uncheck Allow some non-intrusive advertising

Deeply Nested Code

Encountered a deeply nested code (see below) or arrow code. Based on past experiences, you know this is sure going to turn into a maintenance hell as readability is low and modifiability is next to impossible.

Sample simplified pseudocode.
if ( condition a ) {
    if ( condition b ) {
        if ( condition c ) {
            foreach ( ...) {

How we going to solve this ? Flatten the nested code. How ? From the  Refactoring book by Martin Fowler, you can either use Extract Method or Replace Nexted Condition with Guard Clauses.

Sample refactored pseudocode.
if ( ! condition a ) return something_not_a;
if ( ! condition b ) return something_not_b;
if ( ! condition c ) return something_not_c;