Be A Better Programmer

Aaron Swartz posted an article today on his blog titled: How I Hire Programmers. If you haven’t done so already, read it!

I believe this article is not only for employers looking for a way to hire better programmers. But, it could also be beneficial for an aspiring programmer to work on themselves to be a better candidate for the job.

Below, I’ve picked out a couple quotes and added my response.

“Someone who gets stuff done but isn’t smart is inefficient: non-smart people get stuff done by doing it the hard way and working with them is slow and frustrating.”

Anybody can Google around and find a code snippet, paste it in, and continue working. However, do you know what that snippet of code is doing? Do you know why that block of code works, and how it works? You need to.

Whenever I run into something I don’t know how to solve, I’ll Google it. I’ll find a snippet of code or a tutorial and read through it. I’ll pick it apart, look up function calls, comment the code and even completely rewrite it. Line by line I’ll figure out a better way it could have been written. I will make it mine. You should do the same.

“… do they learn? At some point in the conversation, you’ll probably be explaining something to them. Do they actually understand it or do they just nod and smile? There are people who know stuff about some small area but aren’t curious about others. And there are people who are curious but don’t learn, they ask lots of questions but don’t really listen. You want someone who does all three.”

With the technology of today, always changing and new things being introduced, you can’t afford not to learn something new. Are you intrigued by the API’s and Web Services available? If you’ve never used it before, use it now! Make a simple script, just to test it out. Do that, and you will become a better programmer.

Do any of these sound like you? Take a look at yourself and these points from the article. See how you can make yourself a better programmer.

Don’t Hard Code Your Age!

Even though this is not at all as important as not hardcoding copyright years. It can still a time saver if you happen to hard code your age into a biography on a website.

Lets make those ages DYNAMIC!

I wrote a little code snippet which updates your age automatically. All you do is enter your birth date and php does the rest! I will use my birthday in the example:

$birth_date = getdate(strtotime("August 1st 1988")); $current_date = getdate(time()); echo ($current_date['mday'] >= $birth_date['mday'] AND $current_date['mmonth'] >= $birth_date['mmonth']) ? ($current_date['year'] - $birth_date['year']) : (($current_date['year'] - $birth_date['year']) - 1);

This would return 21, which is how old I am currently. If the current time was July 31st 2009, it would return 20. I made sure it checked both the day and month, not just subtract the years, to figure out the date. Just replace “August 1st 1988″ with your birth date.

It’s that Simple.

CodeIgniter’s alternator() function


It’s surprising to me how often I find little functions for tedious tasks, that CodeIgniter already has built in. One of these functions is the alternator() function in the String Helper.

To begin using this function, make sure you have loaded the String Helper with the following code:

$this->load->helper('string');

What the alternator() function does is allow two or more items to be alternated between when iterating through a loop. Example from the CodeIgniter User Guide:

for($i = 0; $i < 10; $i++)
{
	echo alternator('string one', 'string two');
}

There is also no limit to how many parameters you can have:

...
echo alternator('one', 'two', 'three', 'four', 'five');
...

Put it to Use

What would you ever need that for? Well, what about if you are creating a list of items and every other needs class="alt" attached to it for styling differences? I run into this issue all the time.

This is how I used to do it:

<ul>
	<?php $count = 1; ?>
	<?php foreach($list as $item) : ?>
		<?php (empty($count)) ? $count = 1 : $count = 0; ?>
		<li <?=($count == 1) ? 'class="alt"' : ''?>>
			<?=$item?>
		</li>
	<?php endforeach; ?>
</ul>

And this is with the alternator() function:

<ul>
	<?php foreach($list as $item) : ?>
		<li <?=alternator('class="alt"', '')?>>
			<?=$item?>
		</li>
	<?php endforeach; ?>
</ul>

The alternator() function makes the ability to do this, much easier and cleaner than my original way. Hopefully I’ve helped someone out who had no idea this function was available.

Limit Words in a String

While developing websites, I’ve frequently run into clients who would like a news system integrated on their site. A news system consists of a list of articles, usually a summary at first, and when an article has been selected, the full article is displayed. So, how do you extract the summary from the content without duplicating content?

If you wanted to do this effect quickly, you could just use the function substr(). However, the substr() function only limits the number of characters being displayed. The returned result would be an excerpt of text that may or may not have the ending word cut-off.

The purpose behind this function is to limit the number of words displayed in such a way that the ending word is not cut-off. Personally, I think this small change makes the site look a little nicer.

function limit_words($string, $word_limit)
{
    $words = explode(" ",$string);
    return implode(" ",array_splice($words,0,$word_limit));
}

To use this function, pass the text you would like to extract the excerpt from, as $string. Then, set the number of words you would like to display as $word_limit. The function will return the excerpt as a string.

The function separates the string where it finds a space, therefore separating each word using the explode() function. Each word is put into an array called $words. We then cut out the excerpt using the number of words we would like to display ($word_limit) starting from the beginning using the array_splice() function. With the excerpt extracted from the full text, we then recreate the string by adding spaces after each key (word) in the array using the implode() function.

# Example Usage
$content = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
echo limit_words($content,20);

The above example would output this result:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut

Simple function, Nice results. Enjoy!