core error

Archive for July 2011

Head JS

Posted on: 28/07/2011

What

With Head JS you can load your javascript files in a non blocking way, just like images.

Why

When a page loads its JavaScript files through Head JS, the rendering is not blocked until the files are downloaded ensuring a boost in performance especially for pages with a lot of javascript code.

How

// use jQuery on the body of the page even though it is not included yet
head.ready(function() {
   $("#my").jquery_plugin();
});

// load jQuery whenever you wish bottom of the page
head.js("/path/to/jquery.js");

With this piece of code we can add js code in the body of the page without blocking the page render. For example, jQuery code can be added wherever we want even we load jQuery at the bottom.

What

__autoload()  is a function which is automatically called in case you are trying to use a class/interface which hasn’t been defined yet.

Why

This is very useful for removing the need of making a long list of includes at the beggining of each script so we can use the classes inside those files.

How

you can create an autoloader.php file with:

function __autoload($class_name) {
    include str_replace('_','/',$class_name) . '.php';
}

This file can be included in the main php file (i.e. the index.php of an MVC) so it gets included in all the php scripts in the application. Therefore we could do:

$c1 = new controllers_c1;
$m2 = new models_services_profiles;

As you can see, we can create a model of class profiles without including the file models/services/profiles.php since it is autoloaded by the autoloader php when the class was not found in the first place.

What

stdClass is php’s generic empty class.

Why

Using PHP’s stdClass feature you can create an object, and assign data to it, without having to formally define a class.

How

You can quickly create an object out of the stdClass to hold data in it:

$movie          = new stdClass;
$book->name     = "Back to the Future";
$book->director = "Robert Zemeckis";
$book->cast     = array('Michael J. Fox', 'Christopher Lloyd', 'Lea Thompson');

What

ReflectionClass allows the developer to retrieve information about a class during runtime.

Why

Two of the many uses we can give to this class are:

  1. get a list of the methods before making any call to them (ReflectionClass::getMethods)
  2. make a private method available to be called (quite handy to unitest private methods)

How

Let’s see an example of the use #2 (testing a private method):

class Foo {
          private function validateData($data) {
		$is_valid = true;
		if($data == 7){
                       return true;
                }else{
                       return false;
                }
          }
}

class DataTest extends PHPUnit_Framework_TestCase {
    public function testValidateData()
    {
		$reflection_class = new ReflectionClass("Foo");

		$method = $reflection_class->getMethod("validateData");
		$method->setAccessible(true);

		//We need to create an empty object to pass to
		//ReflectionMethod invoke method followed by our
		//test parameters
		$foo = new Foo();

		//Boundary test - False expected
		$this->assertFalse($method->invoke($foo, 5));

		//Good data - True Expected
		$this->assertTrue($method->invoke($foo, 7));
    }
}


SELECT id, tip_text FROM tips ORDER BY importance:

  1. Denormalize! always design your tables thinking of performance, not in space
  2. Use the slow query log
  3. Avoid wildcards (%) in the beggining of a LIKE statement
  4. Learn how to use EXPLAIN the best way
  5. Use MyISAM if you know that the data in the tables is not very dynamic (i.e. a reporting system)

Since Javascript is ‘class-free’ inheriting is not part of the definition of a class but it is handled by the method .inherits(<classn

ame>) and it’s done in run-time.

For example:

function ZParenizor(value) {
    this.setValue(value);
}

ZParenizor.inherits(Parenizor);

Now ZParenizor has inherited all the methods and attributes from Parenizor and still they can be overwritten or overloaded.

Sharding

Posted on: 17/07/2011

What

Sharding consists in horizontally splitting a database: divide the rows of a database in groups to improve the performance of the queries on that database.

Why

Sharding reduces the index size and therefore, improves the search performance. Besides, sharding allows to store separate pieces of a database in different hardware improving the performance.

How

This technique should be transparent for the developers and it’s the Systems Engineers job to configure the databases in the most efficient way. Anyway, the developers may be very valuable at the design time since they best know which groups of data will be the most accessed and in which way.

tip: sharding should not be done unless we really need it for the sake of scalability due to the performance hit we may have in the DBMS each time we need to identify in which shard we can find the desired data.



  • None
  • Justin Cooney: Great comic, this made me laugh!
  • Justin Cooney: Interesting, I didn't know about the Google App Engine, I'm glad I found your article on it!

Categories