Best practices – a poem

I.T. took away my remote server access today
Now I can only code local-ly
Though I don’t blame them, I’d just like to say
If my code hygiene needs washing
These bugs that I’m squashing
Weren’t found in Q.A.

‘Best practice’ was my first and my middle name
I agiled my scrums, I sprinted my days
Test-driven delivered a build to throw away
But the users don’t understand it
And they simply demanded it
Be fixed right away

Pear's mail_mine not working with Outlook

In a WordPress plugin I am developing, I am using Pear’s mail_mime to generate an HTML email and send it with an attachment. This worked fine for me during testing with my gmail account. However once I started using the plugin to send emails to others, Outlook users were getting a garbled mess. Outlook could not properly understand the generated multipart/mixed message and was showing the raw text. It took a bit of digging to discover the solution, but the root of the issue goes back to an age-old C.S. problem.

Windows uses ‘\r\n’ to define a line break, while Unix-like systems use ‘\n’ to define a line break. Gmail doesn’t care which kind of line break you use, but Outlook requires line breaks to be in the ‘\r\n’ format.

Mail_mime allows you to pass the default for line breaks as a constructor argument. However, this didn’t work correctly for me. The Mail_mime code uses the End-Of-Line constant (PHP_EOL) and passing the EOL value in was not enough to change the value in all places.

What I had to do to finally get Outlook emails to display correctly was to first define the PHP_EOL constant and then pass it in as well.

// define the PHP_EOL constant
if (!defined('PHP_EOL')) define ('PHP_EOL',"\r\n");
// pass in the value on create
$mime = new Mail_mime(PHP_EOL);

I had to define the constant AND pass in the value to catch all the places the linebreak characters were used. This seems like a bug in Mail_mime.

I used the comments on this bug report to solve this issue. Unfortunately since this work-around was discovered, the maintainers don’t seem willing to fix this issue.

Apache RewriteRule for Changing Your Permalink Settings

I recently updated my blog’s permalink settings and did away with the year and month in the url structure. I’m experimenting with placing the post name more prominently in the url as it is better for SEO.
Here’s the Apache rewrite rule I used to move my blog posts from /yyyy/mm/post-name to /post-name. The rewrite rule uses a 301 redirect so this should not negatively affect the post’s Google rankings.

# Redirect moved blog posts
RewriteBase /blog/
RewriteRule ^20[0-1][0-9]/[0-9][0-9]/(.*) $1 [R=301,L]

Breaking the RegEx apart:

RewriteBase /blog/
This tells the rewrite engine that we are only going to look at URLs that are under the /blog/ directory

^20[0-1][0-9]/
Since we indicated our RewriteBase is “/blog/” on the previous line, “/blog/” will be chopped off of the URLs;
“^” says that our string should now start with the following pattern
I started this blog in 2009, so I only need to target years after 2000. I started my year RegEx with “20” to simplify.
“[0-1]” indicates I’m looking for a number range between zero and one. Since I only need to target between 2009-2013 the next digit could only be a zero or 1.
“[0-9]” indicates that the last digit in the year could be any of these.
“/” the next character should be a forward slash

[0-9][0-9]/
This RegEx targets the months: any digit zero through nine, followed by any digit zero through nine
“/” the next character should be a forward slash

(.*)
The rest of the url is our post-name. “(.*)” says take the rest of the url string and remember it as the first variable.

 $1 [R=301,L]
“$1” is the first variable that we got from the (.*) statement above. “R=301” says to 301 redirect this request to whatever url is in the $1 variable. It knows to prepend ‘/blog/’ to the request because we indicated our URLs should start with “/blog/” using the RewriteBase command. “L” indicates that this should be the Last rewrite rule it processes.

Regex can be confusing. Its is often easiest to build up your RegEx bit by bit until you have the completed statement.

WEB-INF\cftags\META-INF\taglib.cftld (The system cannot find the path specified)

Recently I moved a long running process inside a cfthread and began to get this error during testing: “WEB-INF\cftags\META-INF\taglib.cftld (The system cannot find the path specified)”

The issue is that getPageContext().getServletContext().getRealPath(“/WEB-INF”) returns the incorrect path when inside a cfthread.

If you use cfthread you may eventually run into this bug, but maybe not as I think its a very rare bug. There doesn’t seem to be a fix for this issue, at least not in CF9, but I did find a workaround.

A demonstration of the bug






    
    
    

The results:

C:\ColdFusion9\wwwroot\WEB-INF\
C:\wwwroot\WEB-INF

Inside the cfthread Coldfusion has forgotten where /WEB-INF is!

In my case I had:


in the code inside the cfthread.
“New Query()” corresponds with the custom tag C:\ColdFusion9\CustomTags\com\adobe\coldfusion\query.cfc.
Query.cfc extends C:\ColdFusion9\CustomTags\com\adobe\coldfusion\base.cfc.

To create this object Coldfusion calls “getSupportedTagAttributes” in base.cfc which looks up the attributes in taglib.cftld. That file is located under the /WEB-INF directory. If we dig into the guts of base.cfc we find the offending code is on line 228. Coldfusion tries to find “/WEB-INF/cftags/META-INF/taglib.cftld” but since we are inside a cfthread the code fails to use the correct path.

Creating a mapping didn’t help and neither did creating a virtual directory in Apache.

The workaround

Code similar to this was working fine in the production environment, but how was it avoiding this error? The solution was found when I stepped back and read the entire function. Wrapping the offending line is the code:

if(not isdefined("server.coldfusion.serviceTagAttributes.#tagName#"))

Coldfusion will look up the attributes only if they are not already cached in server memory. This is why this bug is hardly seen: normally all these attributes have been cached before being encountered inside a cfthread. My testing was very specific and so the Query attributes were never loaded prior to entering the cfthread. The trick is to make sure the attributes are cached before entering the cfthread.

For testing I added the line:


before entering the cfthread and that forced the attributes to be cached and the error went away.

I hope this post helps someone else with this issue!

The method matches was not found in component:Ensure that the method is defined, and that it is spelled correctly.

I recently updated my local CF build to 9.0.2. After installation I reloaded my app and everything worked fine. I then went back into the admin and enabled robust exceptions and a few other things for my dev environment. I also saw an option to turn off CFC type check. I had never tried this before so I turned it on to see if there was a speed difference. Instead my app stopped loading and displayed this error:

“The method matches was not found in component C:/wwwroot/MyApp/myComponent.cfc .:Ensure that the method is defined, and that it is spelled correctly.”

Digging into the code, the issue is inside Coldspring. “Matches” is a function used by Coldspring in the ProxyFactoryBean.cfc. With CFC type check turned off it seems that Coldfusion can no longer follow the CFC inheritance and does not realize that this function exists. Turning CFC Type check back off in the CF admin solved the issue, though I am still unsure why this was a problem.

The only other info I found on this issue was this Google Groups thread.

cftag2cfscript – The cftag to cfscript converter for Coldfusion

Just uploaded my new project cftag2cfscript to github this morning.

With the arrival of Coldfusion 9 we can finally build our cfcs in cfscript. I’ve found that there can be a significant speed increase when converting an existing tag based cfc to cfscript. YMMV of course. Others may want to convert legacy cfcs to cfscript because they prefer writing in cfscript. Whatever your reason is for wanting to convert cf tags in to script cftag2cfscript is here to help.

Converting tag-based CF to script based CF is time consuming. This project aims to make it dead simple. Its not there yet, but maybe with your help we can reach that goal.

Checkout cftag2cfscript on Github

A difference between cfloop and for loop in cfscript

I am moving key parts of a codebase to cfscript to take advantage of the speed boost that comes with using cfscript. Along the way I’m discovering the differences between tag-based Coldfusion and cfscript.

Today this was the bug I worked out:

This code will delete 10 widgets:




 

This cfscript will only delete 5 widgets:

var i = 1;
setNumberofWidgets(10);
for(i=1;i<=getWidgetTotal();i++)
{
deleteAWidget();
}

This is because getWidgetTotal() will be called on each iteration of the loop. After deleting 5 widgets the loop will exit because there will only be 5 widgets left and i will equal 5. The cfloop tag from the first example will only evaluate once before starting the loop.

This cfscript will delete 10 widgets:

var i = 1;
var total = getWidgetTotal();
setNumberofWidgets(10);
for(i=1;i<=total;i++)
{
deleteAWidget();
}

By calling the function before starting the loop we can get the total number of widgets and are then able to complete the loop.

PointClickPress and The Great Underground Adventure Postmortem

In the Beginning

Before I launch into my next programming adventure I should figure out what went wrong and what went right with the development of both the PointClickPress engine and with The Great Underground Adventure game. Its important to learn from your mistakes, so here we go.

I started this project because I wanted to learn a PHP framework and I thought it would be straight forward (and fun) to make an in-browser point-and-click style game. Maybe that could’ve been true but, between major life changes outside of this project and getting bogged down in game-engine hell, it ended up taking two years.  I think that is way to much time for the small game that was ultimately the result of the project, but then again, its not like I was working on it full time. Sometimes maybe I got an hour a week between changing dirty diapers.  I think other people would’ve (wisely?) abandoned this project when so little progress was being made for such little results, but for better or worse I stubbornly stuck with it.

What went right?

1. Choosing the Kohana Framework

I spent a lot of time researching and testing frameworks before I selected one to learn.  I’ve touched on this subject before.  Kohana made the development of the PointClickPress game engine very straight forward. The framework allowed me to stay organized, leverage existing libraries, and be productive very quickly without getting bogged down in framework guts or configuration.  I was even able to find a Kohaha module that came with fully developed user registration, authorization, and admin sections which allowed me to focus on building the game engine and not go through the dry exercise of user management again.  Of course, I had spent a weekend building the user admin already by the time I found that nice module.

2. Breaking the project into phases

I broke PCP development into manageable chunks that would be functional and releasable when completed.  Software development is an iterative process and you can’t plan for everything along the way.  Marking out stopping points in the development allowed me to make sure that I’d get something released. Nothing keeps up my motivation like releasing something playable for people to try.

3. Mobile & desktop support

Right from the start I made sure that PointClickPress was going to work as well as in a mobile browser as it did on a desktop. Every design decision considered mobile.  I think it would have been very difficult to ignore mobile and then try to shoehorn the site into a mobile browser after the desktop was done.   Everything works equally well on large and small screens. I did go overboard keeping by javascript to a minimum however.

4. Using photos for game art

I really like how the photos came out for The Great Underground Adventure. I think they look great and since you are (for the most part) exploring real locations it adds an extra level of interest to the  game.  Scouting and taking the photos was also an adventure that lead me to all sorts of interesting places.

What went wrong?

1. Getting lost in game engine hell

Despite choosing a light framework that had many built in libraries I still over engineered the PCP game engine and administration area way beyond what was needed to make a straightforward point-and-click adventure game.  If I had just kept my head out of the clouds and built only what I needed to get my game working I’d have finished a year earlier. I kept wanting the engine and admin to be “perfect” before I started building the game. I was re-architecting things constantly and still finding ways to refactor and abstract.  Eventually I just had to stop!

I also stubbornly refused to use ORM, so as my database model evolved I was constantly tinkering with the SQL. Later on I was constantly fixing SQL bugs rather than just loading the ORM library at the start and being done with it.

I greatly admired how WordPress allows a user to add additional functionality easily. I wanted the engine to have plugins, to be extensible, so that end users could easily add features that I hadn’t thought of.  I spent way to much time thinking of how to de-couple functionality from the engine “core” than was healthy.  Depending on the ability for users to extend the existing functionality by adding their own clickable actions was also a trap that eventually doomed the idea of a public PointClickPress portal game website due to security issues.

2. User-created extension security issues derail game creation

I consider this the biggest problem with the current version of PointClickPress.com. This is not an issue for the stand-alone version however.

The first iteration of PointClickPRess was as single-user, multi-story creator.  The use-case was that an author could self-host an installation of PCP and build “stories” (aka games) with it. In this use-case PCP supported only a few out-of-the box clickable actions such as linking two locations together.  The plan was that an author could easily write any intermediate to advanced click actions directly in PHP and connect to the PCP API. It is very straight forward to create a small action file and place it in the action directory. Then it was available to use.  Alternately there was an out-of-the-box clickable action that was simply a PHP eval() call. This allowed the user to type PHP in from the admin area without needing to create a file.  This ability to create new actions easily allowed PCP to support whatever an author would want.

Of course neither of these options is viable on a multi-user public installation such as PointClickPress.com.  The authors no longer have access to the filesystem, and allowing the use of eval() is also out of the question for security reasons.  This loss of extensibility severely hinders game development.  The built-in clickable actions by themselves don’t do much. They were only meant to be shortcuts for simple tasks.  Now an author on PointClickPress.com must chain many simple actions together to equal what one action on a self-hosted version of PCP could do. This is what I have done for The Great Underground Adventure. Often there is a cascade of three or four clickable actions that are called behind the scenes as the result of one click. Chaining actions together is a fairly arduous task. And I don’t expect anybody to enjoy using the public site for this reason.

3. Using photos for game art

Using photos for game art was a million times harder than I thought it was going to be. I talked about this already in a blog post here.  I estimate I took around 3000 photos over the course of two years. Many I got on vacations, but also I scouted locations around me. Probably about 400 of these photos made it into the game either by themselves or as part of a composition.  I’m not a strong “Photoshopper”, so creating the composite photos, such as the views surrounding the cable car area. Took many evenings to get right.

 

Conclusion

In the journey from the start to the end of this project the game felt as if it became an after-thought unfortunately and the PCP admin seemed to be the central focus. Maybe I’m just born to be a back-end programmer. I certainly accomplish my goal of learning a PHP framework from this project.  I did plan on making other games using this engine, but now that I am finished I find myself brainstorming the next project around learning new technologies. I’d like to play with the canvas and try designing a multiplayer game. Maybe learning Python and Tornado, or Node.js, at the same time.   I’m not sure many people but me will enjoy playing The Great Underground Adventure, but I’m proud of it. I think its charming in its own decidedly low-tech way. The same goes for the PointClickPress engine.

If anyone wants to try their hand at building a game you are welcome to give it a try at PointClickPress.com or you can download and host your own copy. PointClickPress  is open sourced under the BSD license and is available for download at SourceForge.net.

CFBuilder, we need to talk

Dear CFBuilder,

I tried, I really tried. You came with a great reputation, and everyone stuck up for you. And though we’ve fought every single day for two years now I can honestly say that I’ve tried to make this relationship work.

I loved your code introspection. The ability to click on a method call and jump right to where that method is defined was a great help. And your ability to intelligently refactor code was also wonderful. There were little things too, like your built-in server controls. And of course I’ll never forget that your code hinting was completely up to date with CF9.

But living with you every day was difficult. Despite my 64bit 8gig machine you still managed to drag my entire system to a crawl. And I can never just start fresh with you; I think in files and code, but you were always demanding I make projects. I’ve already organize my project into folders on the drive; I’m not interested in duplicating the process for your benefit. But you never listen; it was your way or the highway. You threw a tantrum every time I tried to go around you and open a file directly from explorer.

You often get confused with files that contain a mix of SQL and Coldfusion or HTML, JS, and CF. This kills your code hinting and your syntax highlighter. Speaking of your syntax highlighter: it always dies on larger files, and has horrible refresh lag. I shouldn’t have to wait 30-45 seconds with each tab press when indenting codeblocks. I’ve wasted so much time waiting for you.

But I’ve said to much already. Afterall, its not you: its me. I’ve discovered that I’m more old fashioned than I realized. Despite all your bells and whistles all I really want is an IDE that is responsive. An IDE that will listen to me. All I really need is a solid text editor with syntax highlighting. You’re a lot of great things, but a snappy text editor is not one of them. I can type faster than you can think: and that’s not how it should be.

I don’t know whether I’ll run back to my ex, Dreamweaver, or if I’ll finally get serious with Notepad++, but neither have hesitated when helping me in the past.  Its time to move on. We can try to be friends. Give me a call when Twister comes out.

Sincerely,

-Pirate Gaspard

The Great Underground Adventure is finished!

I’m happy to announce that my first PointClickPress game is finished and ready for playing!

The Great Underground Adventure is a low-tech web adventure game I’ve been working on for a while.

Explore some interesting locations, save the day, and try not to get eaten!

Play “The Great Underground Adventure ” here.


You are standing west of a white house.