Rework

Honestly I was really skeptic while seeing many excited reviews about “Rework” here an there. It all looked like another wave of hype around Ruby on Rails creators. Until a good friend of mine presented me this book.

Although it was collecting dust on my desk for a while, I am finally among those who read this book. 🙂 A-a-and going to join the the group of amazed and ignited readers too.

First of all it is very easy to read this book. Once you have started, you will be turning one page after another without any thoughts to postpone reading. Part of the reason is that the book consists of really small chapters (2-5 pages) and it is always exciting what next chapter brings.

Second, the book is very well supported by examples of real people and companies. Many examples fit great into the book and in certain cases may be even more inspiring than the main story.

Also it is worth pointing out that “Rework” is not 100% unique content. Some things are already known well enough and even becoming mainstream approaches for doing business rather than being sort of unusual (although that does mean that all big companies follow them yet).

However the uniqueness of “Rework” is that all those valuable recommendations, stories and examples gathered in one book and work perfectly not only for inspiration but as a reference book too.

So in my opinion it’s a kind of ‘must read’ book basically for anyone.

In the end a few quotes from the book (in addition to the provocative ones on the back cover).

“A business without a path to profit isn’t a business, it’s a hobby.”
“Teach and you’ll form a bond you just don’t get from traditional marketing tactics.”
“If you want something done, ask the busiest person you know.”
“Easy is a word that’s used to describe other people’s jobs.”

On investing in employees

Unfortunately I have not been able to find the author of the quote below. However it carries an interesting message about investing in people.

– What happens if we invest in developing our people and then they leave the company?
– What happens if we don’t, and they stay?

MySQL: How to hide queries in the general log

There could be some cases when the general query log has to be kept running. This includes development stage, different flavors of debugging and query analysis and so on. However certain queries do not need to be stored there even though the logging is on. One of the examples is setting user password – it will be logged in clear text.

A possible workaround is to turn off the general logging temporarily. MySQL 5.1 and higher allows to do it without the service restart.

CREATE USER john;
SET SESSION sql_log_off = 1;
SET PASSWORD FOR john = PASSWORD('smith');
SET SESSION sql_log_off = 0;

However SUPER privilege is required to set the session variable sql_log_off.

PHP: Array as the arguments part of the sprintf function

Using sprintf() function is a great way to integrate variables into string templates. It produces nice and readable code. But the drawback is that sprintf() does not support array as the argument part. As result the code may look less readable as number of parameters increases.

In that case vsprintf() is your friend. It works exact the same way as sprintf() except it accepts array instead of separate arguments.

$link = sprintf('<a href="%s">%s</a>', $url, $title);
$params = array($url, $title);
$link = vsprintf('<a href="%s">%s</a>', $params);

A pointer within a pointer

A pointer within a pointer

If for any reason you have not seen Inception yet, you should definitely check it out.

PHP: Daylight Savings Time Support

The ‘old’ way to support time zones in PHP did not have built-in full time zones support. Each time date(‘I’) flag had to be checked to verify daylight savings time period.

For example, to convert local time to GMT could be done the following way.

// time zone difference in hours
$tz = -5;
 
// local date and time
$local = '2011-03-24 11:00:00';
 
// adjust time zone difference in hours
// using daylight savings time flag and convert to seconds
$diff = ($tz + date('I', strtotime($local))) * 3600;
 
// convert to GMT by subtracting $diff number of seconds
$gmt = date('Y-m-d H:i:s', strtotime($local) - $diff);
 
// the output is 2011-03-24 15:00:00
print $gmt;

However utilizing DateTime class included in PHP 5 makes time zone related operations less stressful.

$local = '2011-03-24 11:00:00';
 
// create and instance of DateTime
// using local date/time and its time zone
$date = new DateTime($local, new DateTimeZone('America/New_York'));
 
// change time zone
$date->setTimezone(new DateTimeZone('GMT'));
 
// the date is already converted to GMT
// so the output is 2011-03-24 15:00:00
print $date->format('Y-m-d H:i:s');

So using time zone name instead of actual difference in hours or seconds allows to take care of daylight savings time automatically.

However unlike MySQL CONVERT_TZ() PHP uses different list of time zone names. For example, for Eastern standard time PHP offers ‘America/New_York’ time zone name while MySQL prefers ‘US/Eastern’.

Kitten in Slow Motion

Zend Server JobQueue issue

Zend Server may be a really helpful product and only ZendJobQueue by itself is a great concept for multitasking, queuing or even regular cron replacement. But unfortunately it lacks a good documentation

For instance I have not managed to find anything official about ZendJobQueue as nearly good as Zend Framework documentation. There are some good blog posts and presentations though which were great to start with. But they do not include in-depth details or troubleshooting guide.

In my case I ran into the following error message: ‘Bad response from Job Queue server to a createHttpJob request. QLocalSocket: Remote closed.’ once I tried to go beyond simple test code.

Although Zend Server was up and running and test ZendJobQueue code was working, it looked like everything had crashed really bad…

A quick search showed that someone had already run into a similar Job Queue problem about a year ago. No solution has been posted though. But the fact that my test code still was working helped to address the issue.

So here is my original call which caused the error message showed above.

$params = array('paramValue');
 
$q = new ZendJobQueue();
$q->createHttpJob('/task.php', $params);

And this is the call which works perfectly

$params = array('paramName' => 'paramValue');
 
$q = new ZendJobQueue();
$q->createHttpJob('/task.php', $params);

So apparently the $params array must have keys!
Of course one can tell that simply by looking at the error message but it was not that obvious for the first hour.

P.S.: Zend Server 5.1 Development License.

FogBugz and Kiln World Tour Talk by Joel Spolsky

Joel Spolsky’s talk on the latest features in FogBugz 8 and Kiln 2.
The same awesome talk I was able to attended last October in ATL. He not only showed all great features and benefits of FogBugz and Kiln but made fun of traditional slides and presentations.

PHP: Mac address validating and formatting

There are three common tasks related to storing mac address in the database as char(12) (it is feasible to use bigint instead but in our case we opted for a text type).

1. Validating mac address entered by user.
In our case user is allowed to enter mac address the way s/he wants:
000CF15698AD
00:0C:F1:56:98:AD
00-0C-F1-56-98-AD

So the perfect solution would be to apply regular expression like this one:
/([a-fA-F0-9]{2}[:|\-]?){6}/

For example, in plain PHP:

public static function IsValid($mac)
{
  return (preg_match('/([a-fA-F0-9]{2}[:|\-]?){6}/', $mac) == 1);
}

or using Zend Framework:

public static function IsValid($mac)
{
  $validator = new Zend_Validate_Regex('/([a-fA-F0-9]{2}[:|\-]?){6}/');
  return $validator->isValid($mac);
}

2. Remove separating symbols ‘:’ or ‘-‘ from the mac address to fit only 12 character storage.
This is really easy to implement using str_replace().

public static function RemoveSeparator($mac, $separator = array(':', '-'))
{
  return str_replace($separator, '', $mac);
}

3. Adding separating symbol ‘:’ back when the mac address is displayed to a user.
This could look a bit ugly if the straight forward solution is applied, e.g. something like below:

public static function AddSeparator($mac, $separator = ':')
{
  $result = '';
  while (strlen($mac) > 0)
  {
    $sub = substr($mac, 0, 2);
    $result .= $sub . $separator;
    $mac = substr($mac, 2, strlen($mac));
  }
 
  // remove trailing colon
  $result = substr($result, 0, strlen($result) - 1);
}

So I kept looking for more elegant solution. Ideally I was hoping for one-line solution. And the magic comes with function str_split():

public static function AddSeparator($mac, $separator = ':')
{
  return join($separator, str_split($mac, 2));
}

« Previous PageNext Page »