Diaspar blog

Code and Life.

Code Salad...just Add Olive Oil #1

I am not a person that feels a compulsory necessity to write anything that comes into his mind or tweet whatever is happening to him, but I do like to write from time to time. These Code Salad Series are an attempt to sit down each sunday and review again all the interesting things I have discovered during the week, knowledge gained and tips for others. Why salad? well, this “weekly review” seems plain boring and the salad analogy just works. The ingredients for this post are: PHP, Laravel, Patterns, Javascript, AngularJs, IDE’s, Phpstorm, Stripe and Math.

We, at Jobsity and Dom&Tom, love Laravel. Its a great PHP framework with solid conventions and without all the complexity of Symfony2. We mainly use it to create APIs for the mobile applications we develop. In my search to reuse framework ideas and avoid DRY code, I found this article http://daylerees.com/codebright/eloquent-collections, so basically On laravel, when items are returned from the database, they are returned as a collection and you can use different built-in methods for dealing with the results. I really love map and filtering. They are powerful, because you can apply any code on every item and then filter the items using a closure. Pretty modern an elegant. This is a simple map example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

// Apply a text to every title on the collection
// app/routes.php

Route::get('/', function()
{
    $collection = Posts::all();

    $new = $collection->map(function($post)
    {
        return 'This is the title: '.$post->title;
    });
});

Martin Fowler, has been talking for years about Programming Patterns and best practices. He recently published an article to describe the Collection Pipeline Pattern.

Collection pipelines are a programming pattern where you organize some computation as a sequence of operations which compose by taking a collection as output of one operation and feeding it into the next. (Common operations are filter, map, and reduce.) This pattern is common in functional programming, and also in object-oriented languages which have lambdas.

Martin Fowler
1
2
3
4
some_articles
  .select{|a| a.tags.include?(:nosql)}
  .sort_by{|a| a.words}
  .take(3)

So basically, as I understand it, in the future, Laravel collection methods could use this pattern and allow chained methods. That would bring lots of code clarity and better error handling.

Now that I mentioned functional programming and lambdas, We had a very big discussion on the programming community all week, about math and if it’s something developers should master or not. I DO NOT THINK a strong math background will transform you into a great programmer, but we have to be honest and there are a lot of cases where Math will help you find efficient solutions. This interesting article is an introduction to lambda calculus, which is the base for the great functional programming ideas.

If we use PHP as our main programming language, we should learn more about its internals. There is now an online book about PHP internals that everyone on the community should check (even if we are not creating extensions).

Besides best practices and code reuse, another obsession is to use the best tools available to code. After one month after switching from Sublime 2, I confirm that I finally leave it for PHPStorm. PHPStorm may not be as fast as sublime or VIM, but its surprisingly fast. Some advantages:

  • You can configure practically everything. I have my sublime commands CMD-R and CMD-O and CMD-P tied to the corresponding functionality in PHPStorm. For controlling freaks this is really awesome.
  • Code completion and live templates are amazing when you work with Laravel and Symfony2.
  • The ability to select a function/class and with CMD-B jump to the original reference.
  • Xdebug support
  • Multiple code inspections and quality tools.
  • Mysql and GIT integration.
  • Powerful search tools

To learn more about this IDE, I followed the excellent Laracast videos about PHPStorm. Some of the videos are free like this one:

https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/2

This is how I finally configured PHPStorm:

neat ah?:

We began a new project for a new client this week. The requirement is to use PhoneGap to create an Hybrid app that can run on Iphone and Android. Phonegap is not the ugly project it used to be several years ago. Phonegap and Cordova now provide a great experience to build and debug.

Also things like AngularJs have brought a great experience an modularity to JS codebases. I checked the Angularjs 2014 conference videos and I found this interesting speech about Angular and Phonegap working together:

We had analyzed OnsenUI, which is an HTML5 framework tightly integrated with angular, but after watching the video and listening about the Ionic Framework, I tested and I loved it. Its really fast, with great UI components, good documentation and examples, and it also works out of the box with AngularJs. This is how it looks on the IOS simulator:

The only thing I did not like, was the code structure. So, based on the following links, we created an structure that works with Grunt and uses the modularity of the NGBP boilerplate:

Finally, another project where I am involved, is dealing with donations. The whole escrow system is very delicate to manage, so we are evaluating other options, and we heard about Stripe connect, which is an interesting alternative where Us, only charge a commission, and contribuitors and final users can interact directly.

This project based on Guzzle, is really nice and allows you to access the Stripe API in a very OOP way. It uses guzzle service descriptions (awesome!).

I say good bye for now, have a great hack time this week!

Flying is learning how to throw yourself at the ground and miss.

Douglas Adams The Hichhikers Guide to the Galaxy