Never stop challenging yourself
As a developer, its super easy to fall into a rythym and do the same things, the same way, forever. This doesn’t help make you a better developer, in fact, the least skilled and hardest developers to work with are generally the guys who are closed off to the prospect of ‘a better way’.
You don’t need to work in a team to challenge yourself on approaches to problems, design styles, methodologies or even small, tiny, one-line code changes. Of course, working around other developers who you respect makes a huge difference and can be extremely beneficial to not only your technical knowledge but your ability to stay open minded and accept that your answer is quite possibly not the best one for the current problem.
I think that a lot of developers who work solo, especially in their first few years of their career, can get caught in an ‘I know best’ rut and are not willing or open to different points of view.
If you want to get better, you need to be challenged and that can be as mentally draining as a brain storming session, or as easy as writing a small script to test the differences between coding technicques or function calls.
Today, while working with my boss who I admire and respect for his technical prowess and general speed of uptake, I noticed that he takes a slightly different approach when using PHP variables in a MySQL INSERT query. In this particular scenario, there is a slight possibility that the variable will occasionally be an empty string which would cause syntax errors in the query. My boss wrapped the variable in an intval() call to default the value to 0 in case the string was empty. This was far different from my approach which would always to use a shorthand if statement to test the string and assign a value.
The intval() call in this scenario looks more elegant, but the shorthand statement is just something I have always done. I decided to put the two to the test by writing an unnecessary test on speed.
The first test used a shorthand statement with no function call;
<?php
$str = '';
$func_total = 0;
$sh_total = 0;
for ($i = 0; $i <=1000; $i++) {
$func_start = microtime(true);
$val = intval($str);
$func_total += (microtime(true)-$func_start);
$sh_start = microtime(true);
$val = ($str? 0 : 1);
$sh_total += (microtime(true)-$sh_start);
}
print 'Function call total: ' . $func_total . PHP_EOL;
print 'Short hand total: ' . $sh_total . PHP_EOL;
Function call total: 0.00370979309082
Short hand total: 0.00205898284912
Shorthand is quicker.
And, the second used an strlen() call;
<?php
$str = '';
$func_total = 0;
$sh_total = 0;
for ($i = 0; $i <=1000; $i++) {
$func_start = microtime(true);
$val = intval($str);
$func_total += (microtime(true)-$func_start);
$sh_start = microtime(true);
$val = (strlen($str) > 0 ? 0 : 1);
$sh_total += (microtime(true)-$sh_start);
}
print 'Function call total: ' . $func_total . PHP_EOL;
print 'Short hand total: ' . $sh_total . PHP_EOL;
And was still quicker;
Function call total: 0.00477647781372
Short hand total: 0.00359439849854
The test, as you can see, just simply executes the code 1000 times and records a total time elapsed for each one. Its silly, but was purely for my own curiosity. While the shorthand is faster, the intval() call is much nicer and I will likely opt to use it in future.
This is just one simple, quick method that challenges yourself and your methods for writing code. If you dont challenge yourself and let others challenge you, you have no hope of ever becoming a better developer.
