Hackers and Painters

Here’s an article published by Paul Graham in 2003 that’s worth reading.

“I’ve never liked the term “computer science.” The main reason I don’t like it is that there’s no such thing. Computer science is a grab bag of tenuously related areas thrown together by an accident of history, like Yugoslavia. At one end you have people who are really mathematicians, but call what they’re doing computer science so they can get DARPA grants. In the middle you have people working on something like the natural history of computers– studying the behaviour of algorithms for routing data through networks, for example. And then at the other extreme you have the hackers, who are trying to write interesting software, and for whom computers are just a medium of expression, as concrete is for architects or paint for painters. It’s as if mathematicians, physicists, and architects all had to be in the same department.”

Continue reading: Hackers and Painters

No need for set/get methods in Python

Python code doesn’t typically use the get and set methods so common in PHP. Normally, when writing PHP code, you carefully protect your instance variables by making them private, so callers can only interact with them via getter and setter methods. For example:

class Book {
  private $title;

  public function setTitle($title) {
    $this->title = $title;
  }

  public function getTitle() {
    return $this->title;
  }
}

$book = new Book;
$book->title = 'Code Complete';

However, in PHP, the code above returns the following error:

Fatal error: Cannot access private property Book::$title

Python’s solution to this problem is more readable, it has a construct called a “property”. Properties are a neat way to implement attributes whose usage resembles attribute access, but whose implementation uses method calls. These are sometimes known as “managed attributes”. Basically, a property is a way to make a function call look like an instance variable reference. For example:

class Book {
  private $title = property(getTitle, setTitle);

  public function setTitle($title) {
    $this->title = $title;
  }

  public function getTitle() {
    return $this->title;
  }
}

$book = new Book;
$book->title = 'Code Complete';

Whenever someone writes $book->title, they’re really calling $book->getTitle(). Although the class has getters and setters, the callers of the class still get to use the original, simpler, easier-to-read syntax for accessing the value.

This way you get the best of both worlds: clean and simple client access to your class, and protection and flexibility within the class.

Examples

Python (run code)

class Book(object):

    def get_title(self):
        return self._title

    def set_title(self, title):
        self._title = title

    title = property(get_title, set_title)

Book.title = 'Code Complete'

PHP (run code)

class Book {
  private $title;

  public function __get($property) {
    $method = 'get' . ucfirst($property);
    if (!method_exists($this, $method)) {
      throw new Exception('No such method: ' . $method);
    }
    return $this->$method();
  }

  public function __set($property, $value) {
    $method = 'set' . ucfirst($property);
    if (!method_exists($this, $method)) {
      throw new Exception('No such method: ' . $method);
    }
    $this->$method($value);
  }

  public function setTitle($title) {
    $this->title = $title;
  }

  public function getTitle() {
    return $this->title;
  }
}

$book = new Book;
$book->title = 'Code Complete';

As you can see in the example above, PHP allows you to do something similar using the __get and __set methods, but can be a bit tricky to get right. Python’s “property” construct lets you do this painlessly.

Microsoft Enviroment video reveals datacenter server numbers

A recent promotional video produced by Microsoft’s Environmental Sustainability group revealed how many servers Microsoft actually has.

Some quick stats gathered from the video: 15 datacenters hosting 148,357 servers sitting on 17,406 racks consuming 72,500KW of utility power as of the end of January 2008. If you’re a bit of a imperial measurement person, that’s 97,000 horsepower.

How variable naming can significantly impact your teams productivity

What does kunnr mean? Anybody? If you’ve worked with SAP than you might know but to everyone else good luck. What about rqnb? Does it mean anything to you? Isn’t it obvious to you that rqnb represents a request number? Seriously though, why do people name their variables so cryptically?

Think about it a little, how much easier would the code be if the variable was named requestNumber instead of rqnb? And for those of you who think it will save a few keystrokes, hence make your typing faster, remember that each time you use the variable you have to remember how to abbreviate it. With one variable it’s not so bad, but when you have dozens, you’re always looking up the different variable names.

Continue reading: How variable naming can significantly impact…

Every method must have 10 lines of code or less

The key idea is not short methods nor few methods per class. The key idea is Conceptual Singularity. The class has one and only one purpose. All methods support a single, singular purpose and nothing else.

When a class is singular, it tends to be “small” simply because every line you add potentially disrupts the Conceptual Singularity of the class. A “small” class tends to have fewer methods, again, simply because every method you add increases the probability of contaminating the singular purpose. Methods tend to be short for the same reason.

Advantages

  1. Easier to test.
  2. Easier to reuse.
  3. Easier to modify.
  4. Less bugs are discovered on short methods and on short classes.
  5. Development team codes faster, because there is less need for refactoring.

Experiment

Take a relatively small class and go merciless on it. Refactor it as much as you possibly can. When you’re done, look at any class that has multiple member variables in it. Can any one of the member variables exist separately from the others? Split off that member variable into it’s own class (even it seems absurd to you). Can the methods be partitioned into methods that work on variable A and methods that work on variable B? Split the sets of methods into two classes. Can any part of the class be made more abstract? Extract out the abstract part into a new class. Continue doing this until you can’t refactor any more. Split the class up based on the functionality. When you’re done, you will have singular purposes classes. Repeat this experiment over and over again. After two years go back and take another look at your first attempt. Split the classes up even further on what you now see.

Source: c2.com

Using PHP_CodeSniffer

PHP_CodeSniffer is a library that tokenises and “sniffs” PHP code to detect violations of a defined set of coding standards. It is an essential development tool that ensures that your code remains clean and consistent. It can even help prevent some common semantic errors made by developers. Raphael Stolt did a great job porting the TooManyMethods and ExcessiveMethodLength rules to PHP_CodeSniffer. Visit his blog for more information.

Collaboration on steroids

Confluence, the world’s most widely used enterprise wiki, announced expanded integration with Microsoft Office and SharePoint that introduces Confluence to the masses. The latest release includes a host of goodies for business users looking to flex their productivity muscles and improve collaboration.

Confluence 2.9 includes a new alternative that makes editing a wiki page much simpler for everyone. Confluence users can now edit wiki pages directly from Microsoft Excel, Word or PowerPoint, via an exclusive new plugin called Office Connector.

More info: Confluence 2.9 Released

Is PHP an agile programming language?

In raising the question about whether a specific programming language is agile I want to avoid exploring what makes a language agile, or comparing PHP with other languages. The intention here is not to associate PHP to agile as a natural relationship, as much as to try and understand if we can find the ingredients in the PHP world, for creating an agile environment.

Continue reading: Is PHP an Agile Programming Language?