$theTitle=wp_title(" - ", false); if($theTitle != "") { ?>
Talks around Computer Science and Stuff
Here is a very interesting video from Linus Torvalds on Git. He emphasizes on the importance and added value for companies to use decentralized source control.
You better listen to this video or you just may end up stupid and ugly 🙂
In: Scalability
10 Aug 2009If you ever worked in a software company, you most likely heard once someone saying “Hey, we can’t do this, because this doesn’t scale!”
I believe “Scalability” must be on the top 10 list of the “buzz words” most used in the software industry.
It is usually considerated as a killing argument in a conversation. “If something doesn’t scale, then we don’t want it.”
One of the things that fascinates me about “buzz word” is that most of the time it has so many different definitions. Everyone has a tendency to give it a personal definition. Often, people will be too shy to ask for a clear definition. Therefore, it makes it even harder to be able to really understand what underlies behind it. So, What does Scalability mean ?
In telecommunications and software engineering, scalability is a desirable property of a system, a network, or a process, which indicates its ability to either handle growing amounts of work in a graceful manner or to be readily enlarged. (Source: Wikipedia)
In my humble opinion, there are a few important keywords (in bold) in this definition that should be kept in mind when thinking about scalability.
I recently watched that video (I love google tech talks) from Cuong Do Cuong given during a quite old conference in Seattle (in 2007). Cuong is an engineering manager at Google (YouTube). “He was part of the engineering team that scaled the YouTube software and hardware infrastructure from its infancy to its current scale. Prior to YouTube/Google, he held various software development and management positions at PayPal and Inktomi.”
Cuong shares with us some his experiences, the different issues related to scalability he and the team faced. I hope you will enjoy it as much as I did.
In: User Experience
15 Jun 2009Recently, I have been working on building a new search engine. After performing some searches on Google, I ended up watching a tech talk from Marissa Mayer. Marissa is vice President (Search Products & User Experience) at Google. Just to give you a background here is a short summary of her bio, she has an impressive experience (found on the google site) :
Marissa leads the company’s product management efforts on search products – web search, images, news, books, products, maps, Google Earth, the Google Toolbar, Google Desktop, Google Health, Google Labs, and more. She joined Google in 1999 as Google’s first female engineer and led the user interface and web server teams at that time. Her efforts have included designing and developing Google’s search interface, internationalizing the site to more than 100 languages, defining Google News, Gmail, and Orkut, and launching more than 100 features and products on Google.com
I saw her presentation named “Scaling Google for Every User” (June 2007) at the Seattle Conference on Scalability. I really liked the part where she talks about how much features should be presented to end users, how to present them and how basically simplcitiy beats complexity. She kinda destroys the myth that “having more features is always better”.
In: PHP
29 May 2009PHP Code Comparator is a command line script allowing to compare different versions of a library, framework or package. It will detect code changes in terms of class existence or method definitions.
If you ever wanted to have a way to quickly identify what classes have been altered or methods have been changed between two different versions of the same package, PHP Code Comparator will help you achieve that.
Instead of using a diff tool, you can simply use this tool. It will basically parse a folder recursively, detect any defined classes and extract the various methods and parameters. This will then be performed on the second version of your library. Finally, a summary will be displayed showing the differences that have been detected.
This tool will detect if a class has been removed, if new classes have been added, if methods have been removed or added and also if the signature of a method has changed.
You can browse the code and download it from github.com.
http://github.com/alfallouji/PHP-Code-Compare/
You can also directly get the source code from the git repository.
git clone git://github.com/alfallouji/PHP-Code-Compare.git
You will find more information on the official page : PHP Code Comparator
In: PHP
25 May 2009I recently heard about a funny contest handled by Microsft. The concept is really fun, and so is their web site (Check the video!).
Here is a short description of the contest.
Microsoft presents the Ultimate Battle: It’s a knock-down, drag-em-out battle between professional and student developers! And it’s your big chance to show the world your awe-inspiring PHP application – on the Windows-based platform! We’ll select one student and one professional coder for the Ultimate Throwdown – a final bout at the “Make Web, Not War” Conference! The winner can take home up to $10,000 in total prize money!Challenge a friend, invite a colleague, ask anyone – get the whole gang in on the Ultimate Throwdown Action.
 Last day to enter the contest is June 3rd. So hurry !
In: Knowledge
21 May 2009WolframAlpha is a computational knowledge engine created by Stephen Wolfram
It basically generates output by doing computations from its own internal knowledge base (composed of many trillions of elements), instead of searching the web and returning links.Â
I spent some time testing it and I gotta say it is awesome. So, no it isn’t gonna replace Google, it is for a different usage.
It is very usefull when you want to find answers to a particular question, for instance, if you look for earthquake in indonesia, you will get this as a result.
Â
I really like the way the information is reported / outputed to the end user. If you try with “barcelona montreal”, it will even display the result in a table and compare the results.
Â
Â
I took some time to read a little bio about Stephen Wolfram and well, it is pretty impressive. Here is a short summary of what we can read about him on Wikipedia.
Who is Stephen Wolfram ?
Stephen Wolfram (born  in 1959) is a British physicist, mathematician,author and businessman, known for his work in theoretical particle physics, cosmology, cellular automata, complexity theory, and computer algebra.
Wolfram is considered as a child prodigy, he published an article on particle physics at age 16 and entered Oxford University at age 17. He received his Ph.D. in particle physics from the California Institute of Technology at age 20. He developed the famous software Mathematica.
(Source : Wikipedia)Â
Have fun and try that very nice tool, this is defintely going in my bookmarks folder: WolframAlpha.com
In: PHP
17 May 2009Some people just like to write as few line of codes as possible. Well, PHP can be tricky sometimes.. For instance, be carefull when declaring a variable and passing it as a parameter to a function or to a method at the same time. If that parameter is supposed to be passed by reference, you may end up with some unexpected behaviors !
Look at the following code, What do you think will be outputted ?
function changeStr(&$str)
{
$str = 'foo_' . $str;
return $str;
}
$res = changeStr($str = 'bar');
var_dump($str);
var_dump($res);
Here is the result.
string 'bar' (length=3) string 'foo_bar' (length=7)
So basically, $str hasn’t been affected by the changeStr() function. Let’s try with an array now..
function changeArr(&$arr) { $arr['id'] = 10; return $arr; } $res = changeArr($arr = array()); var_dump($arr['id']); var_dump($res['id']);
The result is…
null int 10
Well, same behavior as earlier. $arr wasn’t altered by the changeArr() function !
What about objects, does it do the same thing ?
function changeObj(&$obj) { $obj->id = 10; return $obj; } $res = changeObj($obj = new StdClass()); var_dump($obj->id); var_dump($res->id);
Here is the result.
int 10 int 10
So, it doesn’t work for a string or an array, but it work with objects !
Well, I highly suggest any developer not to declare a variable and pass it to a method or a function at the same time. Just write the additional line of code, it is gonna reduce the risk of error and actually might make your core more readable !
I have decided to write a serie of posts around the Singleton design pattern. This first one will discuss the uniqueness aspect of a Singleton.
What is a Singleton again ?
In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. (Wikipedia)
Here is a typical implementation of a Singleton class in PHP. You will see this kind of code in many different existing frameworks.
/**
* A typical implementation of a Singleton
*/
class aSingleton
{
private static $_instance = null;
public $foo = null;
private function __construct()
{
}
public static function getInstance()
{
if(null === self::$_instance)
{
self::$_instance = new aSingleton();
}
return self::$_instance;
}
}
So, is there anything wrong with that code ?
One of the things that I really dislike about this implementation (there are actually many others things I dislike about Singleton, but I will discuss them in others posts), is the fact you cannot (actually this isnt necessarily true as we will see later) have two different instances of a Singleton. I dont understand why a class should enforce a such rule / constraint. Why would someone want to have a such constraint ? What good would it make ?
Some may argue that sometimes in an application, you want to keep using the same instance of an object. Usually, they will say that objects like a logger, Â a configuration or a database handler are good candidates for a such use. Well, that’s fine with me, but why do I have to use a Singleton for that ? Is this really the best solution to do that ?
Let’s use a parrallel. Imagine, we need to always use the same hammer to do a job for some good reason. Â So, we decide you are going to enforce that requirement, by destroying all the others existing hammers and any shop / factory selling or producing hammers. That way, when we use a hammer, we are sure it is the same one (it is the only hammer that exist in the whole universe and it is impossible that for any other hammer to get built).
Doesn’t that sound crazy and  extreme ? I mean, why wouldn’t we just mark that hammer (write a name on it or something) and just store it in a specific place, so we know where to get it and we can ensure that we are using the same hammer. Also, if one day we do need to have another hammer, well we would be able to…
There is also a problem with most existing implementations of a Singleton. Â The design pattern is supposed to enforce the number of different instance of a particular class, right ? Well, there are ways to bypass that and end up with different instances of a Singleton.
In PHP, we could use the clone construct.
$a = aSingleton::getInstance();
$a->foo = 10;
$b = clone $a;
$b->foo = 20;
echo $a->foo . PHP_EOL; // Display 10
echo $b->foo . PHP_EOL; // Display 20.
// Damn this isnt anymore my Singleton!
To prevent that, we would need to implement the __clone() method (define it as final and throw some kind of exception whenever called). This would  solve the issue with the clone construct.
Well, that still won’t completely prevent someone from being able to have two different instances of our Singleton. Someone could still use the serialization. Look at this code.
$a = aSingleton::getInstance();
$a->foo = 10;
$str = serialize($a);
$b = unserialize($str);
$b->foo = 20;
echo $a->foo . PHP_EOL; // Display 10
echo $b->foo . PHP_EOL; // Display 20.
// Damn this isnt anymore my Singleton!
Again, to prevent that, we could implement the __wakeup() method (also declare it as final and throw some kind of exception).
So, we just added a new constraint on the class : We cannot unserialize it anymore via the unserialize function. If we cannot unserialize, then we made serialization pretty much useless…
That’s the end of this first post on Singleton. I am hoping that after reading this, you already feel (if you are a Singleton adept) that there might be something wrong or bad using this design pattern.
My next post will discuss other aspects that I really dislike about Singleton, such as how to unit test Singleton, why Singleton is a global state and why global states are bad, etc.
A few weeks ago, I made a post about a very interesting video made by Miško Hevery intituled “The Clean Code Talks – Don’t Look For Things!”.
Today, I discovered that there is a PDF summarizing the important principles, with actually some very detailled examples also.
I really highly suggest to any programmer interested by unit testing to take the time to read this document.
It can be found on Miško Hevery’s website or you can directly download the pdf from this shortcut.
Here is a summary of the content of that pdf (this is taken directly from Miško Hevery’s website).
Flaw #1: Constructor does Real Work
Warning Signs
- new keyword in a constructor or at field declaration
- Static method calls in a constructor or at field declaration
- Anything more than field assignment in constructors
- Object not fully initialized after the constructor finishes (watch out for initialize methods)
- Control flow (conditional or looping logic) in a constructor
- Code does complex object graph construction inside a constructor rather than using a factory or builder
- Adding or using an initialization block
Flaw #2: Digging into CollaboratorsWarning Signs
- Objects are passed in but never used directly (only used to get access to other objects)
- Law of Demeter violation: method call chain walks an object graph with more than one dot (.)
- Suspicious names: context, environment, principal, container, or manager
Flaw #3: Brittle Global State & SingletonsWarning Signs
- Adding or using singletons
- Adding or using static fields or static methods
- Adding or using static initialization blocks
- Adding or using registries
- Adding or using service locators
Flaw #4: Class Does Too MuchWarning Signs
- Summing up what the class does includes the word “and”
- Class would be challenging for new team members to read and quickly “get it”
- Class has fields that are only used in some methods
- Class has static methods that only operate on parameters
A friend and I have been working on different projects together. We recently decided to share one of our script to the open source community.
The name of the tool is the Autoload Manager.
The AutoLoad Manager is a generic autoloader that can be used with any PHP framework or library. Using the PHP tokenizer mechanism, it will parse folder(s) and discover the different classes and interfaces defined. The big advantage of using this autoloadManager is that it will allow you to implement whatever naming rules you want and may have mutliple classes in one file (if you want to have a such feature).
So basically, you don’t have anymore to implement some complex naming rules between the filename and the classname. You may organize the way you want your API (may have as many subfolders as you want, you may have multiple API folders, etc.).
Would you like to know more about it ? Then, check the documentation here.
My name is Bashar Al-Fallouji, I work as a Enterprise Solutions Architect at Amazon Web Services.
I am particularly interested in Cloud Computing, Web applications, Open Source Development, Software Engineering, Information Architecture, Unit Testing, XP/Agile development.
On this blog, you will find mostly technical articles and thoughts around PHP, OOP, OOD, Unit Testing, etc. I am also sharing a few open source tools and scripts.