Archive for the 'Programming' Category

Professional Development, Programming

On Becoming a Better Programmer

Nick Halstead runs a programming blog and is one of my favorite reads. His insight is very good and he has a lot of experience. His post today was about becoming a better programmer and was a very good read. I totally agree with what he was saying, but I had something to add. I think we would all write better code if we had this in the back of our minds:

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. –Martin Golding

Javascript, Programming

How To Identify a Javascript Keycode

I recently made some modifications to a client’s website that involved using key presses to trigger certain events. I had the numeric keycode for a few of the keys that were already in use, but I didn’t really have a good way of identifying the keyCode for the additional key events I wanted to make use of.

Googling for the result left me with a lot of links that had incorrect information. I searched a little more and found a small Javascript snippit that was supposed to tell you what the keycode was when you pressed down a key, but I found it to be incomplete.

I took that snippit and expanded it to work in IE and Firefox. I’ve included the source here, but you can also check out the working demo.

Here is the source for my little keycode function. Keep in mind that I am using jQuery to update the input box with the correct keycode value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html>
<head>
<title>Demo :: How To Identify a Javascript Keycode</title>
<script type="text/javascript" src="jquery-1.2.3.js"></script>
<script type="text/javascript">
function keyCheck(evt){
	key = (evt.which) ? evt.which : evt.keyCode
	$("#keyCode").val(key)
	//alert("KeyCode = "+key)
}
</script>
</head>
<body onKeyPress="keyCheck(event)">
	<p>Press any key to see it's numeric keycode.</p>
	<p>Numeric Key Code: <input type="text" length="5" id="keyCode" name="keyCode"/></p></body>
</html>

Internet, Programming

WordPress 2.5 Rocks

I just blindly upgraded this site to WordPress 2.5 and wow. I figured it would be a minor upgrade, but there is a completely revamped dashboard and admin backend to this thing that is stellar. Great job guys, I am really loving the changes.

Programming, Python, Ruby on Rails

Diving Into Python & Ruby

I’ve got a job application in with a company I used to work for as a developer and the primary technologies we’ll be using are Python and Ruby on Rails. I’ve got a bit of experience with both, but nothing recent. With that said, I’ve really been taking a closer look at what those languages offer and I’m quite excited about getting to build some large scale applications in both of them.

I’m currently working on a basic bank account/ledger application in RoR and some simple pages that emulate the pages of Snackbar Games as a learning experience in Python. I’ve spent the last 5-6 years in PHP and while I love the language because I have a very firm grasp on it, I want to branch out and expand my base of knowledge. After 5 years there are still things I am learning in PHP so the thought of 2 whole new languages/technologies to get familiar with is a bit daunting, but at the same time exciting.

The only downside with RoR is that my hosting account doesn’t support it without an upgrade that I can’t justify right now. It does support Python so maybe you’ll see a few python modules or classes start to pop up over here in the near future.

PHP, Programming, phpBB

Prosilver: Online/Offline Text Tag

If you don’t like the idea of an angled online image like the one that is default in the Prosilver theme of phpBB3, you can move to an Online/Offline text tag instead.

1. Go to your ACP
2. Click on the Styles Tab
3. Under Style Components click Templates
4. Click edit next to Prosilver
5. In the dropdown, select viewtopic_body.html
6. If you already applied my Profiles Left Mod, find this code:

<dl class="postprofile<!-- IF postrow.S_ONLINE --> online<!-- ENDIF -->" id="profile{postrow.POST_ID}"></dl>

Change it to:

<dl class="postprofile" id="profile{postrow.POST_ID}"></dl>

This just removes the online class from the postprofile dl object, which is responsible for the angled online background image.

7. Find the following code:

<dt>
				<!-- IF postrow.POSTER_AVATAR -->
					<!-- IF postrow.U_POST_AUTHOR --><a href="{postrow.U_POST_AUTHOR}">{postrow.POSTER_AVATAR}</a><!-- ELSE -->{postrow.POSTER_AVATAR}<!-- ENDIF -->
				<!-- ENDIF -->
				<!-- IF not postrow.U_POST_AUTHOR --><strong>{postrow.POST_AUTHOR_FULL}</strong><!-- ELSE -->{postrow.POST_AUTHOR_FULL}<!-- ENDIF -->
			</dt>
 
			<!-- IF postrow.RANK_TITLE or postrow.RANK_IMG -->
<dd>{postrow.RANK_TITLE}<!-- IF postrow.RANK_TITLE and postrow.RANK_IMG --><!-- ENDIF -->{postrow.RANK_IMG}</dd>
 
<!-- ENDIF -->
<dd> </dd>

Change it to:

<dt>
				<!-- IF postrow.POSTER_AVATAR -->
					<!-- IF postrow.U_POST_AUTHOR --><a href="{postrow.U_POST_AUTHOR}">{postrow.POSTER_AVATAR}</a><!-- ELSE -->{postrow.POSTER_AVATAR}<!-- ENDIF -->
				<!-- ENDIF -->
				<!-- IF not postrow.U_POST_AUTHOR --><strong>{postrow.POST_AUTHOR_FULL}</strong><!-- ELSE -->{postrow.POST_AUTHOR_FULL}<!-- ENDIF -->
			</dt>
 
			<!-- IF postrow.S_ONLINE -->
<dd style="color: #00ff00">Online</dd>
 
<!-- ELSE -->
<dd style="color: #ff0000">Offline</dd>
 
<!-- ENDIF -->
 
			<!-- IF postrow.RANK_TITLE or postrow.RANK_IMG -->
<dd>{postrow.RANK_TITLE}<!-- IF postrow.RANK_TITLE and postrow.RANK_IMG --><!-- ENDIF -->{postrow.RANK_IMG}</dd>
 
<!-- ENDIF -->
<dd> </dd>

What this does is adds the following code above the Rank Title or Rank Image (or both) in the postprofile block.

<!-- IF postrow.S_ONLINE -->
<dd style="color: #00ff00">Online</dd>
 
<!-- ELSE -->
<dd style="color: #ff0000">Offline</dd>
 
<!-- ENDIF -->

You can move that chunk around and put it below the Rank Title/Image or elsewhere. Let me know if you need more help.

PHP, Programming, phpBB

Adding Post Titles To Your Forum List in phpBB3

6 weeks after it’s release, I finally found the time to upgrade the forum over at Snackbar Games to phpBB 3. My hand was kind of forced due to some massive load that was generated on our phpBB2.x install and to appease the webhosting company, I told them I was going to upgrade to a clean install of phpBB3 with no mods or hacks installed.

The one thing I missed about my modded phpBB2.x forum was that the forumlist showed the topic or subject of the last post in that forum, something that phpBB3 lacks out of the box. Luckily there is a very easy way to add this functionality back in.

1. Go to your ACP and click on the Styles Tab.
2. Under Style Components on the left, click Templates
3. On the right, click the Edit button next to Prosilver. This should bring up a drop down list with all the available template files for the Prosilver theme.
4. Select ‘forumlist_body.html’ and let the lower half of the page refresh to display the current contents of that file.
5. Find the following block of code in your file:

<dd class="lastpost"><span>
						<!-- IF forumrow.LAST_POST_TIME --><dfn>{L_LAST_POST}</dfn> {L_POST_BY_AUTHOR} {forumrow.LAST_POSTER_FULL}
						<a href="{forumrow.U_LAST_POST}">{LAST_POST_IMG}</a> <br />{L_POSTED_ON_DATE} {forumrow.LAST_POST_TIME}<!-- ELSE -->{L_NO_POSTS}<!-- ENDIF --></span>
					</dd>

Change it to:

<dd class="lastpost"><span>
						<!-- IF forumrow.LAST_POST_TIME --><a href="{forumrow.U_LAST_POST}">{forumrow.LAST_POST_SUBJECT}</a> <br /><dfn>{L_LAST_POST}</dfn> {L_POST_BY_AUTHOR} {forumrow.LAST_POSTER_FULL}
						<a href="{forumrow.U_LAST_POST}">{LAST_POST_IMG}</a> <br />{L_POSTED_ON_DATE} {forumrow.LAST_POST_TIME}<!-- ELSE -->{L_NO_POSTS}<!-- ENDIF --></span>
					</dd>

All we did was add this small bit of code right inside the IF statement that checks the forumrow.LAST_POST_TIME variable. All this does, is builds a link to the last post in the thread and uses the subject line of this last post to display in the forumlist.

<a href="{forumrow.U_LAST_POST}">{forumrow.LAST_POST_SUBJECT}</a> <br />

Ideally, the topic title would be displayed instead of the subject from the last post, but that information is not available without altering the query for the forumlist and I wanted to go with a simple fix here that didn’t alter any of the core code of the system. To see this small hack in action, head on over to the SBG forum.

PHP, Programming

Programmers: Experience vs Talent

Nick Halstead recently asked himself the question:

Is experience better than talent?

It’s a perfectly valid question and probably one that more hiring managers should ponder before kicking off a recruitment run. Nick’s conclusion was really less of a conclusion and more of an affirmation that it’s too hard to give a simple answer to the question. Instead of attempting to answer the question in the same manner as Nick, which is to say that I don’t really have any valid bullet points on the topic, I am going to turn to my other passion, the NBA, and see if I can draw an analogy to help answer the question.

If we apply this question to the NBA, what we are essentially asking is “Would it be better to hire someone with unproven talent and an unproven track record or a seasoned veteran?” Taking this one step further, put yourself in the shoes of the Portland Trailblazers at the moment they discovered they had the #1 selection in the most recent draft. The question become “Do we draft a potential superstar in Greg Oden or Kevin Durant or trade that draft pick for someone with experience?” Portland chose to draft Oden #1 and he is currently sitting on the bench due to a necessary surgery.

If you stop the analogy there you might arrive at the conclusion that Portland made a mistake in going with unproven talent, but you would be wrong. Oden has yet to prove his value, something Portland is hoping he does next year after being in the league for a full year. Let’s shift the question to the Seattle Supersonics who selected Durant #2. Durant is currently averaging nearly 20 points per game, which is a great way of measuring a single players output on the court. In college, he averaged nearly 26. While lower than his collegiate numbers, you could say that the decision to draft Durant is paying off and falling in line with expectations. Unfortunately, Durant is also out with a minor ankle injury. Injuries are something that don’t affect programmers and their ability to produce quality code.

Durant and Oden were both amazing college ballplayers that entered the draft after a single collegiate season. While this is a form of experience, it is on a different level and could be compared to a programmer with 1 or more years of University under their belt. It would be expected that a college student with any formal exposure to programming has a form of experience.

The analogy does start to fall apart when you factor in the difference between measuring basketball stats as a form of talent versus the intangible talent that a programmer possesses. We did see from the analogy that taking a risk on unproven talent can reap great rewards in the form of a “superstar”. Often enough though, a potential superstar goes bust and fades into the crowd. What had happened if either team had gone with experience? It’s hard to put a real value on the 2 young players in their first year, but both of these players are making close to $4M this year in salary (Source: HoopsHype). One could assume that their trade value is much higher than their per year salary in pure dollars. I’d expect that either of these players on the open “market” would fetch a superstar in return if the deal was right or if there was another player thrown into the deal.

What that leaves you with is an almost 1:1 trade value. Let’s scale back the basketball economics though and get back to the real issue of experience vs. talent. Talent has risks associated with it, especially in younger players or employees. They are unproven and could fizzle out. Take Dolphins running back Ricky Williams for example. Likewise, a young talent could explode and have massive rewards far greater than one could imagine. The experienced employee is the safe choice. You have a track record that shows you what to expect and while not the most talented person you may have more an idea of what you are signing up for versus the young talent.

In the end, I think I am arriving at the same conclusion that Nick did and that is that it depends. Not all young talent is created equal and not all experience is learned from. That said, for hiring managers faced with the choice I would said that a good mix of the two is really the best formula. The young talent can learn and grow through the experience of your veterans and allow their true potential to really shine through. Your vets may even get a second wind as the new talent comes in and raises the drive of those around him/her.

PHP, Programming, Tech, phpBB

Making Prosilver Have Profiles on the Left

phpBB 3.0 was released this week and the default theme, Prosilver, has the post content on the left and the poster’s profile on the right. This is reverse from most forum software and the subsilver theme of previous phpBB releases. I’ve taken the time to tweak the CSS and code to flip the poster profile back to the left and the post content to the right. Here are the steps you need to take:

Prosilver Left Packaged Style Download Now Available
Many users were having issues with fully completing this mod without a typo and since it involves modifying the main templates for the theme, I went ahead and packaged this style into an easy to install theme with the name of “prosilver_left”. You can download this theme as a zip file here.

Simply unzip this file and upload the “prosilver_left” folder to your styles directory in your phpBB3 directory. Install it from the Styles tab of your ACP.

Help/Support Instructions - If you need assistance after attempting this modification, please make sure you accurately describe your problem (including an image if necessary) and either include a link to the live board or PM the link to me. Please post your requests in the official thread at phpBB.com

Mod Instructions

Demo: http://www.darksideofthecarton.com/phpbb3_demo/viewtopic.php?f=2&t=1

1. Go to your ACP and click on the Styles Tab.
2. Under Style Components on the left, click Themes
3. On the right, click the Edit button next to Prosilver. This should bring up an editing window with the full CSS for the Prosilver theme inside it.
4. Find the following code inside that CSS file. It should be around like 1054. We are going to change the float value for the .postbody class from left to right.

/* Post body styles
----------------------------------------*/
.postbody {
padding: 0;
line-height: 1.48em;
color: #333333;
width: 76%;
float: left;
clear: both;
}

Change it to:

/* Post body styles
----------------------------------------*/
.postbody {
padding: 0;
line-height: 1.48em;
color: #333333;
width: 76%;
float: right;
clear: both;
}

5. Find the following code inside that CSS file. It should be around like 1532. We are going to change the float value for the .postprofile class from right to left and move the white vertical border to the right side.

/* Poster profile block
----------------------------------------*/
.postprofile {
/* Also see tweaks.css */
margin: 5px 0 10px 0;
min-height: 80px;
color: #666666;
border-left: 1px solid #FFFFFF;
width: 22%;
float: right;
display: inline;
}

Change it to:

/* Poster profile block
----------------------------------------*/
.postprofile {
/* Also see tweaks.css */
margin: 5px 0 10px 0;
min-height: 80px;
color: #666666;
border-right: 1px solid #FFFFFF;
width: 22%;
float: left;
display: inline;
}

6: Next find this:

.pm .postprofile {
border-left: 1px solid #DDDDDD;
}

Change it to:

.pm .postprofile {
border-right: 1px solid #DDDDDD;
}

7: Next find this:

.online {
background-image: url("{T_IMAGESET_LANG_PATH}/icon_user_online.gif");
}

Change it to this:

.online {
background-image: url("{T_IMAGESET_LANG_PATH}/icon_user_online.gif");
margin-top: -5px;
}
 
.online dt {
margin-top: 5px;
}

8: Click Submit to update your Theme/CSS file for Prosilver.
9: On the left, click Templates under Style Components
10: Click Edit next to Prosilver.
11: In the drop down menu, go to the very bottom and select viewtopic_body.html and it should open an editor with the current code inside it.
12: First we need to remove the current image from being applied to the Post’s div. Find the following line of code at or around line 117:

<div id="p{postrow.POST_ID}" class="post <!-- IF postrow.S_ROW_COUNT is odd -->bg1<!-- ELSE -->bg2<!-- ENDIF --><!-- IF postrow.S_UNREAD_POST --> unreadpost<!-- ENDIF --><!-- IF postrow.S_POST_REPORTED --> reported<!-- ENDIF --><!-- IF postrow.S_ONLINE --> online<!-- ENDIF -->"></div>

Remove this:

<!-- IF postrow.S_ONLINE --> online<!-- ENDIF -->

to create this:

<div id="p{postrow.POST_ID}" class="post <!-- IF postrow.S_ROW_COUNT is odd -->bg1<!-- ELSE -->bg2<!-- ENDIF --><!-- IF postrow.S_UNREAD_POST --> unreadpost<!-- ENDIF --><!-- IF postrow.S_POST_REPORTED --> reported<!-- ENDIF -->"></div>

13: Now we need to add this back to the postprofile dl element. Find the following line of code at or around line 173:

<dl id="profile{postrow.POST_ID}" class="postprofile"></dl>

Change it to this:

<dl id="profile{postrow.POST_ID}" class="postprofile<!-- IF postrow.S_ONLINE --> online<!-- ENDIF -->"></dl>

14: Click submit to save your template.
15: Select ucp_pm_viewmessage.html from the drop down.
16: Find this code at or around line 16:

<div id="post-{MESSAGE_ID}" class="post pm<!-- IF S_POST_UNAPPROVED or S_POST_REPORTED --> reported<!-- ENDIF --><!-- IF S_ONLINE --> online<!-- ENDIF -->"></div>

Remove this:

<!-- IF S_ONLINE --> online<!-- ENDIF -->

to create this:

<div id="post-{MESSAGE_ID}" class="post pm<!-- IF S_POST_UNAPPROVED or S_POST_REPORTED --> reported<!-- ENDIF -->"></div>

17: Find this code at or around line 65:

<dl id="profile{MESSAGE_ID}" class="postprofile"></dl>

Change it to:

<dl id="profile{MESSAGE_ID}" class="postprofile<!-- IF S_ONLINE --> online<!-- ENDIF -->"></dl>

This should correctly align the profiles on the left just like in subsilver in topics as well as Private Messages. Here is what you get if completed properly:

Viewing a PM:

Viewing a Post:

Javascript, Programming

Sniff Your Users Version of Flash with Javascript

I ran across this on a website I hit this morning and found it to be quite useful. So I am reposting it here in case anyone else is trying to do this. In a nutshell, this bit of javascript will return a decimal value of the highest version of the Flash Player installed on an end users system. You can even specify the minimum version you are looking for and get a boolean TRUE or FALSE in return. Very slick.

[code]// If called with no parameters this function returns a floating point value
// which should be the version of the Flash Player or 0.0
// ex: Flash Player 7r14 returns 7.14
// If called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
reqVer = parseFloat(reqMajorVer + “.” + reqRevision);
// loop backwards through the versions until we find the newest version
for (i=25;i>0;i–) {
if (isIE && isWin && !isOpera) {
versionStr = VBGetSwfVer(i);
} else {
versionStr = JSGetSwfVer(i);
}
if (versionStr == -1 ) {
return false;
} else if (versionStr != 0) {
if(isIE && isWin && !isOpera) {
tempArray = versionStr.split(” “);
tempString = tempArray[1];
versionArray = tempString .split(”,”);
} else {
versionArray = versionStr.split(”.”);
}
versionMajor = versionArray[0];
versionMinor = versionArray[1];
versionRevision = versionArray[2];

versionString = versionMajor + “.” + versionRevision; // 7.0r24 == 7.24
versionNum = parseFloat(versionString);
// is the major.revision >= requested major.revision AND the minor version >= requested minor
if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
return true;
} else {
return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
}
}
}
return (reqVer ? false : 0.0);

}[/code]

PHP, Programming

PHP Is a Bad Language for New Programmers

PHP is a very simple language to learn. It’s loose data types and plethora of examples on the web make the cost of entry to PHP development very low. It also empowers new programmers to venture out and get cracking on their own projects. I admire the efforts of these new programmers, but I also think that choosing PHP as their starting point is going to bite them in the butt in the long run.

Like having just enough rope to hang yourself, figuratively of course, PHP provides you just enough rope to develop some very bad programming habits. As a product of a University Computer Science program, I strongly believe that some level of professional or formal instruction is paramount to being a good programmer. There are exceptions to all rules, and especially this one, but by and large most people don’t see programming best practices as common sense so they must be taught. Here are a few tips that I think early programmers should be firmly familiar with:

1. Watch your datatypes - PHP supports loose variable declarations. What this means is that can change a variable’s datatype on the fly based on whatever you are assigning to it at the current time. While this is nice and prevents you from having to declare massive lists of variables, do not get into the habit of reusing variables for numerous purposes in your code. A variable named $count should be used for it’s initial purpose as a counter and not used later to store some other bit of data such as a name or other string.

2. Pick a variable naming standard - PHP allows you to name variables just about anything. There are a few exceptions to this that are explained over on php.net. Keep in mind that variable names in PHP are case sensitive. One mistake that you will want to avoid in variable naming is mixing and matching your variable naming scheme. By this I mean, choose a method for utilizing case or underscores when creating variable names. $my_variable_name and $myVariableName are both acceptable and easy to read variable names. The latter is an example of Camel Case and is the method that I use for naming all of my variables. Either method is fine, but pick one way and stick with it. When working on a group project, discuss this with your fellow programmers and set a standard for the project to avoid inconsistencies.

3. Don’t be afraid to ask for help - There are a huge number of development forums filled with some very experienced programmers that are more than happy to lend new coders a hand. Don’t be afraid to ask them for advice. They have a lot of wisdom to impart and they can be a great source of information. My personal favorite is the PHP forum over at DevShed.

4. Just because  you saw it online doesn’t mean it’s good code - There is a lot of bad PHP floating around and there are a lot of well intentioned people out there passing this bad code off as a good source for learning PHP. Don’t be sucked into that cycle. Try to keep your resources to some of the more mainstream PHP sites like PHP Builder, DevShed, PHP Freaks, and the W3 Schools website.

Next »