How An Expert And Beginner Code Fizz Buzz in Go

This complex solution for Fizz Buzz in Go caught my attention. Decided to write a simpler version for comparison. Still trying my to grok the former code which uses Go's channel to implement concurrency. It has been a while since I last read Go's code, let alone such complex one (for me). Still a lot to catch up.

On a related note, Fizz buzz, which I always thought the typical common way to filter programmer who can or can't code, is actually a children game that teach them about division.

Replacing MySQL with MongoDB ?!

Within this month, four persons have told about migrating to MongoDB from MySQL. When asked why they need to migrate ? Only one person justified it correctly. They need to store a lot of schemaless documents while the other three just use it for the sake of using it. In other words, chasing fads.

Disclaimer time. I have installed and tried MongoDB locally but never use it thoroughly in any live production system. Hence, I have limited experience using it.

But after reading all these comments, it does make you wonder about the the stability of the system. Judging by their heavy marketing push which reminded me of MySQL when they first started out. It seems MongoDB is market ready but "almost" production ready. Instead of totally replacing your MySQL database. Why not look into HandlerSocket, a NoSQL plugin for MySQL.

MongoDB has become the latest buzzword, especially around local .my tech scene. Web scale FTW !

PHP 5.4 and MySQL 5.5 in CentOS 6.4

Due to some shenanigans in office, unfortunate miscommunication, and effing messed up provisioning, have to redo the LAMP installation again in CentOS 6.4. Installation steps as root shown below.

1. Install the Remi third party repository.
$ rpm -ivh

2. Enable the Remi Repository and update.
$ vim /etc/yum.repos.d/remi.repo
$ yum update

3. Install the Apache Web Server, Mysql Database Server, and PHP. Configure and set the MySQL's password as well.
$ yum install httpd php php-mysql mysql-server
$ mysql_secure_installation

4. Enable both web and database server start upon reboot.
$ chkconfig httpd on
$ chkconfig mysql on
$ reboot

On Object-Relationanl Mapping (ORM)

"ORM's are for basic CRUD crap.SQL is for queries that matter. My ORM usage is almost entirely limited to: get this record, change these values, save it. Anything involving complex joins/ unions gets thrown to raw SQL. Better yet, stored functions on the DB side. Postgres is awesome for this."-- fein, emphasis added
This is even so true if you're inheriting old legacy system with one of the most common database table design issues, table with many columns. Not like 10-plus columns but more like 50-plus to 100-plus columns per table. What worse, new columns were still added to these tables.

Why so many columns per table? Three reasons, lack of database normalization knowledge, weak in writing SQL join or support was poor for the chosen database system, and you're optimizing for convenient.
The last reason is the root cause of this design error.

Let me elaborate this. If you're pressing for time, you will start with a simple table design for any CRUD operations. Writing SQL queries were simple and straight forward without any speed penalty. Or you may start with proper database normalized design but with extra SQL queries and speed penalty due to weak SQL JOIN support (I am looking at you, MySQL). Hence, which one would you prefer? Off course the former choice.

To be fair, under certain circumstances, big table with many columns is the preferred design choice. This is even truer if referential integrity was not needed and you want to denormalize the table design to optimize for reading speed. In other word, you don't mind redundant data and trading speed for storage size.

For those tables with lots of columns, ORM really helps a lot during refactoring. We can slowly remove all those raw CRUD SQL without making any changes to database schema. Hence, application code was shorter, readable, and more maintainable. Is always harder and more dangerous to refactor database schema where you haven't really grok the business rules involved.

Based on experiences, setting relationship between tables using an ORM was painful and troublesome. Most ORM libraries follows a certain rigid rules of connection the relationship. Suitable for new database schema but not legacy database schema which first, does not follows standard convention, and second, contains more complex many-to-many relationships.

In summary, use ORM for refactoring legacy system, but only for simple CRUD operations. But use raw SQL for more complex SQL query like JOINs or multiple rows update or deletion.

Free and Clean Up Hard Disc Space in Your Ubuntu System

My lappy seemed overly sluggish. Upon checking the disk space (shown below), some clean up or packages removal was seriously needed.
$ df -h
Filesystem   Size  Used Avail Use% Mounted on
/dev/sda5     232G  211G  9.8G  96% /

1. Clean the downloaded debian package files.
$ sudo apt-get clean

2. Using deborphan to remove unused orphaned packages.
$ sudo apt-get remove --purge $(deborphan)

3. Clear Google Chrome cache files.
$ cd ~/.cache/google-chrome
$ du -h .
32M     ./Default/Media Cache
274M    ./Default/Cache
305M    ./Default
305M    .

$ rm -rf Cache/* Media\ Cache/*

4. Remove unused packages using software-center, unfortunately, there was some issue with SSO authentication. Fixed it by reinstalling oauthlib package.
$ sudo pip install --upgrade oauthlib
$ sudo software-center

Ubuntu Laptop Power Management Using TLP

If you remember in my previous post, my Thinkpad Edge E420 does not support battery charge thresholds. Although disappointing, I stumbled upon this console tool, TLP, which help to optimize your laptop battery life through toggling on and off different devices such as bluetooth, wifi, and others.

1. Installation is pretty much very straight forward, which is also one of the main reason I like to use Ubuntu.
$ sudo add-apt-repository ppa:linrunner/tlp
$ sudo apt-get update
$ sudo apt-get install tlp tlp-rdw

2. Start the program immediately and see the battery status.
$ sudo tlp start
$ sudo tlp-stat -b

3. Applying either in battery or ac power mode.
$ sudo tlp bat
TLP started in battery mode.

$ sudo tlp ac
TLP started in ac mode.

REPL for PHP - Part 1 : Built-in Interative Shell

A Read-eval-print loop (REPL) is a simple and quick programming environment for testing short code snippet or debugging. In PHP, there are five REPLs namely PHP's native interactive shell, Boris, Tiny PHP REPL, PHPSH, and Wigwam. Let's go through each one of them.

First, let's start with the default built-in REPL environment which was available since PHP 5.1. For interactive mode (you can scroll back each line of code through history logging in ~/.php_history), just make sure the readline extension is enabled.
$ php -m | grep readline

$ php -a
Interactive shell
php >

One of the nicest feature is auto-completion (just type the tab key), extremely useful if you can't remember the exact functions or classes name.
php > print easter_da
easter_days  easter_date

Another useful feature is setting your viewing pager. Useful when you're expecting long result that spans multiple screens. Unfortunately, this setting is only available from PHP 5.4 onwards.
php >   .pager=less
php > phpinfo();

Unfortunately, there are two limitations. First, there is no syntax highlighting support thus making reading the code or result a bit difficulty. Second, you have to manually print out result of every evaluated expression, which is repetitious and troublesome.
php > print_r(new DateTime());
DateTime Object
    [date] => 2013-10-13 01:49:44
    [timezone_type] => 3
    [timezone] => Asia/Kuala_Lumpur

Changing Soft Link Ownership

Soft link [1] is a special file that points to another file or folder, as in, an alias to another file or folder. Let's illustrate this in Ubuntu 13.04.

1. Go to the /tmp folder and create a sample dummy file called a.
$ cd /tmp
$ touch a
$ ls -l a
-rw-rw-r-- 1 kianmeng kianmeng 0 Oct 12 12:37 a

/tmp is the global temporary folder in most Unix or GNU/Linux. As the name implied, most applications use this folder to store temporary files and these files are removed after each reboot. The touch command, which is supposed to update the timestamp of a file can be used to create a empty file quickly.

2. Great. Now, let's create a soft link using the ln command, call b, to the file a we've just created.
$ ln -s a b                                     
$ ls -l a b
-rw-rw-r-- 1 kianmeng kianmeng 0 Oct 17 02:12 a
lrwxrwxrwx 1 kianmeng kianmeng 1 Oct 17 02:12 b -> a

I always have a hard time remember the exact order of the link command especially when creating a soft link. The best way is to
$ ln -s filename linkname

3. To change the ownership of the soft link, we will use the chown command. Type these commands at the console as shown below.
$ sudo chown root.root b
chown: cannot dereference ‘b’: Permission denied

4. Solve this using the -h option which only affect the soft link instead of the actual file.
$ sudo chown root.root -h b
$ ls -l a b
-rw-rw-r-- 1 kianmeng kianmeng 0 Oct 17 02:12 a
lrwxrwxrwx 1 root  root  1 Oct 17 02:12 b -> a