Coding Philosophy and Examples
Code samples often separate the wheat from the chaff, the obtuse programmer from the obvious one.
Unfortunately, it also separates the code-flexible from the code neutral, and without background, people who are used to programming in a variety of styles may suffer from criticism of not coding in the house style.
For an example in the ("lets make it possible to do it every way, what could go wrong"?) PHP language:
$imagepile = mysqli_query($pics_connection, 'select name from images where last_check <= DATE_SUB(CURDATE(),INTERVAL 30 DAY) order by last_check ASC LIMIT 80;');
....is a "one-liner", totally acceptable in some bash/shell focused environments, but not in others.
However, this is the exact same PHP/C/SQL code:
$imagepile = mysqli_query
(
$pics_connection,
'SELECT name from `images`
WHERE
last_check <= DATE_SUB
(
CURDATE
(
),
INTERVAL 30 DAY
)
ORDER BY last_check ASC
LIMIT 80
;'
);
...written in another indent and capitalization style. Legible to some, awful to others.
A helpful workflow tendency has been to use "auto-documentation":
/*
* @Author: $foo
* @LastEdit: $bar
* @BugId: $baz
* @TestId: $quixl
* @arguments: $connection_handle
* @returns: $result_handle
* @notes: This runs a mysqli query with an argument to get pictures within a certain range
*/
$imagepile = mysqli_query($pics_connection, 'select name from images where last_check <= DATE_SUB(CURDATE(),INTERVAL 30 DAY) order by last_check ASC LIMIT 80;');
Well, this is another way of writing code...
..of course, even with all of the above, it's not immediately clear whether pictures older, or newer, than 30 days are being "last_check"'ed for, unless a programmer understands date_sub(), curdate() and interval.
Let's take another frequent whipping post, the undocumented regular expression:
$info = preg_replace('/(\$[0-9.]*)[ ]*\([A-Z]\)/','$1',$info);
"Old Hands" at regex can mentally parse this instantly, but somebody new to maintaining the code may greatly benefit from a brief explanation:
/*
The pricing codes look like:
$5 (Z), $30 (V), $17.50 (V)
So:
(\$[0-9.]*) //look for $ immediately followed by digits or a period
[ ]* //followed by one or more spaces
\([A-Z]\) //Followed by a price code of the form (V),(K),etc.
*/
Similarly, some folks comment every 3 lines, some comment every line, some comment for every function/operation.
It's really a matter of house style. If I'm given a "house style", I can code in it. //assertion of ability.
Let's take a fairly universal idea, the "code that launches other code, based on arguments", a.k.a. a "factory". This used to be the way it was done:
PHP 3:
function dynamic_code_loader($file){
include($file);
}
As PHP adopted more language idioms, it matured, and used different metaphors, to suit the programmer and/or style, for example:
PHP 4/5:
class Builder {
function build($object){
include_once($object . '.php');
}
}
$robot = new Builder;
$robot ->build('objects/fish');
$robot2 = new Builder;
$robot2 ->build('objects/bicycle');
Here's a different sample of working within style goals, for a ruby/Rails/MERB (they used both) house:
#We're using spidr to shortcut having to sort out CSS and js
require 'spidr'
#let's open a handle for utility purposes
require 'mysql'
dbh = Mysql.real_connect('localhost', 'root', 'widget', 'cache')
Spidr.site('example.com') do |spider|
spider.every_page do |page|
#links found, lets output them for visual feedback
puts page.links
# This is a Nokogiri::HTML::Document
text = page.doc
#convert to string
str_text = text.to_s
#escape for insertion
escaped_text = dbh.escape_string(str_text)
#links found, lets output them for visual feedback
puts page.url
code = page.code.to_s
url = page.url.to_s
head_array = Array.new
runtime = String.new
# puts YAML::dump(page.headers)
page.headers.each_pair do |key, value|
head_array << "#{key}: #{value}"
#calculate runtime later
# if key == 'x-runtime'
# runtime = value
# end
end
# puts runtime
header = head_array.join("\n")
dbh.query("
INSERT INTO raw_data
(url, header, data, response_code, modified, added)
VALUES
('" + url + "','" + header + "','" + escaped_text + "','" + code + "',now(), now())
")
# puts header
sleep(1)
end
end
Here's a different, simple, dumb, cache routine, written in a PHP4/5 style for raw speed, without cURL/HTML parsing:
/* Global cache management */
function cache_fetch ($item_to_fetch){
$cache_name = md5($item_to_fetch);
if (file_exists("cache/$cache_name") && (time() - filemtime("cache/$cache_name") < 259200) ){ //72 hours
if ($string = file_get_contents("cache/$cache_name")){
return $string;
}else{
return false;
}
}else{
return false;
}
}
In summary, If I'm asked for 'code samples' blind, I can easily have too little/much indenting, commenting, nesting, etc.
The deeper purposes of asking for code samples are three-fold:
1) Can this person write in house style?
The answer? Yes.
2) Can this person understand, and adapt to, the existing in-house style(s)?
The answer? Yes
3) Can this person handle fairly complex, clever, and difficult code?
For this, the answer is harder to find in code samples. Instead, I'll point out that I've maintained and refactored code ranging from single file shell scripts, to ~280,000 files (with millions of LOC) websites and software stacks, and have been doing so for most of my career.