Monday, July 10, 2017

Enforcing Development Teams write Good Code(PSR 1 and 2 Compliant)

If we look at this  relatively old graphic from Zend.com or at other infographics on sites like https://trends.builtwith.com/framework/PHP or we understand that more than 80 percent of websites today including Facebook, Wikipedia, Tumblr and WordPress are using PHP in some form.

The focus for any business is on delivering more robust Apps faster the key being maintainability of the code. Following Agile methodology we keep making many parallel changes to the code base as we go. All this is only possible if we follow certain standards in the way we code.

A few well know PHP projects like Zend, Symphony, Yii, Cake, Slim, Drupal, Magento etc. got together and formed FIG(Framework Interoperability Group). PHP-FIG as it's commonly known have worked on a list of PSR's. More commonly known as PHP Standards Recommendations.

The Accepted list of PSR's include
1 - Basic Coding Standard
2 - Coding Style Guide Paul
3 - Logger Interface
4 - Autoloading Standard
6 - Caching Interface
7 - HTTP Message Interface
13 - Hypermedia Links
16 - Simple Cache

Our focus for this article will remain on PSR 1 and PSR 2. How to ensure that the Basic Coding Standard and the Coding Style Guide are followed by development teams.

A quick look at PSR 1 shows

  • Files MUST use only <?php and <?= tags. 
  • Files MUST use only UTF-8 without BOM for PHP code. 
  • Namespaces and classes MUST follow an “autoloading” PSR: [PSR-0, PSR-4]. 
  • Class names MUST be declared in StudlyCaps. 
  • Class constants MUST be declared in all upper case with underscore separators. 
  • Method names MUST be declared in camelCase.
  • Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.
PSR 2 says
  • Code MUST follow a “coding style guide” PSR [PSR-1]. 
  • Code MUST use 4 spaces for indenting, not tabs. 
  • There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less. 
  • There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarations. 
  • Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body. 
  • Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body. 
  • Visibility MUST be declared on all properties and methods; abstract and final MUST be declared before the visibility; static MUST be declared after the visibility. 
  • Control structure keywords MUST have one space after them; method and function calls MUST NOT. 
  • Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body. 
  • Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before.

First step as usual is approaching the team and sharing the benefits of following PSR which they would most probably already know. Post which we find delays citied as more time is needed to achieve and then check during code review. Everything that goes wrong will somehow be due to this enforcement including logical errors.

Time is money and any standard should not add to timelines so a very Quick solution will be to configure IDE's.

Most of the common IDE's have ready solutions,
Net Beans - https://github.com/allebb/netbeans-psr-formatting
Sublime Text - https://gist.github.com/mikefrancis/63436f1cb94ec31ce31f
PHP Storm - In built, use link to activate - https://www.matthewsetter.com/enforcing-psr1-and-psr2-with-phpstorm/
Eclipse - Can be fixed within Eclipse using http://cs.sensiolabs.org/
Zend Studio - This can easily be imported - https://github.com/wcomnisky/php-formatter
Atom - https://atom.io/packages/php-fmt is a good one but I prefer https://atom.io/packages/php-cs-fixer

Now though this helps set up but enforcing is still not near. For a multitude of reasons these might change, stop working completely or partially. Due to over sight or mistake or whatever reason suddenly your repo might have non PSR compliant code.

So how do we enforce, what can we do to make sure our team ALWAYS commits PSR compliant code in the repos.

GIT Pre Commit hook is our saviour here. A ready solution is https://github.com/hootsuite/pre-commit-php

Just add to your .pre-commit-config.yaml file with the following

- repo: git@github.com:hootsuite/pre-commit-php.git
 sha: 1.1.0
 hooks:
  - id: php-cs
   files: \.(php)$
   args: [--standard=PSR1 -p]
  - id: php-cs-fixer
   files: \.(php)$
   args: [--level=PSR2]

The above handles PSR 1 and PSR 2 adding few more lines can also enable Lint and Unit testing.

While setting up the IDE help the developer follow the PSRs, the Pre Commit Hook will enforce that the commits only reach the repo if the standards are being followed. With this few minutes read and install Startups, SME's corporates everyone can enforce Development Teams write Good Code (PSR 1 and 2 compliant)

Wednesday, September 7, 2016

IPhone 7, Iphone 7 Plus and Apple Watch 2 Launched

Continuing from Probable Launch of IPhone 7, Iphone 7 Plus and Apple Watch 2 Today


What I missed dearly yesterday was Steve Jobs on the Stage. That guy was magical, be it the 1984 Mac Launch (Watch Here) or the 2007 IPhone Launch (Watch Here)

While nothing exciting was happening Amazon decided to add some spice by showing a glimpse of Iphone 7 before it was launched. 

Funny that Mario one of my favourite plumbers had to be brought in and that made Nintendo shares rise by 29 percent while Apple's barely moved.

From my checklist lets quick see what made through

Correct - No headphone 3.5mm socket
Correct - Headphones can be used on Lightning port or bluetooth
Partially Correct 12 and 7 Megapixel - Dual 12 megapixel SLR quality camera
Correct - 256GB Variant possible
Correct and that too IP67 - Water Resistant if not Proof
Not Sure - 3D touch on lock screen
Not Sure - Enhanced messaging App
Not Sure - New widgets screen

Correct - Pressure sensitive home button

Will know more as I lay hands on the extra shiny black handset with a better processor(A10 Fusion), memory and battery. Overall if they would have called it IPhone 6.5 it would not be entirely wrong.

Probable Launch of IPhone 7, Iphone 7 Plus and Apple Watch 2 Today


In the next few hours the Cupertino giant is rumored to unveil the IPhone 7, Iphone 7 Plus and Apple Watch 2 at its annual event in San Francisco today. Apple is also expected to debut its newest mobile operating system for its iPhones and iPads, iOS 10 which is to support upto Iphone 5.

At 10:30pm IST we can watch it Live on www.apple.com/apple-events/september-2016/

There have been numerous Rumours floating around about the new iPhone for months, with many arguably authentic details, images, videos being floated. Lets list a few...

  • No headphone 3.5mm socket
  • Headphones can be used on Lightning port or bluetooth
  • Dual 12 megapixel SLR quality camera
  • 256GB Variant possible
  • Water Resistant if not Proof
  • 3D touch on lock screen
  • Enhanced messaging App
  • New widgets screen
  • Pressure sensitive home button

I am not an apple fanboy though I have been using my macbook for the last 6 years and my Ipad and IPhone for a few years. With player like Samsung Note 7, I am not sure how satisfactory Apple's release is going to be, wether it will be worth selling a kidney for or not but one thing is for sure.

Everyone is waiting with bated breath, the suspense unfolds in a few hours.

Read Further: IPhone 7, Iphone 7 Plus and Apple Watch 2 Launched

Saturday, May 28, 2016

OM

Millions of programmers are as I speak coding or being trained to code how to connect to a DB and create a database driven business application. This could be as simple as a POS system or as complex as an ERP. Are we really using our brain to the optimum here? Is connecting to db, fetching, saving, creating form, implementing business rules not redundant? Different ways, languages, methods of repeating almost the same thing? Whether it be create a mobile API, Web App or Desktop based Software, code in c#, Java, PHP or nodejs!!!

Since 2000 when my team and I created Sahayak and subsequently many other softwares following that, what hit me hard was coders. As a small company in 2000 my approach was recruiting bright trainees and training them up in coding. The approach worked because I and my core team had good knowledge and the trainees came in cheap. The problem was as soon as these trainees "felt trained" they moved to better paying jobs.

One fine day frustrated by that in 2004 I contemplated what is the use of teaching these people who would not be loyal for the smallest of time frames. Why not teach programming to the computer itself. So started what was then called AutoCoder. After personally testing Autocoder in multiple projects doing smaller and then larger functions it was handed to the team as a COM component. The team used it for the ASPA Project and it made coding substantially easy and quick. This moved on towards .Net and then when I closed business and shifted to 123Greetings was shelved. Between 123Greetings and Mail.com I restarted this and since then off and on when ever I can take out time I have been playing with it.

The new avtaar of AutoCoder was rechristened as "OM", the beginning and end of a new phase of coding. It explored web, caching, SOA, messaging, agile development, big data, n layered architecture and many other things. It considered the fact that coding is an agile iterative exercise and there will always be new custom code a developer will want to add in. Ideally OM should learn this as it is done but even if that is not possible should co live with it with ease. 

Focus is for Developers to create the structure and Business Logic, Om to take care of any and all things redundant and developers to fill in the rest. Will update more on this as I would believe it is nearer for a public appearance. I understand there are many tools doing that so I am putting this post here today for people to respond back with those and/or their thoughts.

Tuesday, June 10, 2014

Parallel Processing in PHP - Threads

With the kind of compute resources we have in today's world processing which took hours and days have become a matter of hours. With AWS, Azure and other cloud providers its easy to ramp up and down on resources. Not only do I get my work done faster but also cheaper.

Lets see how its done on PHP.

PHP has multiple ways to allow things to be done parallelly each with its positives and negatives. Lets mention a few ways it can be done before we start...
pthreads
pcntl_fork
popen
fopen
curl
fsockopen

Lets try out pthreads...

Our Problem
Large List
Web API
DB response
= Long Running Time



Solution
Large List
Web API
DB response
Parallel Processing
= Quick work done.



So assuming we have 4 processors what would otherwise be
Loop {
  $i = Read Item
  Fire Web API (i)
  Write DB
}

VS the smarter way

processApi(i) {
    Fire Web API (i)
    Write DB
}
$pipe = 4
$process = 0
$count = 0
Loop
  If ($process < $pipe) {
    $i = Read Item
    $process++
    $threads[$count] = new Thread( 'processApi' )
    $threads[$index]->start( $i );
  } else {
    foreach ($threads as $key->$value){
      if( ! $value->isAlive() ) {
        unset( $threads[$key] );
        $process--;
      }
    }
    sleep (1);
  }

This is a very rough example without error checks etc but should give an example to any developer.

Wednesday, April 16, 2014

Oracle Virtual Box - This kernel requires an x86-64 CPU, but only detected an i686 CPU

A new Lenevo I5 with 16GB RAM didn't please me a lot after my Macbook Pro of same config which I have been using since last one and a half years. Will not start upon the differences and speed cause that's not the aim of this post. In a nutshell I love my Mac due to its ease of use, speed and performance.

I was already using Virtual Box and Vagrant on my Mac but never faced any issue. So on my "shiny new" machine, I installed the needed stuff. I wanted to work on a HAProxy based solution for changing TCP Headers and for a quick setup used Shamil's HAProxy Vagrant Example from GitHub.

To my dismay the vagrant script couldn't start even one of the three instances it was supposed to start. While on the second run it showed instance is running but I cant connect or use it. A deeper check revealed that the machine was stuck on
This kernel requires an x86-64 CPU, but only detected an i686 CPU.
Virtual Box Virtual box manual says ...
3.1.2. 64-bit guests
VirtualBox supports 64-bit guest operating systems, even on 32-bit host operating systems, provided that the following conditions are met:
  1. You need a 64-bit processor with hardware virtualization support (see Section 10.3, “Hardware vs. software virtualization”). 
  2. You must enable hardware virtualization for the particular VM for which you want 64-bit support; software virtualization is not supported for 64-bit VMs. 
  3. If you want to use 64-bit guest support on a 32-bit host operating system, you must also select a 64-bit operating system for the particular VM. Since supporting 64 bits on 32-bit hosts incurs additional overhead, VirtualBox only enables this support upon explicit request. 
On 64-bit hosts (which typically come with hardware virtualization support), 64-bit guest operating systems are always supported regardless of settings, so you can simply install a 64-bit operating system in the guest.
Warning On any host, you should enable the I/O APIC for virtual machines that you intend to use in 64-bit mode. This is especially true for 64-bit Windows VMs. See Section 3.3.2, “"Advanced" tab”. In addition, for 64-bit Windows guests, you should make sure that the VM uses the Intel networking device, since there is no 64-bit driver support for the AMD PCNet card; see Section 6.1, “Virtual networking hardware”.

64 Bit Processor
For my I5 3230M Intel Says
The important thing here is that my processor supports VT-x. So I should be good.

However when I checked the instance properties it shows VT-x - Disabled. The icon on virtualbox showed 64bit and the OS Type here showed only Ubuntu (32 bit).

The solution(Part - 1)

Go to your systems CMOS, depending on what system you use. You might have to press Del, F2 or OM Key on the splash screen after restating your system.

Choose A > B > C and set Virtulization to Enable. Press F10 to save and exit or whatever other method your CMOS might present.

64 Bit OS.
I am running 64 bit Windows 7 Professional. So what needs to be done here.

The Solution (Part - 2)

Goto Control Panel > Programs and Features > Turn Windows features on or off and there you will need to enable Remote Server Administration Tools > Role Administration Tools > Hyper - V Tools.

In case you don't see Remote Server Administration Tools please install Remote Server Administration Tools for Windows 7 with Service Pack 1 (SP1). To be more precise Windows6.1-KB958830-x64-RefreshPkg.msu





And Voila you will see both 32 and 64 bit machines and VirtualBox will work as expected.