TAN'S Blog

if you obey all the rules you miss all the fun…


Leave a comment

Improve the way of writing functions!

Even though we write lot of functions daily we hardly pay attention how to improve our functions.

FUNCTIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL. THEY SHOULD DO IT ONLY.

Doing Many Things At A Time!

Before giving attention to write an effective function you need to know what are the main points you need to take into considerations.

  • Small!
  • Blocks and Indenting
  • Do One Thing
  • Reading Code from Top to Bottom: The Stepdown Rule 
  • Use Descriptive Names
  • Function Arguments
  • Duplication

Small!

Function should be small. If you are thinking how small they can be, then I would say 2 lines or 3 lines or 4 lines long but these number of lines are not fixed though. You may consider these line numbers as a checkpoint like if your function has many lines make sure your function is not doing so many things.

Below example is from clean code book can be taken as reference:

    public static String renderPageWithSetupsAndTeardowns(
        PageData pageData, boolean isSuite) throws Exception {
        if (isTestPage(pageData)) {
            includeSetupAndTeardownPages(pageData, isSuite);
        }
        return pageData . getHtml();
    }

Blocks and Indenting

Blocks within if statements, else statements, while statements, and so on should be one line long. Probably that line should be a function call. Not only does this keep the enclosing function small, but it also adds documentary value because the function called within the block can be a nicely descriptive name.

This also implies that function should not be large enough to hold nested structures. Therefore, the indent level of a function should not be greater than one or two. This, of course makes the functions easier to read and understand.

Do One Thing

Your function may have one line of code but doing many things at that line is not a efficient and optimised function.

For Ex:

return level4 != null ? GetResources().Where(r => (r.Level2 == (int)level2) && (r.Level3 == (int)level3) && (r.Level4 == (int)level4)).ToList() : level3 != null ? GetResources().Where(r => (r.Level2 == (int)level2) && (r.Level3 == (int)level3)).ToList() : level2 != null ? GetResources().Where(r => (r.Level2 == (int)level2)).ToList() : GetAllResourceList();

Similarly another example:

function build_address_list_for_zip($zip) {
    $query = "SELECT * FROM ADDRESS WHERE zip = $zip";
    $results = perform_query($query);
    $addresses = array();
    while ($address = fetch_query_result($results)) {
        $addresses[] = $address;
    }

    // now create a nice looking list of
    // addresses for the user
    return $html_content;
}

In this function you are fetching record from database and generating html content and rendering it. So it can be split into two functions:

function fetch_addresses_for_zip($zip) {
    $query = "SELECT * FROM ADDRESS WHERE zip = $zip";
    $results = perform_query($query);
    $addresses = array();
    while ($address = fetch_query_result($results)) {
        $addresses[] = $address;
    }
    return $addresses;
}

function build_address_list_for_zip($zip) {
    $addresses = fetch_addresses_for_zip($zip);

    // now create a nice looking list of
    // addresses for the user
    return $html_content;
}

Reading Code from Top to Bottom: The Stepdown Rule

We normally read like a top-down narrative. So when you are calling function inside another function definition then make sure to define the calling function below the function where it is called instead on top of that function:

Ex:

function getStudentInformation() {
    $this->getName($studentId);
    $this->getAge($studentId);
}

function getName($studentId){
    // return student name
}

function getAge($studentId){
    // return student age
}

It should not be like this:

function getName($studentId){
    // return student name
}

function getAge($studentId){
    // return student age
}

function getStudentInformation() {
    $this->getName($studentId);
    $this->getAge($studentId);
}

Use Descriptive Names

Don’t be afraid to make a name long. A long descriptive name is better than a short name which is difficult to understand.

A long descriptive name is better than a long descriptive comment.

Use a naming convention that allows multiple words to be easily read in the function names, and then make use of those multiple words to give the function name that says what it does.

Don’t be afraid to spend time choosing a name. Indeed, you should try several different names and read the code with each in place. As modern IDEs like Visual Studio, PhpStorm, JetStorm makes it trivial to change function names easily.

Function Arguments

The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification—and then shouldn’t be used anyway.

If your function is having more number of arguments then your function is doing one thing at a time. So check your code and split the functions doing one thing at a time.

Duplication

Duplication may be the root of all evil in software. Many principles and practices have been created for the purpose of controlling or eliminating it.

If you are writing large functions doing many things then there is more chance of having code duplication in different functions doing same thing which can be refactored by creating small functions. That small function can be invoked in different functions.


Conclusion:-

So check the above points to write an effective and efficient function which is easy for maintenance and can be tested well.

Hope you enjoy reading the blog and please like and share if you like it and what can be improved don’t hesitate to comment 🙂

Stay tune for more on clean and quality code writing.

References:-  

Robert C.Martin Series: Clean Code Book

https://stackoverflow.com/questions/475675/when-is-a-function-too-long

Advertisements


Leave a comment

Caching using zend framework(1sT sTEP tOWARDS zERO tO hERO)

In this blog we will discuss how to cache your whole project. When we run our project, php interpreter enters into the public/index.php file and after interpreted the project source code, again it comes out through index.php file.

ppt

 

As in above image we can see index.php is entry point and exit point so if we will start Caching and end Caching in index.php file then our whole project will be cached. We will see how it can be possible through demo.

DEMO

1st Step: 

You should have created project using zend framework. Here we are using zend framework 1.12 for easy understanding. If you want to install framework and create project so check these 2 links.

1. https://www.digitalocean.com/community/tutorials/how-to-install-zend-framework-on-an-ubuntu-12-04-vps

2. http://framework.zend.com/manual/1.12/en/learning.quickstart.create-project.html

2nd Step:

Create clone of the project from https://github.com/tbiswal/quickstart which we will use for demo.

3rd Step:

As we are caching whole project so we will follow #Zend_Cache_Frontend_Output method.

4th Step:

Create directory called cache inside public directory and give full read write permission to cache directory.

5th Step:

Add below code in mentioned files:-

1. quickstart/public/index.php

Add this code before $application->bootstrap()->run();


// auto loader
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

// config
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);

// Setup caching option
$frontendOptions = array(
    'lifetime' => 7200, // cache lifetime of 2 hours
    'automatic_serialization' => true
);
$backendOptions = array(
    'cache_dir' => 'cache/' // Directory where to put the cache files
);
$cache = Zend_Cache::factory('Output',
    'File',
    $frontendOptions,
    $backendOptions
);
$registry->set('appCacheObj', $cache);

$appCache = Zend_Registry::get('appCacheObj');

$cacheId = 'myCacheId'.str_replace("/", "_", $_SERVER['REQUEST_URI']);

if(strpos($cacheId, ".php") !== -1) {
    $cacheId = explode(".php", $cacheId)[0];
}

//Clear cache for dynamic page
if($_SERVER['REQUEST_URI'] == "/guestbook/sign") {
    $appCache->remove($cacheId);
}

//Cache start
if($appCache->start($cacheId)) {
    exit;
}

After $application->bootstrap()->run(); add below line:


    //Cache end
    if(!is_null($appCache)){
        $appCache->end();
    }

2. Add below code inside end of the public function save() of models/GuestbookMapper.php file:-


//Clear cache on save action to show new data to user instead of cached data
$appCache = Zend_Registry::get('appCacheObj');
$appCache->clean(Zend_Cache::CLEANING_MODE_ALL);

When you will browse http://quickstart.local/guestbook/sign you will get the page like below.

ppt2

You can check the cache directory which contains the cached files. After click on Sign Guestbook it will clear the cache 1st and then it will create new cached files.

FYI : So for your project you have to clear cache on any save action so that user will get new fetched records otherwise you can show data from cached records.

After completing all the above step when you will browse your project you will find that each page of your application is cached. Only the cached record will be cleaned if you click on any save action. So enjoy 🙂

/*Hope you will love it */


2 Comments

cACHING uSING zEND fRAMEWORK(zERO tO hERO)

In my previous post we have seen simple caching using plain PHP. Here we will learn how to implement caching using Framework. We will use ZendFramework-1.12.3. You might wonder that why I am using very old version framework for caching. Don’t worry I will make you zero to hero in caching step by step. In this version caching is very easy to understand also easy to implement. So let’s start!!

Analysis:

Note: You can also refer manual and for demo you can refer my blog.

Zend_Cache provides a generic way to cache any data. Caching in Zend Framework is operated by frontends while cache records are stored through backend adapters (File, Sqlite, Memcache…) through a flexible system of IDs and tags. Using those, it is easy to delete specific types of records afterwards (for example: “delete all cache records marked with a given tag”).

The core of the module (Zend_Cache_Core) is generic, flexible and configurable. Yet, for your specific needs there are cache frontends that extend Zend_Cache_Core for convenience: Output, File, Function and Class.

Example #1 Getting a Frontend with Zend_Cache::factory()

Zend_Cache::factory() instantiates correct objects and ties them together. In this first example, we will use Core frontend together with File backend.


    $frontendOptions = array(
        'lifetime' => 7200, // cache lifetime of 2 hours
        'automatic_serialization' => true
    );

    $backendOptions = array(
        'cache_dir' => './tmp/' // Directory where to put the cache files
     );

    // getting a Zend_Cache_Core object
    $cache = Zend_Cache::factory('Core',
        'File',
        $frontendOptions,
        $backendOptions
    );

Note: Frontends and Backends Consisting of Multiple Words Some frontends and backends are named using multiple words, such as ‘ZendPlatform’. When specifying them to the factory, separate them using a word separator, such as a space (‘ ‘), hyphen (‘-‘), or period (‘.’).

Example #2 Caching a Database Query Result

Now that we have a frontend, we can cache any type of data (we turned on serialization). for example, we can cache a result from a very expensive database query. After it is cached, there is no need to even connect to the database; records are fetched from cache and unserialized.


    // $cache initialized in previous example
    // see if a cache already exists:
    if( ($result = $cache->load('myresult')) === false ) {

        // cache miss; connect to the database

        $db = Zend_Db::factory( [...] );

        $result = $db->fetchAll('SELECT * FROM huge_table');

        $cache->save($result, 'myresult');

        } else {

            // cache hit! shout so that we know
            echo "This one is from cache!\n\n";

    }

    print_r($result);

Example #3 Caching Output with Zend_Cache Output Frontend

We ‘mark up’ sections in which we want to cache output by adding some conditional logic, encapsulating the section within start() and end() methods (this resembles the first example and is the core strategy for caching).

Inside, output your data as usual – all output will be cached when execution hits the end() method. On the next run, the whole section will be skipped in favor of fetching data from cache (as long as the cache record is valid).

$frontendOptions = array(
    'lifetime' => 30, // cache lifetime of 30 seconds
    'automatic_serialization' => false // this is the default anyways
);

$backendOptions = array('cache_dir' => './tmp/');

$cache = Zend_Cache::factory('Output',
    'File',
    $frontendOptions,
    $backendOptions
);

// we pass a unique identifier to the start() method
if(!$cache->start('mypage')) {
    // output as usual:
    echo 'Hello world! ';
    echo 'This is cached ('.time().') ';
    $cache->end(); // the output is saved and sent to the browser
}

echo 'This is never cached ('.time().').';

Simple Demo: I assume that you have installed zend framework 1.12. Here we will use concept of Example #3 for caching. We will use File as backend adapter to store cache records. So create a directory called “cache” inside public folder with read write permission.

Step 1: Add below code in public/index.php before $application->bootstrap() ->run();


    // auto loader
    require_once 'Zend/Loader/Autoloader.php';
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->setFallbackAutoloader(true);

    // registry
    $registry = Zend_Registry::getInstance();

    $frontendOptions = array(
        'lifetime' => 7200, // cache lifetime of 2 hours
        'automatic_serialization' => true
    );

    $backendOptions = array(
        'cache_dir' => 'cache/' // Directory where to put the cache files
    );

    $cache = Zend_Cache::factory('Output',
        'File',
        $frontendOptions,
        $backendOptions
    );
    $registry->set('appCacheObj', $cache);

Step 2: Add below code in indexAction() inside indexController.php


    $appCache = Zend_Registry::get('appCacheObj');
    $cacheId = 'myBigLoop';
    //If cache record is not present then caching will be started from this code
    if($appCache->start($cacheId)) {
        exit;
    }
    $data = '';
    for ($i = 0; $i < 10000; $i++) {
        $data = $data . $i." ";
    }
    $this->view->data = $data;

Step 3: Add below code in index.phtml inside view folder.


<?php
$appCache = Zend_Registry::get('appCacheObj');
    echo $this->data;

//Caching will be ended here
if(!is_null($appCache)){$appCache->end();}
?>

Before caching:

When you will browse your index page( EX: http://localhost/quickstart/public/index.php) 1st time you will get the result 0 to 10000 as output. Now if you will check your cache directory you will find Binary file named “zend_cache—myBigLoop” where myBigLoop was your cacheId. If you will check the time taken to get your output it will be at least more than 70ms.

before_caching

After caching:

When you will browse index page next time you will get your output from cached files. The time taken to generate output will be 3-4 times faster than previous result.

after_caching

For time being you can delete your cached record manually from cache folder and experiment on it. We will deal with little complex demo in my next blog and also we will clear cached record through code.

Reference:

http://framework.zend.com/manual/1.12/en/manual.html

/*Hope you will love it */


Leave a comment

Get Images in 16 : 9 Format

After lots of searching in Goggle, Stack exchange finally I am able to generate images in 16:9 format for youtube, dailymotion and vimeo. So lets have a look how can we simply get the thumb images from dailymotion, vimeo and youtube API.

Dailymotion

Step 1:
From the http://www.dailymotion.com/us if you will click on any video you will get the url in this format for that video:

http://www.dailymotion.com/video/<video_id>

Ex: Here is my url http://www.dailymotion.com/video/x267cnn_pree-two-feet-shy-indmusic-in-the-great-outdoors_music

Step 2:
To get the thumb image in 16:9 format, request dailymotion API in this format:

https://api.dailymotion.com/video/<video_id>?fields=thumbnail_360_url&thumbnail_ratio=widescreen

Ex: https://api.dailymotion.com/video/x267cnn_pree-two-feet-shy-indmusic-in-the-great-outdoors_music?fields=thumbnail_360_url&thumbnail_ratio=widescreen

Step 3:
In Step 2 you will get response like this: {“thumbnail_360_url”:”http:\/\/s2.dmcdn.net\/HphZA\/640×360-E1L.jpg”}

Reconstruct the thumbnail_360_url:
EX: http://s2.dmcdn.net/HPhZA/640×360-E1L.jpg

Image:

640x360-E1L

  640×360(16:9)



Vimeo

Step 1:
Get the vimeo video URL. Ex: http://vimeo.com/831365

Step 2:
To get data about a specific video, use the following url:

http://vimeo.com/api/v2/video/video_id.output

video_id : The ID of the video you want information for. output Specify the output type. VIMEO currently offer JSON, PHP, and XML formats.

So getting this URL http://vimeo.com/api/v2/video/831365.xml:


<videos>
  <video>
    [skipped]
    <thumbnail_small>http://i.vimeocdn.com/video/52914011_100x75.jpg</thumbnail_small>
    <thumbnail_medium>http://i.vimeocdn.com/video/52914011_200x150.jpg</thumbnail_medium>
    <thumbnail_large>http://i.vimeocdn.com/video/52914011_640.jpg</thumbnail_large>
   [skipped]
  </video>
</videos>

Parse this for every video to get the thumbnail.

Here’s approximate code in PHP:


<?php
  $videoid = 831365;

  $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$videoid.php"));

  $hash = $hash[0]['thumbnail_large'];

  $pieces = explode("_", $hash);

  echo "Your Image is here: " . $pieces[0] . "_640x360.jpg";
?>

O/P:
Your Image is here: http://i.vimeocdn.com/video/52914011_640x360.jpg

Image:-

52914011_640x360

     640×360(16:9)


Youtube

Step 1:
Get the youtube video url. Ex: http://www.youtube.com/watch?v=10OTg6lD4Jc

Step 2:
Request youtube API for 16:9 format:

http://img.youtube.com/vi/<video_id>/mqdefault.jpg or http://img.youtube.com/vi/<video_id>/maxresdefault.jpg

Ex(Medium quality image):
http://img.youtube.com/vi/10OTg6lD4Jc/mqdefault.jpg

Image:

mqdefault

320×180(16:9)

Ex(High quality image):
http://img.youtube.com/vi/10OTg6lD4Jc/maxresdefault.jpg

7788_91393518121913073

 1280×720(16:9)

References:
http://stackoverflow.com/questions/25740034/show-dailymotion-video-and-thumbnail-in-correct-169-format
http://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api
http://stackoverflow.com/questions/1361149/get-img-thumbnails-from-vimeo

/* Hope You will love it */


Leave a comment

CACHING

What is caching?

Caching is an area of a computer’s memory devoted to temporarily storing recently used information. The content, which includes HTML pages, images, files and Web objects, is stored on the local hard drive in order to make it faster for the user to access it, which helps improve the efficiency of the computer and its overall performance.

Most caching occurs without the user knowing about it. For example, when a user returns to a Web page they have recently accessed, the browser can pull those files from the cache instead of the original server because it has stored the user’s activity. The storing of that information saves the user time by getting to it faster, and lessens the traffic on the network.

Caching Mechanism:

Drawing21

Very Simple Example to learn caching in PHP:

Step One: Create the top-cache.php file:

We need to create two files. Here’s the first one: Create a new file named top-cache.php and paste the code below in it.

<?php
  $url = $_SERVER["SCRIPT_NAME"];
  $break = Explode('/', $url);
  $file = $break[count($break) - 1];
  $cachefile = 'cached-'.substr_replace($file ,"",-4).'.html';
  $cachetime = 18000; //in seconds

  // Serve from the cache if it is younger than $cachetime
  if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
      echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
      include($cachefile);
      exit;
  }
  ob_start(); // Start the output buffer
?>

So, what this code does? The first 5 lines create the cached file name according to the current php file. So, if you’re using a file named index.php, the cached file will be named cached-index.html.

Line 6 creates a $cachetime(in seconds) variable which determines the life of the cache. You can experiment by changing the time.

Lines 9 to 13 are a conditional statement which look for a file named $cachefile. If the file is found, a comment is inserted (line 10) and the $cachefile file is included. Then, the exit statement stops the execution of the script and the file is sent to the client brother. Which means that if a static file is found, no php code is interpreted by the server.

Line 14 creates a buffer, if the $cachefile file isn’t found. That’s all for the top-cache.php file.

Step two: Create the bottom-cache.php file:

Now, create a second php file, named bottom-cache.php and paste the code below in it.

 <?php
  // Cache the contents to a file
  $cached = fopen($cachefile, 'w');
  fwrite($cached, ob_get_contents());
  fclose($cached);
  ob_end_flush(); // Send the output to the browser
?>

If a file named $cachefile isn’t found on your server, this code is executed and create the file, so next time the page will be called, the $cachefile static file will be served to the client browser instead of executing the whole PHP file.

Step three: Include cache files on your page
Now that you have created the two necessary files, you simply have to include them on the php page you wish to cache. As you probably guessed, the top-cache.php file must be included in the beginning of your php page and the bottom-cache.php at the end, as shown below:

You can save this page as index.php.

<?php
  include('top-cache.php');

  // Your regular PHP code goes here.
  //You can uncomment the code and test it.
  /*
  for($i=1;$i<=100000;$i++) {
    echo "Hello world";
  }
  */

  include('bottom-cache.php');
?>

Now if you test the cache on a slow page, you’ll be amazed by how faster the page is.

Note: Give full read and write permission to the directory where you will put the above 3 files. For Windows OS no need to give file permission.
Ex: If this is my folder structure in ubuntu():

cac

Then you should give the read and write permission to caching folder.

When you will browse the page after uncommenting the code in step three “http://localhost/caching/index.php” for very 1st time you will see it will take time around 50-70ms. Next time you will see it will show the result in 17 to 25 ms. Its amazing.

Before caching(time taken 63 ms):

before

After Caching(time taken 18 ms):

after

Reference:

http://en.wikipedia.org/wiki/Cache_(computing)

http://www.catswhocode.com/blog/how-to-create-a-simple-and-efficient-php-cache

http://www.citrix.com/glossary/caching.html

/*Hope you will love it*/