Programming, Vue.js

Thoughts on NativeScript Vue

First Impressions

Having been familiar with HTML and XML layouts building layouts was easy. Styling the layouts feels similar to designing a web app, but not completely the same. Some CSS selectors and HTML attributes aren’t allowed or don’t work the same as you would expect while building a web app. The documentation was mostly helpful, I got hung up on something that wasn’t documented well in my first app. Overall there’s a sense that NativeScript is new and that’s because it is, but I still enjoyed the experience building out the app.

Read more
PHP, Programming

PHP Type Hinting Arrays Using The Splat Operator

It’s possible to type hint an array (sort of) in PHP without using a Doc block.

Version 5.6 added a splat operator or sometimes called argument unpacking. A splat operator is 3 dots before a parameter. The splat operator allows a user to pass in an arbitrary amount of parameters. The arbitrary parameters are then turned into an array by PHP.

For example if we have a method that adds items to a cart and expects each item added to the cart is an instance of CartItem we can do the following:

function addItemsToCart(CartItem ...$cartItems) {
    //$cartItems is an array of CartItem objects

$cartItem1 = new CartItem();
$cartItem2 = new CartItem();
$cartItem3 = new CartItem();

addItemsToCart($cartItem1, $cartItem2, $cartItem3);

The function can now be called using 1 or many CartItem parameters. The end result is $cartItems contains an array of CartItem objects.

Notice how we just pass as many parameters as we want to the function and the splat operator handles the rest. We could pass in 5 or 100 and the result would still be an array of CartItem objects. The splat operator allows someone to pass in an arbitrary amount of parameters to a function turning those parameters into an array.


  • IDEs can identify the type hint which helps auto completion and preventing simple mistakes.
  • Other developers reading the code can quickly identify what type is expected to be passed in.
  • PHP will throw an error if someone tries to pass something else besides a CartItem object.

Cons (maybe)

  • When using splat operator the splat parameter must be last. This is because it allows an arbitrary amount of parameters.
  • Other developers may not be familiar with the splat operator, but a quick look at PHP documentation may help.

Laravel Upgrade Considerations and Tips

Laravel claims estimated upgrade times upgrading version to version, but this estimation may not be true for you. Upgrades take time and vary depending on application size, architecture, and knowledge. If you’re asked for an estimate use these tips and considerations to help determine a more realistic estimation.


Verify other dependencies in project are compatible with the targeted upgrade version. How frustrating would it be to make all the necessary changes only to find out one of your dependencies doesn’t play well with an upgraded version leaving no other option than to revert.

Most issues will occur from packages specifically built for Laravel because they may rely on a particular version. Having to update other dependencies may drastically increase upgrade time. Be sure to read upgrade guides or change log on each of the updated dependencies.

During Upgrade

Unless it prohibits you from moving forward any item that is questionable make note, discuss with your team, and revisit later. Write notes so it’s easy to comeback later. This approach works well because it allows for continuous progress and momentum especially when upgrading multiple Laravel versions.

If you’re not using an integrated development environment (IDE) such as PhpStorm, Aptana, etc then I highly recommend. Most if not all will help with renaming, find and replace, and reduce risk overall when making changes that affect other parts of the app.


The entire application will need to be checked. Ideally the app has a test suite which will help identify areas that may need specific focus. The time commitment here varies depending on application size, who’s doing the quality check, etc.

Depending on changes made during upgrade the deployment process may be slightly different than the norm. Be sure to make notes during upgrade as this will help when the deployment comes around.

Using array_reduce to Transform Data

First, why we might transform data? If we have raw data from a database and need to send data to an external system or maybe export data. In either case we most likely don’t want to expose database column names or the structure returned must be different than how the data is stored.

For our example we’ll be sending data from our database to a CRM. Here’s the example class. In the database we probably store first and last name separately, but the CRM expects that we pass in the full name. Likewise the CRM expects a full address and company name.

class TransformUserForCrm {
    private $columns = [

    public function prepareData($rows)
        $data = [];
        foreach($rows as $row) {
            $data[] = array_reduce($this->columns, function ($result, $column) use ($row) {
                $methodName = 'get' . ucfirst($column);
                $result[$column] = (method_exists($this, $methodName)) ? $this->$methodName($row) : $row->$column;
                return $result;
            }, []);
        return $data;

    private function getFullName ($row) {
        return $row->firstName . ' ' . $row->lastName;

    private function getAddress ($row) {
        return $row->street . ' ' . $row->city . ', ' . $row->state . ' ' . $row->postalCode;

The $columns array defines the output column names.

In prepareData we foreach through each of the $rows.  Each row calls array_reduce. Simply put array_reduce will reduce an array to a single value by way of a callback function. This means we can call array_reduce on each row of data to transform the data into another array with the proper structure and formatting.

Blank array_reduce function with no logic and blank array passed in for initial value.

$data[] = array_reduce($arrayToReduce, function ($result, $valueFromArrayToReduce) {
    //Logic for each iteration goes here
}, []);

array_reduce takes 3 parameters.

  • First parameter ($arrayToReduce) is the array to reduce to a single value.
  • Second parameter is the callback function that is called for each element of $arrayToReduce. The callback itself has two parameters: previous value returned by the callback function ($result) and current iteration value of $arrayToReduce which we have named $valueFromArrayToReduce.
  • Third parameter is the initial value to pass into $result as the previous value because there is no previous value on the first iteration.

The class above implementation of using array_reduce below:

$data[] = array_reduce($this->columns, function ($result, $column) use ($row) {
    $methodName = 'get' . ucfirst($column);
    $result[$column] = (method_exists($this, $methodName)) ? $this->$methodName($row) : $row->$column;
    return $result;
}, []);

The final output structure we want is from $this->columns so that goes into the first parameter.

The callback function carries the result from each previous callback interation unless it’s the first time running through in which case it passes [] because the final parameter of array_reduce is []. Second parameter is the current column we are working on.

In the callback check to see if a method exists on the class. This defines a standard for retrieving data. For example the fullName column will call getFullName method. If a method is not defined then it will assume the value is fine the way it is and puts the raw value in for output.

Each iteration of the callback adds the column to the result by $result[$column]. The result is an array that we keep adding columns to until array_reduce is done. When array_reduce is done the result is returned and added to the $data array. Eventually $data contains all transformed rows.

This method of holding all data in memory doesn’t work well for massive datasets, but works well for small to medium size datasets.


Using the class above given the following data:

$user1 = new stdClass();
$user1->firstName = 'Nick';
$user1->lastName = 'Escobedo';
$user1->street = '123 Fake St.';
$user1->city = 'Chicago';
$user1->state = 'IL';
$user1->postalCode = 12345;
$user1->company = 'Fake Company';

$user2 = new stdClass();
$user2->firstName = 'Will';
$user2->lastName = 'Smith';
$user2->street = '456 Fake St.';
$user2->city = 'Chicago';
$user2->state = 'IL';
$user2->postalCode = 56789;
$user2->company = 'Fake Company';

$data = [$user1, $user2];

$transformer = new TransformUserForCrm();



array (
    array (
        'fullName' => 'Nick Escobedo',
        'company' => 'Fake Company',
        'address' => '123 Fake St. Chicago, IL 12345',
    array (
        'fullName' => 'Will Smith', 
        'company' => 'Fake Company', 
        'address' => '456 Fake St. Chicago, IL 56789'

Selecting Your Next App Dependency

We often turn to open source or proprietary packages for adding functionality to our apps. Here are some key points and questions to ask when selecting your next dependency. This list can help compare multiple packages to determine which fits our needs better. The goal is to find well thought out packages because our apps depend on them.


Documentation is often the first place we look when there are questions. If the package is well documented then developing a solution will be easier.

  • Does documentation exist?
  • Is the documentation up-to-date?


An active community signals to us this package is adopted and being used by other people. Finding whether the community is active should be easy. My go to places are:

  • Stackoverflow (Are people asking questions about this package? Are the questions answered?)
  • GitHub (When was the last time the package was updated? How many issues exist? Do the maintainer(s) respond to issues? How many pull requests? Do the maintainer(s) respond to pull requests? Does the package follow semantic versioning? How many stars?)
  • Package specific forum (Does it exist? Is the forum active?)
  • Google

If the community is actively helping other people it’s a good sign. The chances of getting help with an active community are higher than if there was little to no community.


Often times the package won’t meet our requirements 100% so we must add additional functionality.

  • How easy is it to build a module?
  • Are there hooks or events to add additional functionality without modifying core code?

Code Coverage

Well tested code can lead to less defects. Depending on a package that isn’t tested is risky.

  • Does the code base have tests?
  • How much of the code base is covered by the tests?
  • Are the critical parts of the package tested?


As developers we work with non-technical people and we can do a better job connecting with them. Non-technical people don’t care about the technical jargon certainly don’t want to hear about how the repository doesn’t fit into the polymorphic inheritance. What they do want to hear are trade-offs or implications.

For example: If someone non-technical asks “Can x feature be developed and deployed within the next month?” The answer is of course “it depends“. Most likely the person asking if the feature can be developed doesn’t necessarily understand the trade-offs of building the feature within 1 month instead of 6 weeks.

Don’t blindly say yes or no with technical jargon. Find a way to connect that brings value to everyone in the conversation. Adjusting what you say depending on who the receivers are. In our example above we want to explain that we can finish the feature within the month, but recommend not to and explain.

Our answers may be:

Finishing the feature within a month will increase technical debt, make it difficult to implement features in the future, and push the limit on capabilities. If any other requests came through during the month we most likely would not finish the feature in time. 1 month is best case scenario and is risky.

If we take 6 weeks instead of 4 we can test our code maintaining stability, keep our sanity, and maintain application flexibility.

In both answers above we explain the trade-offs in a non-technical manner. Arming the person with information necessary to make a proper decision. The most important part is to actually give the person enough information to make an informed decision instead of making it blind.

* Keep in mind recommendations are not always adopted and certainly don’t take offense if the recommendation isn’t adopted. If you provided them enough information to make an informed decision that’s most likely all that can be done.

Life Pro Tip, Tips

Mental Snapshots

Think back to a time where you accomplished something you had no idea how to do or when you accomplished something and surprised yourself. If this hasn’t happened yet be on the lookout when it does happen and you successfully solve the problem create a deep mental snapshot of the wonderful feeling. Keep this in your head and refer to this when things are tough. It will help remind you that you are capable and to give yourself time.

I use this technique often. My mental snapshot I often dig up is during one of my internships, I was tasked to update a Java app. My Java experience at that time was limited to only the exercises the professors had us write. The Java app was bigger than any other app I had ever worked with. At the start of the summer my reaction was “How will I be able to do all of this?”. As the summer came to a close I ended up completing my work plus some additional items. I now use this experience as one of my mental snapshots. I refer to this when asked to things I have no immediate path or answer to.

This can be a frightening, but it’s important to take a step back and think back to your mental snapshot. This will give you the confidence to keep going and to believe in yourself. If possible break the ask down into smaller more manageable pieces. These smaller pieces will help keep your sanity as you attempt to figure out an approach.

Embrace what you don’t know and view it as an opportunity to learn, not something to be afraid of.

Laravel, Programming

The Importance of Eager Loading Laravel Relationships


Page took over 30 seconds to load locally on a local Vagrant virtual machine. After investigating the results were astonishing, over 1,000 queries executed in order to load the page. Not only was there an extraordinary number of queries executing, but also no constraints on the number of records loaded from each relationship.

Test and production environments didn’t show noticeable signs of slowness, but was clear on my local virtual machine that something was wrong.


My research began using the Laravel debugbar. The debugbar helped me discover the amount of queries running for each page. Laravel debugbar has a query collector that conveniently displays the query count. An additional bonus is the number of duplicate queries along with which class called the query. Using these pieces of information I began tracking down the cause.


The cause was the classic N+1 problem. Essentially a loop was calling a relationship that had not been eager loaded. Each iteration of the loop caused the app to query the database.


The problem was easily solved using Laravel’s eager loading along with eager loading constraints.  Eager loading is loading your data up front before accessing the relationship. Because the relationship data was loaded up front accessing the relationship now pulls the value from memory instead of querying the database.

Eager loading constraints tell Laravel to only load specific records within a relationship. When you don’t need every record that a relationship has. E.g. Authors have books, but you only need love and thriller book types.

Eager loading with constraints reduced:

  • Query count from over 1k queries to 50 a whopping 2000% decrease in queries.
  • Number of records loaded from 7200 to 24.
  • Memory consumed by page from 140MB to 14MB.
  • Page load from 30 seconds to 2.5 seconds.


Record labels have artists and artists have songs. If you wanted to get all artists on a record label along with their songs, but only the songs that reached top 10 on the billboards

Non-eager loaded:

$artists = Artist::take(500)->get();

Eager loaded without constraints (returns all songs):

$artists = Artist::with('songs')->take(500)->get();


Eager loaded with only songs that made top 10:

$artists = Artist::with('songs' => function ($query) {
    $query->where('highest_position', '<=', 10); 


Consider the following loop. If we don’t eager load the songs then for each artist a query to the database will be executed. This example may be a bit over the top because in most cases paginating the data would also solve part of the issue in this specific example, but I wanted to outline the potential issue.

foreach ($artists as $artist) {
    $artist->songs->each(function ($song) use ($artist) {
        $song->fullName = $artist . ' - ' . $song->title;