It was the start of summer and Stephen was almost as excited about college junior Nathan's internship as Nathan was. Stephen’s company had a history of selecting new employees from their small group of fresh interns that started each summer and Nathan seemed like the right guy for the position.
To get his feet wet, Nathan would be first assigned to the SEO group where Stephen would be overseeing Nathan’s work. Though the SEO team was small when compared to the rest of the web development group, Nathan was in a position to make a big impact on driving traffic to the sites that Stephen’s company maintained and add a nice set of bulleted items to his resume or, if things worked out, a full time job after graduating from college.
On Nathan’s first day, Stephen and his manager carefully explained to him some of the problems faced by the company.
"We have millions of users, but the search engines don't know that. We show just enough information about each user publicly so that when you Google for this person, you can find their profile on our site. For this to be truly effective, however, we need a user directory the search engines can crawl."
"For each locale, just throw up a page listing all usernames in that locale alphabetically ordered and linked to their profiles." Stephen’s manager continued, "We serve pages in over a dozen languages, so just let standard libraries figure out what 'alphabetically' means.”
Stephen capped the conversation by explaining, “You don't need to worry about how it looks-- the crawlers won't care. This is really just to get you familiar with the codebase and the way we do things here before to get you ready for your real project."
Later, once Nathan got settled into his, Stephen began explaining some of the basics of their architecture, gave a refresher on SQL, being sure to highlight the use of the "ORDER BY" clause, and told Nathan to be sure to drop by if he needed anything.
The next day, Nathan spent the day browsing through code running in production, but found himself confused by the mixing of different paradigms. He found a potpourri of heavily object-oriented code and fancy UI libraries, to endless seas of HTML string concatenation and hard-coded FAQs. Stephen explained to Nathan about ongoing efforts to rewrite some of the poor code, majority of which was inherited from clients. He offered to go into more detail about current best practices later that day, but Nathan, in his words, was “on top of this one”.
He knew what methods worked for him after several years in a well-known computer science program, and knew doing things his own way would continue to serve him well. He did have a 3.9 average after all.
After working through the weekend, Nathan proudly showed his results four days later.
$letters = range('A', 'Z');
$locale_arr = array('ca_es', 'cs_cz' , 'da_dk', 'de_de', 'en_gb', 'en_us', 'es_la', 'es_es', 'fr_ca', 'fr_fr', 'nl_nl', 'ja_jp', 'ru_ru', 'he_il', 'fa_ir');
foreach ($locale_arr as $locale) {
$locale_str = substr($locale,0,3).strtoupper(substr($locale,3,2));
exec("mkdir /user_dir/".$locale);
$output_path = '/user_dir/'.$locale.'/'.$locale;
$line = sprintf('mysql -e "SELECT * FROM users WHERE locale=\'%s\'" > %s &', $locale_str, $output_path);
exec($line);
//open 26 files to write to (A to Z)
foreach ($letters as $l) {
$seperate_var_temp = 'handle'.$l;
$$seperate_var_temp = fopen('/user_dir/'.$locale.'/'.$l, "w");
}
$inp = fopen($output_path, "r");
while($line = trim(fgets($inp))) {
list($name, $locale) = explode("\t", $line);
$line_output = $name."\t".$locale."\n";
if (preg_match('/^a/i',$line)){
fwrite($handleA,$line_output);
} else if (preg_match('/^b/i',$line)){
fwrite($handleB,$line_output);
} else if (preg_match('/^c/i',$line)){
fwrite($handleC,$line_output);
} else if (preg_match('/^d/i',$line)){
fwrite($handleD,$line_output);
} else if (preg_match('/^e/i',$line)){
fwrite($handleE,$line_output);
} else if (preg_match('/^f/i',$line)){
fwrite($handleF,$line_output);
} else if (preg_match('/^g/i',$line)){
fwrite($handleG,$line_output);
} else if (preg_match('/^h/i',$line)){
fwrite($handleH,$line_output);
} else if (preg_match('/^i/i',$line)){
fwrite($handleI,$line_output);
} else if (preg_match('/^j/i',$line)){
write($handleJ,$line_output);
} else if (preg_match('/^k/i',$line)){
fwrite($handleK,$line_output);
} else if (preg_match('/^l/i',$line)){
fwrite($handleL,$line_output);
} else if (preg_match('/^m/i',$line)){
fwrite($handleM,$line_output);
} else if (preg_match('/^n/i',$line)){
fwrite($handleN,$line_output);
} else if (preg_match('/^o/i',$line)){
fwrite($handleO,$line_output);
} else if (preg_match('/^p/i',$line)){
fwrite($handleP,$line_output);
} else if (preg_match('/^q/i',$line)){
fwrite($handleQ,$line_output);
} else if (preg_match('/^r/i',$line)){
fwrite($handleR,$line_output);
} else if (preg_match('/^s/i',$line)){
fwrite($handleS,$line_output);
} else if (preg_match('/^t/i',$line)){
fwrite($handleT,$line_output);
} else if (preg_match('/^u/i',$line)){
fwrite($handleU,$line_output);
} else if (preg_match('/^v/i',$line)){
fwrite($handleV,$line_output);
} else if (preg_match('/^w/i',$line)){
fwrite($handleW,$line_output);
} else if (preg_match('/^x/i',$line)){
fwrite($handleX,$line_output);
} else if (preg_match('/^y/i',$line)){
fwrite($handleY,$line_output);
} else
fwrite($handleZ,$line_output);
}
}
//close the 26 files opened
foreach ($letters as $l) {
$seperate_var_temp = 'handle'.$l;
fclose($$seperate_var_temp);
}
foreach ($letters as $l) {
$sorted_file_name = '/user_dir/'.$locale.'/'.$l;
exec('sort '.$sorted_file_name.' > '.$sorted_file_name.'_sorted.txt');
exec('rm /user_dir/'.$locale.'/'.$l);
}
foreach ($letters as $l) {
$fname = $l.'_sorted';
$conn = get_db_conn();
exec(sprintf('mysqlimport -h db.company.com -u company_ceo --password=ceo_password users_%s %s', $locale_str, '/user_dir/'.$locale_str.'/'.$fname.'.txt'));
}
}
Several weeks later, despite Stephen’s best efforts, Nathan was still working on Stephen's requested improvements, his way, and Stephen could hardly wait for the summer to end.