Globally Scoped

July 14th, 2013

Global variables are a code smell. This statement is both true and false. What is true is that they are a problem when they are no longer the right variables to be global.

Today I had to re-factor a function to be able to use multiple servers inside one script instance. Initially, the server connection was created and stored in one variable that was then referenced. This worked well, because the code that called this function didn’t have to worry about what server connection was involved.

Of course, the problem started when I added a second server into the mix. Now, this function had to be able to access either server, selected by the code in some of the places that called it. The easiest way to solve this would have been to keep the global and add a second optional parameter to override it, but that would not be as clean a fix as just fixing the global.

And so I re-factored the function call to pass in the server connection to use. This makes the function more flexible, because I can add new servers to the mix and just pass in the variable for the one I want to use. The other benefit is that in eliminating the global, this function is now finally completely self-contained, making it that much easier to test and fix.

Lesson Learned
Globals can be useful, but will make it harder to properly test a function, and complicates matters if the function that uses them is widely used and has to be changed.

Choose carefully how to use them.

Problem Solving

July 13th, 2013

Yesterday’s lesson was that the better solution to a problem usually only becomes obvious when you’re almost done solving the problem.

PHP Heatmap Gradient Color Generator

December 25th, 2011

Based on the awesome work done here Samson PHP Color Gradient Generator, I have packed up his code into two functions that are by default set to generate heatmap blue to red color gradients, in as many steps as you need.

https://gist.github.com/bendauphinee/10472641

[php]function heatSteps($config){
$config[‘colorStart’] = (isset($config[‘colorStart’])) ? hexdec($config[‘colorStart’]) : 0xdee7f8;
$config[‘colorEnd’] = (isset($config[‘colorEnd’])) ? hexdec($config[‘colorEnd’]) : 0xff0f15;
$config[‘colorSteps’] = (isset($config[‘colorSteps’])) ? $config[‘colorSteps’] : 10;

$config[‘colorStart’] = (($config[‘colorStart’] >= 0x000000) && ($config[‘colorStart’] <= 0xffffff)) ? $config[‘colorStart’] : 0x000000;
$config[‘colorEnd’] = (($config[‘colorEnd’] >= 0x000000) && ($config[‘colorEnd’] <= 0xffffff)) ? $config[‘colorEnd’] : 0xffffff;
$config[‘colorSteps’] = (($config[‘colorSteps’] > 0) && ($config[‘colorSteps’] < 256)) ? $config[‘colorSteps’] : 256;

$theR0 = ($config[‘colorStart’] & 0xff0000) >> 16;
$theG0 = ($config[‘colorStart’] & 0x00ff00) >> 8;
$theB0 = ($config[‘colorStart’] & 0x0000ff) >> 0;

$theR1 = ($config[‘colorEnd’] & 0xff0000) >> 16;
$theG1 = ($config[‘colorEnd’] & 0x00ff00) >> 8;
$theB1 = ($config[‘colorEnd’] & 0x0000ff) >> 0;

$colorSteps = array();
for($i = 0; $i <= $config[‘colorSteps’]; $i++){
$theR = interpolateHeatSteps($theR0, $theR1, $i, $config[‘colorSteps’]);
$theG = interpolateHeatSteps($theG0, $theG1, $i, $config[‘colorSteps’]);
$theB = interpolateHeatSteps($theB0, $theB1, $i, $config[‘colorSteps’]);

$colorSteps[] = dechex(((($theR << 8) | $theG) << 8) | $theB);
}

return($colorSteps);
}

function interpolateHeatSteps($pBegin, $pEnd, $pStep, $pMax){
if ($pBegin < $pEnd){
return(($pEnd – $pBegin) * ($pStep / $pMax)) + $pBegin;
}else{
return(($pBegin – $pEnd) * (1 – ($pStep / $pMax))) + $pEnd;
}
}[/php]

The Reddit pi Icon and :hover

October 19th, 2011

Today I had a task come up on my list that I’ve been interested in digging into for a while. After seeing the reddit π icon, and learning that it’s awesome, as well as reference to the movie The Net, I wanted to make use of the idea to hold some info on page generation times and a few other things useful as a dev on a site I’m working on.

So, digging in with Chrome’s developer tools, I looked into figuring out how to do it. At first, I thought it was a Javascript thing, using jQuery and mouseOver events, because that’s how it could have been done. However, that seemed a little heavy for this purpose, since it’d require some Javascript be loaded on each load. So, examining it a bit closer, I dug into the CSS, and discovered a new use of the pseudo property :hover.

Today I learned that you can use :hover on a parent element to change properties on a child element. Of course, it makes perfect sense now to use it like that, but it was one of those Ooohhhh! moments that you get when something obvious smacks you in the face.

Here’s one way to do the reddit π icon:
[code]
<style type="text/css">
#pibottom{float: right; padding-top: 4px;}
#pi{color: #a0a0a0; font: 16pt serif; padding: 2px;}
#picontent{font-size: 11pt; display: none;}
#pibottom:hover #picontent{display:inline;}
</style>

<br clear="all">
<div id="pibottom">
<span id="pi">&pi;</span>&nbsp;<span id="picontent">Load in ‘ . getScriptTiming() . ‘ sec. Ran ‘ . getCountDBQueries() . ‘ queries.</span>
</div>
[/code]

jQuery Pagination Top And Bottom

March 10th, 2011

Thanks goes to VNSMANIAN2006 for the code for a quick top and bottom pagination element with jQuery Pagination. The original note can be found here: http://plugins.jquery.com/node/8360#comment-4157

And here is how I used it:
[code]
$(document).ready(function(){
$.get("ajax.php?getcount=1", {}, function(data){
$("#pagination").pagination(data, {
num_edge_entries: 2, num_display_entries: 6, items_per_page: 20,
next_show_always: false, prev_show_always: false,
callback: function(page_index, jp){
$.get("ajax.php?p="+page_index, {}, function(data){
$("#paginationb").empty();
$("#pagination").clone(true).prependTo("#paginationb");
$("#reportInfo").empty().append(data);
});
return false;
}
});
});
});[/code]

And all I did was add the #paginationb div below the table.

WordPress - Entries (RSS) and Comments (RSS) - © 2011 Ben Dauphinee