Idea Notebook

Like most developers, I have ideas. Some of those ideas are built, some are still bouncing around in my head and that is a productivity killer for me. Constantly having new ideas and constantly thinking about old ideas is distracting. Not only is it distracting, it makes me feel uninspired.

So what did I do? I started building those projects. I would code for hours and hours, trying out new things until I didn’t have the inspiration to continue. At that point I moved onto another idea, repeating the process until I had a dozen incomplete projects with a burst of inspiration baked into each. Still, I am left uninspired.

I needed to change something and fast. I am a minimalist so my instinct was to create a process to keep all my ideas on my Macbook. At first I just created a folder “Ideas” which contained a plethora of plain text files, each explaining the idea in my head. It didn’t take long before I realized I would like to have some sort of wireframe or mockup to give myself a better idea of how it’d look and function.

Eventually, I realized keeping all my ideas on my Macbook wasn’t the best idea. Overflowing with plain-text and Adobe Fireworks files, I trashed it along with all the ideas I had up to that point. I decided to go the traditional pen and paper method and ordered a new Moleskine Squared Notebook and a Uni-ball Gell RT Pen.

Best. Investment. Ever.

I bring my idea book wherever I go, ready to draw or write down any idea I may have pop into my head. Instantly. If I wait or tell myself I’ll write it down later, I’ll forget to. It has to be right away or I’ll fall into the same routine which started it all.

Writing them down

Just like any other day of developing, I’ll get an idea. Whatever the idea, I’ll put everything on hold and I do mean everything. Instant messenger, email, the current project I’m working on, podcast I’m listening to, you get the idea. I push my laptop away from me and grab my idea book, which is conveniently located on my desk next to my Macbook.

Each idea I have goes on a single page and uses at least the front and back of that page. So, if I don’t use the entire page I can always go back and add more to the idea. If I need more space, I use more pages. I title the page with whatever title for the idea pops into my head first. Following the title, I write a quick description of the idea, what I want to see in it, how I envisioned it functioning, etc. Sometimes I’ll even draw a little wireframe to show what I pictured it looking like.

Close idea book; continue doing what I was doing.

Simple, but it takes self-discipline. It’s easy to get distracted from writing the idea down when you have much more important things to do. Isn’t not getting the idea out of your head the distraction from doing the work you should be doing? That is the case for me. Do it. You’ll be glad you did.

So why the Moleskine Squared Notebook? I like to draw straight lines and write on straight lines. That’s all. Find what is optimizing for you. After all, that’s the minimalist lifestyle.

Building an Idea

Ideas are cheap, which is why I keep them all in my notebook. Cheap is good, so stock up as much as you can, right? Ideas can be evaluated later as to the greatness of itself so don’t worry about that when first writing them down. When evaluating those ideas, don’t just ask yourself what you think of the idea, share the idea with others! What do others think? After hearing other people’s thoughts, you can form your own opinion on the idea. Does it really suck? People may seem to think so. Then again, they could just be shortsighted in the opportunity.

Once you’ve evaluated your ideas, decide on just one that you (and probably others) are really excited about. Start building it. If you run out of juice after hours of programming, take a break. It’ll be there waiting for you to pour some more passion into it later. When you come back, work harder. You’ll get past that bump, that little bug you can’t seem to figure out. You’ll be that much closer to finishing it.

Conclusion

We all have our own style and process of gathering and developing ideas. If you don’t have any process in place for gathering ideas, try the method I use. Get pen and paper and carry it everywhere you go. It’s helped me get my ideas down and continues to help me stay focused.

Refactoring in Practice

I love taking larger functions and slimming them down into a more efficient and readable format. Here is a simple example of just that.

The original function:

function strip_slashes( $data )
{
    if( is_array($data) )
    {
        foreach($data as $key => $val )
        {
            $data[$key] = strip_slashes($val);
        }
    }
    else
    {
        return stripslashes($data);
    }

    return $data;
}

The refactored function:

function strip_slashes( $data )
{
    return is_array($data) ? array_map('strip_slashes', $data) : stripslashes($data);
}

The code is much more readable and can be understood easily. Try to do the same with your own functions. The more you practice refactoring old code the easier it will be to remember how to do things quicker.

Directory Listing from a Path

$path = dirname(__FILE__);

$listing = array_filter(scandir($path), function($var) {

	// remove special directories '.' and '..' from listing
	if( preg_match('/^[.]{1,2}$/', $var) ) { return FALSE; }

	// remove files from listing
	if( !is_dir($var) ) { return FALSE; }

	return TRUE;
});

This is a little snippet of code I’ve been using a lot recently. This function will return all directories inside the path passed as $path. The functions utilizes the ability of Anonymous Functions, only available in PHP 5.3.0.

Validate terminal command in PHP

Testing if a function exists in PHP is easy. Testing if a command exists on the system is also easy.

I’ve never really run into the problem of having to create a compatibility suite script to make sure my PHP script will run without trouble. Simple things like checking the version of PHP installed or the version of MySQL are straightforward. This time though, I needed to make sure a command line program existed on the system which I invoke using exec().

First and foremost, we need to check that exec() exists and we can use it. If it does, we then need to use it to execute a command on the server which will output the path to our command. I will be testing for tar in the example.

if( function_exists('exec') )
{
    // send test command to system
    exec('command -v tar >& /dev/null && echo "Found" || echo "Not Found"', $output);

    if( $output[0] == "Found" )
    {
        // command is available
        return TRUE;
    }
    else
    {
        // command is unavailable
        return FALSE;
    }
}

Our focus will be this line:

exec('command -v tar >& /dev/null && echo "Found" || echo "Not Found"', $output);

In the first part of the command we will run command with the -v option. The -v option causes the output of the command to be displayed or return zero if the command is not found. Here’s a short description of the command:

SYNTAX
    command [-pVv] command [arguments ...]
OPTIONS
    -P  Use a default path
    -v  Verbose
    -V  More verbose

The next part of the command we use >& which is a metacharacter in Unix which tells the command to redirect the standard output and standard error. Which in this case, we redirect the output to a file /dev/null. We do this because we want to handle the response of the command with the last part.

The last part of the command we use && which is another metacharacter which tells Unix to execute the following command only if the preceding command succeeds. We also use the || metacharacter which tells Unix to execute the following command if the preceding command fails. To understand it better, it’s just like writing an if-then-else statement:

If( command -v tar >& /dev/null ) Then
    echo "Found"
Else
    echo "Not Found"
End If

Now we need to bring the response back to PHP. We do that with the second parameter of exec(); $output. Every line of output from the command will be returned in $output as an array which we can then run our conditional against.

Short, simple, easy little command. Just replace tar with the command you’d like to check for. You could even take the code and place it into a function to make it easily reusable.