Copy and paste is a design error.
David Parnas

Block Comment

We all know what a block comment is, sometimes we use it to comment a bunch of lines of code. And sometimes we might want those lines of code to be uncommented and commented again. In a traditional way, we need to make two modifications each time, at the beginning and at the end of block comment symbol.

For example:

usual_code();

sometimes_unused();
sometimes_unused_too();

another_code();

If we want to comment some lines:

usual_code();
/*
sometimes_unused();
sometimes_unused_too();
*/
another_code();

We modified two spots. Now, I prefer to do just one modification each “toggle” (comment-uncomment switch), so I use this:

usual_code();
/* Some description
*/
sometimes_unused();
sometimes_unused_too();
/**/
another_code();

To comment them:

usual_code();
/* Some description
     <-- only need to delete this part, one spot
sometimes_unused();
sometimes_unused_too();
/**/
another_code();

More efficient. IMHO.

May 25th, 2011 Programming Tags: , , 1 Comment 394 views

PHP Code: Rows to Tree

Have you ever encounter a case where you have a database table which represents a tree, like this:

CREATE TABLE `node` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `parent_id` int(10) unsigned DEFAULT NULL,
 `name` varchar(100) NOT NULL,
 `other` text NOT NULL,
 PRIMARY KEY (`id`)
)

Now when working with this table, usually we need to transform these rows into a tree. For example, let’s say you get this when executing `SELECT * FROM node`: Read the rest of this entry »

January 27th, 2011 PHP Tags: , , , , , 1 Comment 682 views