Why you should stop using loops

If you have been programming for more than 2 secs. You are probably already familiar with loops.

In PHP (which is the language we will be talking about in this post) there are three common methods to run a loop.

  • While
  • For
  • Foreach

Even seasoned programmers make generous use of this constructs. While they still have their use, I am here to tell you that their more functional equivalents

  • array_map
  • array_walk
  • array_filter

are far much better.

To demonstrate this point here is a gist showing a snapshot of a short demo. The full demo can be found here https://github.com/prodeveloper/array_compares This code capitalizes first name of each string.

Here is a the snapshot

Just from the code above you see the following benefits:

  1. Functional constructs are are far more concise: From our example above there we’re 2 more lines in the loop constructs than in functional ones. This may seem trivial but a typically project has thousands of lines of code. You do the math.
  2. Functional construct enforces single logic for a function: It may not seem like it at first glance but loop constructs are in fact executing two pieces of logic in that one function. The first one to loop through an array the second to actually deliver the payload to the string.

 

  1. Less variables in functional constructs: You note we needed to create a variable to hold the value that the pointer is currently pointing to. Its usually best to reduce variables where it makes sense. If nothing else to save ourselves the hustle of coming up with names
  2. Readability is compromised in loop constructs: To understand what is happening we must understand both the function being called and how the looping works. Case in point did you notice the name variable was passed by reference?

Well I am sure there are far more benefits to the functionals. If you have something to add, would love to hear from you.

Thank you!

Chencha Jacob

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Why you will love API centric design

It is no secret that am a big fan of API centric app design. Its also no secret that I want you to follow this footsteps as well. Why? Because there is just so much love to go around. So below I have listed two of the top reasons this kind of design will benefit everyone in your development cycle

 

What is API centric design

 

First we begin by talking about an API. An application programming interface (API) is a source code based specification intended to be used as an interface by software components to communicate with each other. An API may include specifications for routines, data structures, object classes, and variables. This is the definition according to wikipedia. http://en.wikipedia.org/wiki/Application_programming_interface

 

API centric design is the craft of designing your entire application around APIs and API based concepts and best practices.

 

 

Why your users will love you

 

I am a firm believer in the maxim “Love your user” This is what we stand for and as an API Engineer here are some of the reasons I believe your users will love you.

 

  1. Well defined functionality: Thinking in terms of API inherently forces you to define concerns early on.
  2. Mashups: Its all about bringing value to the users. While your propriety software may have a lot of value to your current user base, there maybe an entire user base out there that could very easily benefit by being able to extend your user functionality

 

Why your clients will love you

  1. Faster turnaround: API centric apps tend to have a lot of reusable components. This ensures that you can quickly deliver your app to clients
  2. Better rates: With less code to write, you can, if you so wish, choose to pass on some of the cost benefits back to your clients.
  3. Future ready: New devices come up everyday, for example the mobile revolution is currently peaking and more users than ever are accessing the Internet via their mobile devices. With an API centric design your app is ready for mobile and whatever may hit next http://thenextweb.com/insider/2013/03/18/npd-us-homes-now-hold-over-500m-internet-connected-devices-with-apps-at-an-average-of-5-7-per-household/

 

Why your colleagues will love you

 

  1. Maintenance heaven: Due to the strict adherence to the single responsibility principle http://en.wikipedia.org/wiki/Single_responsibility_principle that this kind of design begs, your code will be far much more easier to understand and debug for your fellow devs.
  2. Collaboration: since responsibilities are handled entirely per API, this means that its far easier to have multiple developers working on the same project smoothly.

 

Why you will love you

  1. Money money money: With more and more resusable code, you spent less time developing new applications thereby saving you time and effectively increasing your income on fixed projects.
  2. Visibility: An easy to integrate with system is a developers dream, by making this dream come true your app can gain mass adoption within the developer community earning not only the developers as new users but also their users.

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Resource Intensive PHP Tasks part 5

Well we are finally here, end of this 5 part session.

By now you should have learnt principles that you will need in building your next project with long running task.

However if you are running enterprise application or otherwise an application that may need high volume you may need to consider a more formal method of serving your users.

Below you will find some of my recommended sites.

Please note I do not get paid anything when you follow this recommendations, I only have them here because I think they will be useful to you.

Open source queuing libraries

  • PHP-Resque: Built by the guys who built github, this is one of the most powerful yet easiest to implement queuing system out there. Its biggest weakness is that it borrows a lot from Ruby and may not be intuitive to use for hard core PHP devs. See more here https://github.com/chrisboulton/php-resque
  • Mongo Queue: Schemaless databases are gaining a lot of ground in the dev community due to their relatively faster response times. If you are part of this wave, you may want to consider Mongo Queue for your job. It runs on Mongo db. See more here https://github.com/lunaru/MongoQueue
  • Laravel Queue: Being a sucker for Laravel you all knew it had to make an appearance right 🙂 Seriously the Queue module for laravel is one of the easiest to configure and use. Plus it provides integration with third party commercial queue services if you need it. If there was no other reason to switch to Laravel this is it! See more http://laravel.com/docs/queues

Commercial queuing services

  • Iron.io: At 10 million free API calls per month, this is the definitely the service you want. Not to mention integration is easy and is supported by numerous libraries. As a matter of fact Laravel supports this out of the box!! See more http://www.iron.io/
  • Amazon SQS: Amazon is known for reliability and scalability. This is for you if you have some money to spend and reliability and scalability are an issue. With Amazon you get 1 million calls free http://aws.amazon.com/sqs/.
  • Google Queue: This is last on the list because its only relevant to Google App engine users. This is not yet supported in PHP as of today (July 2013) but beta tests are in way to support php. In the meantime it doesn’t hurt to check out the docs for python https://developers.google.com/appengine/docs/python/taskqueue/queues.

I believe you now have the knowledge to build that next resource hungry task. Devs, go forth and build the world!!

 

by Jacob Chencha

Facebooktwittergoogle_plusredditpinterestlinkedinmail