Saturday, August 30, 2014

Things I wished I knew while doing my tech bachelor / undergraduate (Slides and video)

9 comments :
Last week when I was in Kathmandu, Nepal I did a talk at Prime College, the college where I did my bachelor (undergraduate) and my Plus 2. The talk was about "Things I wished I knew while doing my bachelor / undergraduate".


This talk was about how to use your bachelor to craft your tech career. I started with what I have done and some examples of where my friends have reached. Then I focused on things to consider for selecting and doing projects for technical subjects in bachelor study.

After that I opened up the secrets of selecting a company and doing internship. To concluded I gave some tips on approaching the job market. I think the talk went good and I was able to convey my message correctly to all the participants. I hope the students will benefit from it.


Attentive Students

Thankfully the bachelor students who attended the talk were very attentive and inquisitive towards what I was presenting. I was expecting more students from Bachelor of Information Management (BIM) but the majority were from Bsc. CSIT 6th semester. I am happy and thankful to the attendees who gave their time to listening to what I had to say.


The Slides

Below are the slides of the talk:

And the video

The video of the talk is here, it has the full presentation and some questions:

Conclusion

Overall the weeks long preparation of the slides came out very fruitful. I am thankful to the college management and Robert Shrestha for his co-operation and help. I hope we can do other events or workshop like these at the college.
Read More

Friday, August 29, 2014

Basic overview of Message queues - Rabbit Mq with Symfony 2 (Slides and Talk video)

3 comments :
Exactly a week ago, I was in Kathmandu, Nepal my hometown where I attended the 12th edition of PHP Developers Meetup organized by PHP Developers Nepal at Leapfrog Technology, Hattiban, Lalitpur.


The talk on Message Queue (Rabbit MQ and Symfony 2)

Not only did I attend the event I also presented a talk on "Message Queues - A basic overview" where I talk about how we are using Rabbit Mq at Namshi. There was no code examples, it was a plain experience sharing of how we use Rabbit Mq with Symfony 2 with the RabbitMq Budle. There was a good overview of how and where we use Rabbit Mq for scalability and reliability.



I did my talk at the end there were 2 other talks before me one from Ujjwal Ojha about using "Imagine for Image manipulation" and another one by Shritesh Bhattarai about "RESTful Web Architecture". Both of you guys you did a great job, I was amazing to see young guys take center stage when the older gurus were listening to you guys carefully.


You can check the album on facebook for more pictures and some more in this post.

The Slides

You can check the slides below:

And the video

The video of the talk is here as well (excuse us for some of the Nepali words and I could not capture the last slide and the questions but the 30 mins video below should be informative enough):

The discussion

We had some discussion on how the talks were and took some feedback on what should we do next as a community with some members from Leapfrog technology and Roshan Bhattarai had some points to clarify.


Group Photo

Thanks to everyone who attended the event, finally there were some discussion on how to take the PHP developers community forward and we snapped a group photo to wrap up the event:


Conclusion

I am really happy to know that there is a emerging PHP community in Kathamndu Nepal which has ~3900 members (where ~40 come for a program also because some people went to attend the world record largest human flag on the same day.) and some other things:
  • PHP developers in Nepal are aware of the new technologies and tools like Vagrant, PHP 5.5 stuff etc.
  • There is still a lot of room for growth and development with active sharing of knowledge between developers.
  • We should target to conduct a International level PHP/Dev/Code conference in Kathmandu.
Kudos to the organizers, keep these meetups coming. 
Read More

Monday, July 28, 2014

4 Git tips beyond the basics

6 comments :
What is common between Google, Facebook, Microsoft, Twitter and Linked in? If you are looking for the answer its Git the Distributed Version Control System (DVCS) that has simplified software revision control. Its major features are speed and performance combined with ease of use provided you know the basic git concepts.

If you are new to git you should read some getting started tutorials or if you have used SVN you should read migrating to git. For the audience of this post, I assume that you are already using git for some time and are familiar with concepts like commit, push a branch, pull changes from remote repository, merging a branch to "master" and similar daily things that git users normally do.

Git is a lot more popular than SVN or Mercurial. If you take check Google trends in the past 5 years (July 2009 - July 2014) in Internet and Telecom category, git it twice or move as popular as the other two:

More on Git

I started using git in early 2009, then I has little experience with SubVersion (SVN) and git was a refreshing change to slow and difficult SVN. At that time, we did not do much branching or follow the git flow branching model.

I rediscover the power of Git in 2012 when I joined Namshi where we were a bigger team than I used to work with and Git made total sense. We were also using a revised git flow with lots of branching, merging, rebasing and tagging for releases.

Recently I watched a video "Git happens" by @Jessitron and I refreshed the git concepts. This video is great not only that she is talking about git, also because it's not a presentation she uses a whiteboard, markers and some sticky notes to make all the most important git concepts very clear to you. If you are using git I really recommend watching this:



And remember when using Git, its about telling the story of your project, how you and your team decide it will help maintain and informative history of your project. 

The Tips

Some Git tips beyond the basics (commit, pull, push) are as follows:

Diff (what changed?)

So you are working on a new feature on your branch my-feature, before you commit, you do a git status to list your files, then you need to know what changed in the files you should do git diff on your working directory. It will see all the changes you have made.

ᐅ git diff 

diff --git a/Dockerfile b/Dockerfile
index 392e7c0..a0a7c9f 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -48,6 +48,13 @@ ADD build/sites-enabled/dashboard       /etc/nginx/sites-enabled/dashboard
 ADD build/sites-enabled/test.local    /etc/nginx/sites-available/test.local
 ADD build/sites-enabled/test.local    /etc/nginx/sites-enabled/test.local
 
+ADD build/sites-enabled/drupal7.local    /etc/nginx/sites-available/drupal7.local
+ADD build/sites-enabled/drupal7.local    /etc/nginx/sites-enabled/drupal7.local
+
+ADD build/sites-enabled/drupal8.local    /etc/nginx/sites-available/drupal8.local
+ADD build/sites-enabled/drupal8.local    /etc/nginx/sites-enabled/drupal8.local
+
+#phpmyadmin
 ADD build/sites-enabled/phpmyadmin    /etc/nginx/sites-available/phpmyadmin
 ADD build/sites-enabled/phpmyadmin    /etc/nginx/sites-enabled/phpmyadmin
 
diff --git a/readme.md b/readme.md
index 7ddea6c..4188d66 100644
--- a/readme.md
+++ b/readme.md
@@ -1,6 +1,6 @@
 # Docker: Ubuntu, Nginx and PHP Stack
 
-This is the basis for LEMP stack (minus MySQL). This is based on [phusion/baseimage-docker](https://github.com/phusion/baseimage-docker) base Ubuntu image, which takes care of system issues which Docker's base Ubuntu image does not take care of, such as watching processes, logrotate, ssh server, cron and syslog-ng.
+This is the basis for LEMP stack (without MySQL). This is based on [phusion/baseimage-docker](https://github.com/phusion/baseimage-docker) base Ubuntu image, which takes care of system issues which Docker's base Ubuntu image does not take care of, such as watching processes, logrotate, ssh server, cron and syslog-ng.

You can also use git diff if you have already added the files to the staging area with git add, you will need to use git diff --cached. Diff can be used to compare branches and commits for example:

✹✭ ᐅ 
git diff master...my-feature

diff --git a/Dockerfile b/Dockerfile
index 9f9cd77..392e7c0 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -6,34 +6,115 @@ RUN /etc/my_init.d/00_regen_ssh_host_keys.sh
 
 CMD ["/sbin/my_init"]
 
+# install certificates
+ADD build/certificates/server.crt /etc/nginx/certificates/
+ADD build/certificates/server.key /etc/nginx/certificates/
+
 # Nginx-PHP Installation
 RUN apt-get update
 RUN apt-get install -y vim curl wget build-essential python-software-properties
 RUN add-apt-repository -y ppa:ondrej/php5
 RUN add-apt-repository -y ppa:nginx/stable
+
 RUN apt-get update
+RUN apt-get install -y python-software-properties python python-setuptools ruby rubygems

It can be used on github as well, with a URL like: https://github.com/geshan/angular-presentation/compare/master...more-intro - so this is comparing master with the more-info branch. The same can be done with commit hashes.

Global Git Ignore

If you use an IDE and it creates folders like .settings or .idea and you want to ignore it globally on all git repositories you can do it with global git ignore rather than adding it to each .gitignore file in each repository. You can create a file say global_gitignore on your home directory and list the folders and files you want to be ignored in all git repositories like:
#global git ignore
.idea
.settings

/test.txt

Then run the following command you will have the above git ignore rules applied to all the git repositories:
git config --global core.excludesfile ~/.gitignore_global

There are a lot of other git configurations you can try and tune git to meet your needs, if you want to look at the current configs applied to your git globally do cat .gitconfig on your home directory.

Stash your work

You are working on feature-a, an urgent bug say bug-x is discovered and you need to leave what you were doing for feature-a and fix bug-x. What do you do with the changed files in branch feature-a that you were working on, you don't wan't to commit or push it as its a work in progress. Here comes git stash to your rescue, you can just stash what you were doing on branch feature-a and finish work of bug-x then come back to feature-a branch and pop or apply your stash. How?

✹ ᐅ cd my-project
✹ ᐅ git checkout -b feature-a

# working on feature-a, some files changed say 3 of them
# bug-x discovered, need to fix it urgently so let's stash changes
# (save them for later)
✹ ᐅ git stash save feature-a

# lets go to master and create a new branch to fix bug-x
✹ ᐅ git checkout master
✹ ᐅ git checkout -b bug-x

# work to fix bug x for 1 hr, its fixed now.
✹ ᐅ git add .
✹ ᐅ git commit -m 'bug-x fixed with change in config files'
✹ ᐅ git push origin bug-x

# then open a pull request on github for bug-x,
# and come back to working on feature-a

✹ ᐅ git checkout feature-a
✹ ᐅ git stash pop
# all your changed files are back and you are at the stage
# where you left it before fixing bug-x

Stash pop will remove the last stash, you can do  git stash list to check all your stashes, stashes can be saved without name and can be applied with the hash. Check the stashing docs for more information, generally I follow the above flow for staging and prefer not having any stashes in the list.

Squash your commits

You did this big feature taking 4 days work and as a good git user you did commits when the code was stable. So now you have 10 commits in total for the feature-b you worked in for 4 days. Do you really want to send a Pull Request with 10 commits, may be along the way you wrote some commits messages that are not so relevant now. So how do you get the 4 commits to become 1, its easy you squash your commits using git reabse -i, how?

Now you have 4 commits (shown using gitk ) on top of master:


you need to execute the following command:

✹ ᐅ git rebase -i HEAD~4 #the last number, here 4 is the no. of commits you want to squash.

# you will get a screen like this :

pick 8dd81e6 feature-b changes to the config files
pick 7ea892d Feature-a fixed class A b and C
pick a465749 Feature-b fixed tests
pick cd6f406 Feature-b fixed tests


# change all picks except of the first one to s. 
# s is short for squash like below:
pick 8dd81e6 feature-b changes to the config files
s 7ea892d Feature-a fixed class A b and C
s a465749 Feature-b fixed tests
s cd6f406 Feature-b fixed tests

# now exit your editor, if you are using vim :wq
# then as you are squashing commits you can select which commit messages to pick
# from a screen like:
# This is a combination of 4 commits.
# The first commit's message is:
feature-b changes to the config files

# This is the 2nd commit message:

Feature-a fixed class A b and C

- [x] Works for A and B

# This is the 3rd commit message:

Feature-b fixed tests

* fixed most test

# This is the 4th commit message:

Feature-b fixed tests

- [x] fixed all tests


# Pick the ones that you need or delete, for this example I will not delete anything
# and quit the editor

# 4 of your commits are squashed into 1.

If your commit squash was successful, you will see only 1 commit in place of the 4 on gitk with all commit messages intact, like:

Conclusion

This is just scratching the surface of git there are lots of other things that can be done with git, like cherry pick commits, list git branches sorted by created date, bisect for debugging, you have to know how to fix conflicts with tools like meld etc.

Then you have git hooks which open up a new world of its own like having pre-commit hooks to do checks of code and run tests. Git has also been used to version control text related projects like books not only code. So the possibilities are endless and if you are not using Git you are missing on a lot of things.
Read More

Friday, July 18, 2014

3 Bundles to get started with REST in Symfony 2 and some tips

4 comments :
"I found out that you guys just build an amazing mobile app for your e-commerce venture, I heard you are using Symfony 2 for your back-end APIs. How did you make it that fast?" This is not very different that what I was asked some months back. The answer is we use a Service Oriented Architecture (SOA) where all back-end service follow the REST architecture to communicate with all the clients. The client can be built in any language as longs a they can do HTTP calls. Lets look at what Symfony 2 bundles you can use to build a similar scalable, fast and cacheable REST APIs.

I assume that you have some experience with Symfony 2 including how to use composer to download dependent libraries/bundles.

What is REST

REST, short form for Representational state transfer is a resource based client and server communication protocol which is stateless and cacheable. It is an abstraction over HTTP communication where emphasis is given on uniform interface to make the communication structured and consistent. This video describes the constraints of REST and explains what it is or read a simpler one.

REST is a lightweight substitute to older ways of doing web services like Remote Procedure Call (RPC) and Simple Object Access Protocol (SOAP). Basically communication in REST happens in light weight format like JSON which makes it fast.

Describing REST involves having knowledge if the HTTP methods (verbs) like GET, POST, PUT, DELETE. Before proceeding further knowing what are they used for will be essential. There is also lots of fuss about how to name resources (and corresponding URIs) but rather than doing things by the book making it logical and practical can be the best solution.

RESTing With Symfony 2

Symfony is a very loosely coupled framework which has lots of useful components structured in easily plug-able bundles. Here are 3 useful public Symfony 2 bundles for your next project using REST with Symfony.

FOSRestBundle

The FOSRestBundle provides a solid foundation to start building RESTful services in Symfony 2. It can provide with automated routing for resources (probably doctrine entity if you use doctrine). It provides a very good base for a format (JSON/HTML/XML) independent view layer. 

If you want to be able to encode or decode multiple format FOSRestBundle recommends using JMSSerializerBundle. The serializer bundle is like glove on the hand with FOSRestBundle to give out response in JSON/HTML/XML. Please go through the documentation very well and for custom routing you can use Symfony routing and not the one provide by FOSRestBundle. It is one of the most popular bundles in KNPBundles and has been downloaded more than 784K times.

NelmioApiDocBundle

NelmioApiDocBundle is a boon when developing REST web services on Symfony2. The bundle enables clean documentation of all API end points by just using some structured annotation. There are at least 2 amazing benefits using this bundle, first you can give the link to the docs (generally /api/doc) to anyone who wants to implement the REST API you have created. Second while developing you can test your API with the sandbox feature provided by this useful bundle, if you use the annotations correctly linking the involved forms.

Restricting sandbox with user access might be a good feature to add on on this bundle. Reading the full documentation is highly encouraged. It is also a very popular bundle with 473K downloads till date.

LexikJWTAuthenticationBundle

As discussed REST is stateless, so for authentication you have to opt for some mechanism that can be communicate with each request. You can go for something like Oauth or use JSON Web Signature (JWS). There is already a public bundle the LexikJWTAuthenticationBundle using the JOSE library built at Namshi. It is recommended that you read about JWS and JSON Web Token (JWT) before using this bundle. It can be used by putting the token as a cookie for each request.

It will enable role based access to your API end points, lets say you want normal users to create users and only admin users to update/delete users that will be easily possible using this bundle than trying to generate a time limited key or some other type of authentication. Yes, reading the documentation of the bundle will help you achieve the desired output.



I hope these modules will come in handy when you start your REST Api with symfony. For more features and to leverage REST to the fullest you can take a look at BazingaHateoasBundle and TemplatedUriBundle.

Other Tips

Some other things you can consider when building a REST based API with Symfony 2 are:
  1. Use the validation groups in Symfony 2 and keep all the validation conditions as annotation in the Entity itself than at multiple places.
  2. Keep the handing of forms central, though you will not show any HTML forms for the purpose of validation use the Symfony forms on top of the entities and link them to controllers which expose the end points.
  3. Use the HTTP status code wisely like when a resource is created its not 200 OK its 201 Created, when you are paging results 206 will make for sense and if the user sent bad data 400 Bad Request is better.
  4.  Think how will you version your API early when you develop your API if today you use /api/V1 how will you use api/V2 and what will be its consequences.
  5. Last but not he least be liberal on what you accept and strict on what you give out.
All in all Symfony is a great framework and doing REST in Symfony though not a piece of cake but is challenging yet rewarding. 
Read More

Saturday, July 12, 2014

A big thanks to all the programmers, you make the world go round

4 comments :
Call them programmers or developers or software engineers, in this technology centered world, your life is a easier also because of them. If you notice you are addicted to technology in all aspects of your life now a days, you wake up when the alarm app rings on your phone, all day at work you are constantly checking email, after work you connect with your friends on facebook, before sleeping you browse through the news. All these activities are possible because one or more programmers wrote some working code and released the application that you are using on your desktop, laptop, mobile etc.

It is structured words like the one you see below that is changing how we live in the current times.


How do programmers make the world go round?

You just beeped your smart card at the metro/train station entrance, it showed you the current balance n the card on the small screen, how? Someone coded the software for the embedded system to detect the NFC chip and saved your valuable time and effort. There are other countless examples on every nook and corner you go today. You are able to catch up to the fast paced life because someone thought of a software solution to a problem and other people spent hours coding the right solution for it.

When I worked for a Travel reporting system for UNICEF in Kathmandu, Nepal, it was a pleasure to know that after implementing the new system the reimbursement of travel expenses was 40% faster. I think each programmer/developer/software engineer whose project has gone live has one or more similar experiences.

Due respect to other professionals

Surely it is only only about programmers, like doctors save lives, engineers help build houses, pilots fly the plane. If you look at any profession software has changed how things are done now and how it was done some years back. Think of a banker who had to record all data on paper registers, now can simply enter the data to this awesome system and it is reported till the balance sheet. 

I would also like to convey my due respect to all other tech related jobs including systems analysts, project managers, data architects, system administrators they are all very important parts of the puzzle.   

We need more programmers a.k.a super hero world changers :)

I watched the video below some times back and I think people should know that their live is a bit more easy because great ideas were successfully executed by talented programmers.



Computers are everywhere in all fields, so we will be needing more rock stars (programmers) who can transform from ideas on papers to real life applications that can be used by many people.

Programmers are addicted to code

Starting to code is not easy and taking it as a profession does require lots of passion, determination and devotion. It takes lots of learning with hours and hours of thinking and making things work. After you pass the initial phase of how do I make it work then you start getting addicted to coding. It is an amazing feeling that you can build something and you dive deeper into it. It requires lots of problem solving and creativity to get things done and get it done right. I recently read "How I hacked my husband's code addiction" which explains how programmers behave at times.

All in all it is this passion and addition to code which keeps programmers glued to their machines hours on hours to come up with something productive and useful. That mobile app you think is helping you a lot was build by some programmers who had to go through lots of hard work to get the thing working. So respect the devotion, time and effort put in by programmers and other tech professionals.

Programmers deserve a big thank you

Regardless of the language used, the product platform or the audience scope, all programmers deserve a big thank you. Thanks for all the free apps I got to use, for the endless long hours programmers have put to get things work, ripping your hair off to make that test pass and seeing the tests turn green at midnight,  for all the free and open source code that helped all other programmers. Hats off to your hard work. Keep up the good work and making the world a better place.
Read More