This Week I Learned - 2016 Week 47

Last week post or the whole series.

What an excruciating stressfull week. So many things to follow up and so many things broken, including my own body till I have lost 3kg. On a positive side, when you're down with sickness, your perpestive towards your environment changed, in a slightly turn off way.

On Git. I just realized, unintentionally, my git-fu just increased by 0.5% since last two weeks. When checking for merge through rebasing and merging, it seems the LOCAL and REMOTE branch have been interpreted differently in all merging tools. P4Merge interprets it differently? To summarize it, LOCAL is the originals, REMOTE is the changes you want to add regardless it's a rebasing or merging process.

Now, after you've resolved all the conflict either through rebasing or merging, to visualize and compare your changes (just to be sure), we can use `git diff` command to compare between two ranges.
$ git diff branchX..branchY

Note the double-dot to specific range there. It seems that you can specific either double-dot or triple-dot to specific ranges but indicates different types of output. The Venn diagrams and commit trees below shows the differences.

There is one practive that I do follow when using Git in feature branch, which is to commit early and commit often. However, one of the issue is that the feature branch history is cluttered with many tiny commits. While this is useful when working with others on the same branch (we're aware of what's going on), it's best to squash these commits before merging into `master` branch. There are two ways (with different behaviours) to squash all the commits.

First, is to squash these commits when merging to `master` branch. The source branch, in this case, `branchX` will be throw away.
$ git checkout master
$ git merge --squash branchX
$ git commit

Second, is to sqush these commits when rebasing. This is my prefer method where we still keep the source branch. However, if you have a lot of commits, it's quite slow as you have to assign the rebasing method (either squash or fixup) for each commit. Some Git client tools support this feature to squash commit but I never really explore this.
$ git checkout branchX
$ git rebase -i `git merge-base branchX master`

That about it for this week, more stuff to come in coming weeks as we're approaching the end of they year.

This Week I Learned - 2016 Week 46

Last week post or the whole series.

Busy week. Probably the busiest week ever and I only managed to settle down. Sickness, faulty workstation, and idiocracy all over the places.

Follow up from last week in investigating C++. It seems that `unsigned` is equivalent to `unsigned int`.

On the Perl front. Seriously, Perl's CPAN does have almost everything under the sun. Yes, the damn xxx helper function does exists. When I started my job as a programmer many moons ago, we have this helper debug cum print function called `xxx` in PHP. Besides the benefits of easy to type and identify, the Perl's version does not requires any parenthesis (one of the quirks of Perl), thus a major saving of two key strokes and your pinky as well.

C++ - Differences Between `unsigned` and `unsigned int` Data Type

During a discussion last week, I've made a mistake when discussing the `unsigned` data type with an acquaintance (not from programming background). Basically, I've misinformed him that `unsigned` is not a data type and it should be `unsigned int` instead. The truth is, in C++, `unsigned` is a data type, implicitly is an alias to `unsigned int`.

To illustrate this, let's use the below code, through typeid operator to determine the data type at runtime, a C++ mechanism called Real-time Type Information (RTTI).
#include <iostream>
#include <typeinfo>

using namespace std;

int main() {
    unsigned int x = 10;
    unsigned y = 20;
    cout << typeid(x).name() << endl;
    cout << typeid(y).name() << endl;

Compile and run the code. Observe that the output shows two js? Because in GCC, the return name is a decorated name (which have been mangled) and we need to demangle it.
$ g++ -o typeid
$ ./typeid

To demangle the name, use the c++filt program, result as shown below. Now the question here is why the function name was mangled? As C++ supports function overloading, a feature where you can define two or more functions/methods with the same name but different function parameters. Conversion to the assembly code needs unique assembler name for these functions. This is where the process of mangling comes in. The c++filt tool reverses the process to find the exact name.
$ ./typeid | c++filt -t
unsigned int
unsigned int

Lots of new things learned here but most importantly, always double check (google in this sense) for any assumed knowledge before you shared with someone else.

This Week I Learned - 2016 Week 45

Last week post or the whole series.

Slow week. Down with workload at work and home and sick as well.

Lots of C++ related stuff this coming weeks. I was reminded again and again why programming in C++ is not an exciting experiences. The complexity of the whole development environment and the lack of my undertanding of the compilation cycle caused me stuck on simple trivial matters and can't proceed ahead. That's is part of the learning process every one have to go through when picking up something new in such a short notice.

The first concept you need to understand is compilation model which consists of four tools of Preprocessor, Compiler, Assembler, and Linker. The source code goes though these four tools to generate the final executable binary. Coming from a dyanmic-typed programming languages (PHP, Perl, Python, and etc.), the whole process seems quite a hassle. But programs generated by C/C++ is probably the most optimized human readable assembly code. Few things I've picked up.

This Week I Learned - 2016 Week 44

Last week post or the whole series.

Every time I reread Perl in about 2 hours 20 minutes, there is always new insights that increase my understanding of Perl itself. I can finally grok the intricacy of the three data types: scalar, array, and hash, especially the later two. To make it short, don't use list as container for arrays or hashes, just initialize and declare both types as anonymous array(using bracket) or hash (using braces). You can use them through reference or arrow (`->`) operator. Example as shown.
use v5.10;

my $contact = {
    name => 'John Doe',
    mobiles => [
        {carrier => 'at&t', no => '111-222-333'},
        {carrier => 't-mobile', no => '444-555-666'}

say $contact->{mobiles}->[0]->{carrier}; # at&t
say $contact->{mobiles}->[1]->{carrier}; # t-mobile

Another key concept is Perl calls by reference. This means when you pass a variable to a subroutine, it's a reference to the original value, not a copy. Any modification to the variable, will be reflected within the scope or context of the code until the end of the execution. The most important takeaway is that arguments to the subroutine is a list of scalar (yes, the @_ is a list) and the element of @_ is an alias to the passed parameters. According to perlsub documntation (emphasis added),
Therefore, if you called a function with two arguments, those would be stored in $_[0] and $_[1] . The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable). 

While we are on Perl (yes, we still talking about it). What are the preferred ways to check if an element exists in an array? Two most common way. One is using the default grep method, another is using the `List::MoreUtils` method.
# using grep
if (grep { $_ eq $element } @list) {

# using List::MoreUtils
use List::MoreUtils qw(any);
if ( any { $_ eq $element } @list ) {

My migration back to Vim from Sublime Text seems to be progressing quite well. It's nice to expose yourself to another editor and reapply certain features back to your default editor. First, is update tmux tab with Vim's opened file name. This was one of those things that you want to fix it, but never remember to do so. Next, fix copying from clipboard in Windows not working issue. Next, set indentation rules (like tab only) by file type. And lastly, how to open most recent opened files. Something I didn't realize exists in the default settings.

Remember Lorem Ipsum? Which is commonly used as filler text or place holder text in graphic design before the actual content is used. Well, this process is also known as Greeking. In Perl, there is this module, Text::Greeking, which provides such feature. There are others as well, like the usual Text::Lorem. But nothing can compare to the sheer bullshit of Lingua::ManagementSpeak which can generate meaningful sentences but pure bullshit management speak.

"a web browser is a JS interpreter". So true. Maybe I should start looking into all these "ancient" technologies (e.g. Tcl/Tk) instead of chasing latest greatest fad.

Is always a grey area if you choose to do development work in either porn or gambling industry. But I never realize that advertising industry is as shady as well.

Hate to be prompted for password every time you need to commit to Git repository through SSH? Save some typing by caching the credential. There are two ways.

Through SSH.
$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/id_rsa_key
$ ssh-add -l
$ ssh

Through Git.
$ git config --global credential.helper cache