Friday, October 26, 2012

Code Complete: High Quality Routines

So reading Steve McConnell's Code Complete chapter 7 on writing "High Quality Routines" I am struck that there is lots and lots of great advice in there about writing methods.  Particular things such as clear naming conventions, relatively short routine length, short sets of input parameters etc. are all excellent.  However the problem that I have with the chapter overall is that it feels like it is easy to read it and then not put much of the advice into practice.  While the chapter has lots of great example code snippets, I feel like it is easy to ignore the great advice, given that it's not being encountered as part of a larger project.

Maybe this is just me.  I'm a huge fan of working on things in a larger context, but I know that that's not everybody's cup of tea.  It has been argued that some people like to read the abstract theory associated with things before encountering examples in context.  If you're one of these people please do get in touch and let me know.  I'd like to learn more about your thought processes.

Anyhow, just as an example of some mixed routine practices from things I was programming today, I created one short routine
function processForm(params) {
var ss = SpreadsheetApp.openById(csc3211fall2012_ss);
var email_sheet = ss.getSheetByName('Week '+(parseInt(params['week'],10)+1)+' Assignments');
var range = colName(parseInt(params['assignment'],10)+1) + (parseInt(params['match'],10)+1);
var existing_val = email_sheet.getRange(range).getValue()
email_sheet.getRange(range).setValue(existing_val+ params['submission']);
}
view raw gistfile1.js hosted with ❤ by GitHub
and grabbed another from StackOverflow:

http://stackoverflow.com/questions/8240637/javascript-convert-numbers-to-letters-beyond-the-26-character-alphabet

function colName(n) {
var s = "";
while(n >= 0) {
s = String.fromCharCode(n % 26 + 97) + s;
n = Math.floor(n / 26) - 1;
}
return s;
}
view raw gistfile1.js hosted with ❤ by GitHub
Now I've got a really awful vanilla name for the routine I created (processForm) and all sorts of hacked together pieces of things, but that's partly a reflection of the "tracer bullet" methodology I was working on here, which was to get something that would take a form submission from a Google App Script generated html page, and enter the resulting data into a spreadsheet as described in this StackOverflow post:

http://stackoverflow.com/questions/13086880/can-i-mix-jqueryui-and-google-app-script-form-submissions

At least I can be pleased that both routines are relatively short and have a very small number of input parameters.  I think that both could be improved with refactoring.  I'll present that in a future blog post, but right now I know I and my students are going to be using this code every week to support assignment submissions, and getting a feel for getting this working in Google App Scripts was the top priority that trumped every other consideration.  Not the best programming practice perhaps, but let's see how I can iterate and improve this over the next few weeks.

Google App Scripts really rocks, but I'm still trying to work out how to version and share all this stuff effectively so we can see the software engineering and systems analysis issues in the context of this larger project, as described in this other SO post:

http://stackoverflow.com/questions/12712593/should-google-app-scripts-be-stored-in-version-control-like-github