Pages

Friday, September 30, 2011

New StackExchange Site: CodeReview

I'm really excited about the newest site that StackExchange has released as a beta: CodeReview (http://codereview.stackexchange.com/). The premise of this new site is to post code you're working on and get feedback from other developers. The best part is that its a StackExchagne site so you know that the best answers will naturally rise to the top and bad answers will get down voted so you don't need to be overly concerned about getting poor advise.

Being a developer at a smaller company means that I don't get to have my code looked at as often as I would like to and this website could be an excellent resource developers just like myself. I know that sometimes its hard for developers to share your code and hear any feedback that isn't glowing but I really believe that most of what a good developers knows is knowledge that has been passed to them from another developer. So far this site doesn't look like its getting much traffic yet so I hope that if you read this and are excited about this new site you'll also help spread the word. I'd especially love to see a lot of you ColdFusion developers on there sharing code and commenting on others.

Screencast: Using E Texteditor To Write ColdFusion Code

Lately on twitter there has been a lot of talk about using text editors like SublimeText 2 and E Texteditor for windows to write your ColdFusion code.  I've been using them a lot in the last few months so I thought I'd put together a quick screencast to show you how to use them (in this case E) to quickly produce ColdFusion code and how to customize the ColdFusion bundles for the way you code.

This particular screencast was done in E, however much of it applies to Sublime Text 2 as well, its just that its not quite as user friendly.  Where E has a nice bundle editor built in Sublime will make you browse to the "Packages folder" (you can get there through the interface by going to "Preferences -> Browse Packages" and find the tag's .tmSnippet file and manually open it in Sublime Text.

You'll defiantly want to make sure that you view this in the "full screen" mode by clicking on the icon in the lower right corner of the player.

Unable to display content. Adobe Flash is required.

Monday, September 26, 2011

Serialize Your CFC's Getters Into JavaScript Objects With CF PB&J

This weekend I worked on a little project called CF PB&J that will serialize a CFC's getters and their output into a well-encapsulated JavaScript object.  It's meant to be lightweight and work seamless with your JavaScript  while not cluttering up your global by having to create a  bunch of global variables to store your dynamic data.  You can find the project up on github at https://github.com/bittersweetryan/CF-PB-J.

Lets take a look at a quick example of how it works.  First lets create an Account object (note that there would typically be setter methods for these functions as well but they are left out for brevity's sake):

<cfcomponent hint="Test very simple cfc for searalization to javascript">
	<cfscript>
		variables.accountNumber = 0;
		variables.accountName = "";
		variables.productArray = [];
		variables.insuredProducts = {};
		
		
		variables.PBJ = "";
	</cfscript>
	
	<cffunction name="init" returntype="Account" access="public" output="false" hint="" >
		<cfscript>
//create the PBJ object and pass in a reference to this
			variables.PBJ = createObject("PBJ").init(this);
			return this;
		</cfscript>
	</cffunction>
	
	<cffunction name="getAccountNumber" returntype="numeric" access="public" output="false" hint="" >
		<cfscript>
			return variables.accountNumber; 
		</cfscript>
	</cffunction>
	
	<cffunction name="getAccountName" returntype="String" access="public" output="false" hint="" >
		<cfscript>
			return variables.accountName; 
		</cfscript>
	</cffunction>
	
	<cffunction name="toJS" returntype="String" access="public" output="false" hint="Proxy to the PBJ toJS method" >
		<cfscript>
			return variables.PBJ.toJS();
		</cfscript>
	</cffunction>
	
	<cffunction name="getProductArray" returntype="Array" access="public" output="false" hint="" >
		<cfscript>
			return variables.productArray;
		</cfscript>
	</cffunction>


	<cffunction name="getInsuredProducts" returntype="Struct" access="public" output="false" hint="" >
		<cfscript>
			return variables.insuredProducts;
		</cfscript>
	</cffunction>
	
</cfcomponent>

If you look closely at the Init method you'll see that a PBJ object is created and initalized and the "this" reference is passed to it.  This tells PB&J to inspect the current object for its "getter" methods defined by the naming convention getXXX.  PB&J will look for all getter methods that return strings, numbers, arrays, and structures.  This object also defines a proxy function called "toJS"that calls the PBJ.toJS() method.  When this method is invoked it will run all of your object's getter methods, save the return data, and output JavaScript that is a mirror of your object with its getter methods and its data.  Lets take a look at an example of this in action.  First lets create an instance of the account object and set some of its data:

account = createObject("Account").init();
	
account.setAccountNumber(12345);
account.setAccountName("Ryan ""Pretty Boy"" Anklam");

Now in our .cfm page we output the value of  #account.toJS()#  in the header to produce our JavaScript representation of our object:

< script type = "text/javascript" charset = "utf-8" >
var Account = (function() {
    var accountName = "Ryan \"Pretty Boy\" Anklam";
    var productArray = [];
    var insuredProducts = {};
    var accountNumber = 12345;
    return {
        getAccountName: function() {
            return accountName;
        },
        getProductArray: function() {
            return productArray;
        },
        getInsuredProducts: function() {
            return insuredProducts;
        },
        getAccountNumber: function() {
            return accountNumber;
        }
    };
})(); 
< /script>

Looking at the JavaScript you'll see that CF PB&J created a few private variables and returned public methods to return that data.   It's meant to be a "read-only" representation of the internal state of your object.  PB&J will also work on calculated fields as it just invokes your getter methods and saves its output.

Now in any of your scripts you can access the object's data by calling the JavaScript object's getter methods:

< script type = "text/javascript" charset = "utf-8" >
     Account.getAccountName();
     Account.getAccountNumber();
< /script>

Friday, September 16, 2011

Do the ColdFusion Community Members Need to Become Jerks?

Forgive me about the over-dramatic title of this post, however there is some truth into it.  The reason Ruby and Rails has such a good reputation for producing good code is that the community won't accept anything less.  A Rails project is expected to have a comprehensive test suite and well crafted code.  If it doesn't and the developer tries to release a poorly engineered project to the community its a given that the developer will hear about it.  Nothing less is accepted.

As ColdFusion developers I think we tend to be a bit more forgiving of poorly crafted code as to not discourage anyone from using the language, or we just aren't as outspoken as other communities.  I think by these things we are doing the entire community a disservice.  We need to push for well-crafted, professional applications.  It starts in the open source world, however we need to be able to push this mentality into private development as well.

From what I've seen a large percentage of the open source ColdFusion projects are well written and well tested, however its in the private development where "professional" development practices are lacking.  This is where the root of the poor perception of ColdFusion exists.  Every time a developer from another language opens a poorly CF written app, that's one more developer that forms a poor opinion of ColdFusion.  Even though we all know that that ColdFusion isn't to blame for a poorly written application, we are to blame for accepting poor development practices.

I think if we speak loudly enough about poor practices via twitter, blogs, in conferences, and when reviewing others code we can start to shift the perception of ColdFusion code to a respectable level.  We don't necessarily need to become "jerks", however we do need to be more outspoken when we see poor coding practices being implemented in projects.  If we as ColdFusion developers clean up our code we can do more one thing to clean up the perception of ColdFusion.

Wednesday, September 14, 2011

I'll be presenting "JavaScript Fundamentals For ColdFusion Developers" Tomorrow

Level Up your JavaScript skills with me tomorrow at 11:00 AM CST on the ColdFusion online meetup: http://www.meetup.com/coldfusionmeetup/events/32356622/.  I'll be presenting on the fundamentals of JavaScript from a ColdFusion developers perspective.  A lot of the code samples in the presentation will have ColdFusion syntax side-by-side with JavaScript syntax so you can relate the concepts with a language you are already familiar with.  I'll be covering a lot of material in this session including:

  • Variables
  • Operators 
  • Functions
  • Objects
  • This 
If you are interested in JavaScript but never really took the time to learn it, of you've only done programming in your favorite JavaScript framework you should come check it out and see whats behind one of the fastest growing languages around today.   According to GitHub JavaScript is their most popular language with 20% of all their projects being JavaScript related.