kdmurray.blog

Personal Weblog of Keith Murray

kdmurray.blog - Personal Weblog of Keith Murray

Programming Languages 101

A few weeks ago I got an email from my brother asking about some programming tools for a project he wanted to try. He’s a fairly technically savvy guy, but has very little experience programming. He had asked a couple of questions which made assumptions about the lineage of some modern programming languages — assumptions which are totally reasonable given the names, but which didn’t reflect the actual nature of the languages.

This post is based on the email response I sent him.

Disclaimer: I realize that I have glossed over a number of technical details, and even introduced some of the concepts in a way which may even have some technical inaccuracies. This is not intended to be a technical manual, simply an introduction to a technical topic in terms that most non-programmers should be able to figure out.

Typically there have been two primary types of programming languages, compiled and interpreted. The source code of a compiled language is read by a lexer, parsed and then re-written into low-level machine instructions which can be executed directly on the hardware involved. Compiled languages almost always need to be recompiled for each individual platform because the physical instruction sets of Intel (x86), SPARC, ARM and other processors are all different. Operating system calls are also different. This means that code compiled to run on an Windows-based Intel machine won’t run on a Solaris-based SPARC machine.

Interpreted languages are not compiled. They are executed as they are read by some other process. These are sometimes called “hosted” programs since they don’t execute natively on the computer which is running them. The host process (web browser, game, or other runtime environment) reads the script line by line and then takes the appropriate action. So it’s the host process which actually reads files, communicates with the Internet or displays graphics on the screen. The interpreted language (script) is little more than a recipe. This is why differences in the implementation of the specification behind the script can cause such big problems. When you have 5 different web browsers which don’t quite agree on how to execute a particular construct of JavaScript it’s like the chef’s at 5 different restaurants having a different idea of what a medium-rare steak is. Sure it’s nice if one happens to do things the way you want, but you’ll never know until you try them all.

C is like the grand-daddy of modern languages. It’s curly-brace syntax pervades many modern languages (C++, Java, C#, JavaScript and many others). It is, however, a much lower-level language providing direct control of many system resources. C can also be optimized for speed and does not explicitly require any external frameworks or libraries to work. It’s a good language to have a grasp on, but may not be one you would ever use on a day-to-day basis.

JavaScript (a variant of and predecessor to the ECMAScript standard) was a language developed by Netscape in the 1990s to be a part of its web browser. Aside from the curly-brace design and the name, JS has absolutely nothing to do with Java. Until very recently, JS was purely an interpreted language. Its domain was to live inside the browser and help animate funny little things on screen or possibly display messages as you filled out a form. It’s only in the past few years that JS has really taken on a more leading role as massive libraries of complex JavaScript (jQuery) and people doing some seriously cool stuff with the language have led to uses of JS outside the browser. The node.js project is a perfect example. Node (whose executable is written in C) will serve as an engine for running JavaScript from the command-line much in the same way as Python, Perl and PHP do.

Just as most rules are made to be broken, so is the rule about a language being either compiled or interpreted. There are some languages which are a strange (and powerful) hybrid of both. Java and C# are both compiled languages. The thing is, they don’t compile down to natively executable machine code. They compile down to an intermediate format which is then interpreted when the code is executed. This provides a mechanism for the compiler to optimize the code for faster execution, while also providing a mechanism for the code to be ported to other platforms with minimal modifications.

From a language perspective C# and Java are like half-siblings… both members of a generation of languages designed to help build large cross-platform enterprise business systems, which have been drawn out into other areas due to sheer popularity. Visually the two languages look almost identical, with similar features and a “C-like” syntax, but due to each one being built to operate primarily with it’s own native framework (.NET for C# and J2SE for Java) the source code is essentially incompatible with the exception of a few trivial examples.

This all brings me to HTML5. This term has to be one of the most overused, over-hyped and poorly understood technological terms of the past decade. The name would imply that HTML5 is a new version of the HTML specification, designed to replace the rather aged HTML 4 specification in use on most websites today. And technically, that’s exactly what it is. There is a new version of HTML with some new tags (like <;video>; and <;canvas>;) which will provide web developers with some new tools to create compelling website experiences. The problem is that there are a lot more things behind the scenes that really make the next generation of web platforms powerful. A new version of HTML is just the start.

The new additions to the HTML DOM (Document Object Model) bring with them more powerful capabilities for JavaScript and CSS to help code and style the way web applications work. The <;canvas>; element is great, but it doesn’t do much without some fabulous JavaScript code to do the heavy lifting.

The next iteration of the CSS will provide more versatile styling for websites, allowing designs to function both for the desktop as well as the dozens or hundreds of combinations of screen sizes and browser capabilities on modern mobile devices. There’s a big difference between the kinds of things an iPhone 4S can display compared to a 3-year old BlackBerry Bold — both of which I have on the desk in front of me.

To wrap this up I really wanted to thank my brother for asking the question and giving me the opportunity to examine this question in detail. It isn’t something that I think about in my day-to-day work with software, but it’s still something important that bears examining from time to time.

Accessing HttpContext objects from other classes

I could swear I wrote about this at some point in the distant past, but I couldn’t find the article this week when I needed it to help troubleshoot an issue with another developer. The issue he was having was how to access objects from the executing web page’s HttpContext object from a class other than the CodeBehind of the executing web-forms page. Essentially he was looking for a way to map a web-path to a physical folder path without needing to hard-code it or know where the application was deployed on the server in question.

If done correctly, an application can reside anywhere in the file system and be deployed to a virtual directory at any depth without causing a problem with URL resolution. In the code-behind of a web-forms page, the code is simple:

string physicalPath = Server.MapPath("~/somefolder/myfile.xml");

However doing this from another page involves just a little bit more work:

Using System.Web;
string physicalPath = HttpContext.Current.Server.MapPath("~/somefilder/myfile.xml");

It’s really quite straightforward when you see it, and I can’t believe that I forget how to do it. This method will also provide you access to lots of other useful objects which makeup the “state” of the application from an HTTP perspective.

Back to Basics

Over the past year my personal life as undergone some fairly major changes. I started a new job a little over a year back and there were the obvious changes that go along with that. But more importantly my wife and I welcomed our first child into the world and that was a life changing moment. Now, most of you know that I don’t talk about my personal life in the blog so suffice to say that we have thoroughly enjoyed our first year as parents. It is a wonderful experience and we eagerly await every new day to see what will happen next.

One of the things that changes when you have a new baby is the amount of time you can spend on yourself and your own hobbies and pursuits. I used to spend upwards of 4-6 hours every day outside of work on the computer blogging, coding, or otherwise toiling in one digital adventure or another. Now I find that the number ranges somewhere in the range of 0-2 hours per day. That is a pretty drastic reduction no matter how you slice it (about 80% for those of you scoring at home).

There are a number of projects that I have started and stopped over the past few years each of them trying to build a better mousetrap, or re-make something from scratch just to see if I could do it. With the limited time available to me now, I have become more focused on wanting to actually do more with the time I have — this means not reinventing the wheel every chance I get.

My wife and I have both found that we have become far more effective with our time, getting more done with less time than we ever have before. In the past couple of months I have started to extend that to my digital life as well. Gone are the days when I focused on a writing a to-do list, a backup utility, a blogging engine, a photo manager or a disk-erasing tool. There are lots of great (free) tools out there which can handle those tasks very well, even if they don’t satisfy all my neurotic desires (like how my historic completed work tasks should be handled, cataloged and stored for reporting purposes (you know, for when I will pull metrics on my completed work)).

I have also decided that diving in to learn a new, modern programming language is probably something that would realistically take more time than I’m willing to devote to the enterprise. Python, Ruby, Java, and the ASP.NET MVC framework are all on my list, but are undergoing changes and enhancements so frequently that I’m having trouble keeping up with what’s out there, nevermind trying to actually learn the stuff. But I do want to become a productive programmer in some language outside the rather constrained, and somewhat self-imposed, .NET bubble in which I have spent the majority of my professional career. Ideally I would like to write in something that I can port between operating systems without too much headache. Being able to produce code that will run on anyone’s machine is a great asset — especially when you have Windows, Mac and Linux machines in your own house to start with.

So the question is what can I learn that will allow me to:

  1. write code for multiple platforms
  2. grow as a developer
  3. not have to keep up with constant enhancements

The answer I came to was 42 C. It seems to satisfy all of the criteria above for me in a way that other languages don’t.

C is by nature intended to be a multi-platform system. If you’re able to confine your applications to CGI or the command-line this is made even easier.

C also requires developers to know much more about how computers and compilers work than more contemporary languages like C#, Java or Python. Though it arguably makes programming more difficult, I think it will help me become a better programmer over time as I learn some of the trickier parts of getting a computer to do what I want it to do.

The current ANSI standard specification for C was introduced in 1999. This means that for the past 12 years, the standard for C programming has remained essentially unchanged. This makes C a good choice for someone who doesn’t have a great deal of time to keep up with changes and enhancements in the specification.

For all these reasons, and my own simple curiosity I’m embarking on an adventure to learn and become proficient in C. I make no assertions that I’m trying to master the language as I can’t see myself getting beyond the hobbyist or perhaps open-source contributor stages. I do have some ideas for the first couple of projects I would like to tackle once I get the basics out of the way. Hopefully I’ll be able to release some source code back into the world over the next year or two — after all, I’m in no hurry.

C# IsNumeric implementation

Here’s a quick and dirty implementation of “IsNumeric” in C#. This is one of those methods that just seems to be missing from C# which appears in so many other languages.

UPDATE 12-Apr-2011: After some fantastic discussion elsewhere I’ve modified the code to handle a number of additional scenarios. A point was also raised that a combination of Int64.TryParse() and Decimal.TryParse() would accomplish the same thing. They would, almost, but those methods test for valid 64-bit integers and valid 64-bit decimals — they don’t test whether a string is numeric. Feed them a long enough string of numbers and they’ll return false. It’s a pretty fine distinction, I grant that, but I figured since I was writing the code I might as well make it as robust as possible.

        public static bool IsNumeric(string s)
        {
            return IsNumeric(s, false);
        }

        public static bool IsNumeric(string s, bool allowDecimal)
        {
            bool result = true;
            if (String.IsNullOrEmpty(s))
            {
                return false;
            }

            if (s.StartsWith("-"))
            {
                s = s.Substring(1);
            }

            char[] chars = s.ToCharArray();

            if (allowDecimal)
            {
                bool decimalFound = false;
                foreach (char c in chars)
                {
                    if (c == '.' && !decimalFound)
                    {
                        decimalFound = true;
                    }
                    else
                    {
                        result = result & (char.IsNumber(c));
                    }
                }
            }
            else
            {
                foreach (char c in chars)
                {
                    result = result & char.IsNumber(c);
                }
            }

            return result;
        }

I built 14 39 unit tests for this on the project I built it for throwing all sorts of weird and null data at it, and it seems to run fairly well and reasonably quickly. Any comments/suggestions are welcome.

Announcing EpubSharp

Over the past few days I’ve put some time into working on a library to create EPUB documents in .NET.  When I first did a search for this a few months ago I really didn’t find anything that suited my needs: a library that I could use to create EPUB documents on the fly, in code.

So I said to myself: “Self! You can write code, build the damn thing yourslef!”. So I did.

The initial version of the library has been published up on Google Code and is probably full of holes. If you’re interested, have a look and let me know what you think.  I’ll try to publish some more detailed specs for what the library does in the coming weeks.

For now, it can get got at: http://code.google.com/p/epubsharp/ — and yes, the documentation on that page is as sparse as it is here.  :)

New Podcast: Short Order Code

Over the past several months I’ve mentioned occasionally that I’m working on a new software development focused podcast. The time has finally come for me to make a more official announcement about this new endeavour.

The new show called Short Order Code. The show will be a
series of audio podcasts and video screencasts focusing on a number of
areas of practical software development.

The first episode is out now, and the next few are in various stages of being produced while I put together the final bits and pieces and trying to sort out my process.  Once that’s done and the first few episodes have been put together
I’ll try to provide more information around long-term plans for the
show.

For those who may be wondering, this is not going to affect my
affiliation with the Aussie Geek Podcast (despite our . I love doing the show, and plan to continue in my co-hosting role for the foreseeable future as long as the AGP community is willing to have me. Time zone considerations will probably continue to make a mess of our schedule, but we’ll do our best to produce a great show for the community as often as we possibly can.

So keep your eyes peeled for more information, and head on over to the website at shortordercode.com and subscribe to the feed!

Honing the Craft

We were on vacation for a couple of months back staying with my wife’s family in the US.  I didn’t completely unplug during the trip, though I wasn’t checking work email, my personal accounts were down to a cursory glance once-per-day, and I’ve dialed back on the Twitter usage quite significantly.

In some of my relaxation time (while babysitting my sleeping 7-month old niece) I got the chance to do a bit of software development. My brother-in-law and I got to talking one night about a project he was hoping to get some help with. He had asked me as an adviser, and since the work was similar to things I’d done for work in the past I felt compelled to offer to help.

The project itself was simple enough: take this text file and put its contents into an Access database. The process has reminded me about why I like software development, and why I miss it.

Over the course of the next couple of weeks we went through a half-dozen iterations of the application, much in the same way that I would have worked with end-users inside our business unit at work.  It allowed me to work through a problem, understanding the true nature of the business problem that he was trying to solve, not necessarily just the problem as it was described in the initial requirements discussion.

I had the chance to pull out skills that I hadn’t exercised much in the past couple of years since my job role has changed, which was a great shift for me.  It’s given me the push I needed to get the podcast going, and to dive in to a few of my other projects with a bit more gusto.  Software is a craft, and its one that I need to continue to hone as time moves forward.  I could probably be considered little more than an apprentice right now, but with the entire Internet as my journeyman instructor I should be able to earn my ticket in the months to come.

Regaining my Chops

It’s been a good long while since I’ve written much in the way of production code. I’ve tinkered here and there with some small side projects, but rarely have I given much thought into how those pieces of code were written so long as they got the job done; after all I didn’t expect that the code would ever leave my own desk.

During a recent vacation I had the opportunity to help my brother-in-law with a project. It took a few iterations but we managed to get some code written which fulfilled the need in just a few nights’ work. Though the complexity was about the same as my own small personal projects, this project was different.  It was for a client — albeit a client who I wasn’t charging money for the work.  Someone else was going to read and maintain this code so I knew that I had to take some measures that I hadn’t been used to taking recently.

The first was refactoring. I refactored the crap out of this application — and for the most part things worked out nicely. I was able to turn all my poorly named and one-character variable names into elements and objects with meaningful monikers.

Commenting was the next step.  After the refactoring was completed I could move on to the next step of the application which was to put in some descriptive comments in the applications.  These comments describe not the what, but the why.   This is the approach I’ve tried to take relying on good naming within the application to describe the what, and comments to provide rationale.

The final thing I did was take my brother-in-law through the code.  He has a background in software but has never done much in the way of OO (he writes Cobol) and hasn’t ever worked with the .NET languages.  In doing the walkthrough we came across a few minor issues and a couple of small enhancements for the application.

All told I spent about 25 hours working on this project, and in that short amount of time, I really did begin to regain my programming chops.  I’ve got a ways to go yet before I get to a level that I’d be comfortable with, but it’s a start.

Stack Overflow – Where Experts Exchange Information

soLast year a new Q&A site for developers called Stack Overflow was opened to the public. We covered the site a bit on an episode of the AGP a few months back, but I just realized the other day that I hadn’t posted about it here.

The site is focused on the interaction between software developers which is truly how most of us learn the best — by working with, interacting with and drawing on the wisdom of other developers.

The site draws on a whole bunch of different elements that set it apart from other Q&A sites.  Creator Jeff Atwood describes Stack Overflow as a free Q&A site that’s built and maintained by the community of developers.

While the site was built to answer developers’ questions, it also has some stiff competition in the form of the Experts’ Exchange.  Most developers know about EE, and are annoyed by the fact that the site purports to charge money for access to the answers to development questions.  As it turns out, it’s all available for free, you just need to look around a bit harder to find stuff.

Stack Overflow has been built with transparency and ease of use in mind since its inception; the model is to get as many eyeballs on a question as needed to get a good answer.  Good questions and good answers are up-voted by the community — similar to the way Digg works, except it’s harder to game the system.  Up-votes provide reputation points which at lower levels unlock some of the site’s features.  The site also gives out badges for meeting certain goals on the site, based on the ability to get badges or special goals on Xbox live.

Jeff Atwood’s passion is around developing software from a more human perspective. Much of the design of the site for Stack Overflow, and the code behind it are based on driving positive behaviours within the developer community.  Instead of lots of hard-and-fast rules, there are easy ways to do good things, and more difficult ways to do things that shouldn’t be overdone.

As a side-note, Stack Overflow’s codebase is written in C# using the ASP.NET MVC framework, and has been in use since the very early CTP days of MVC.  It’s a great example of the power that can be brought to bear on the web with this toolset.

I love the site, it’s been a great resource for me for the past year or so, and I highly recommend it to anyone who has a development dilemma that they need to solve.

AnkhSVN and Visual Studio 2008

ankhsvnSource control is one of those things that developers get really polarized about.  Most agree that having source control on projects is a necessity, but that’s typically were the similarities end.  Some folks are of the mind that every line of code, however insignificant, should be under source control.  This provides records of what was written, and a reference for things that were done in the past.  Others believe that source control should be reserved for “real” projects, things that are deliverables for customers, or products to be released to real-world environments.  I really don’t want to get into this debate tonight, so I’m going to stick to the technology.

I was wanting to get some source control in place for a few of my personal projects.  I chose to go with Subversion for my source control server for a few reasons, not the least of which was that my hosting company supports auto-configuration of SVN repositories, so I was able to get that set up in just a couple of minutes.  That left me some time to contemplate how I would access the repository from the client.

newproject_svnI’m running Visual Studio 2008 on my development machine and this gives me the ability to use plugins for the IDE, a feature that is sadly missing from the express editions.  There were a couple of good options available for SVN plugins, VisualSVN which is the 800lb gorilla in this space, and the open-source option CollabNet’s AnkhSVN.  Given the fact that this was for personal exploration of the toolset, the open source (free) option was the obvious choice.

The setup for AnkhSVN was quick and painless, and when the IDE opened up it put options for source control right in the menus where they were nice and easy to find.  I created a project, and selected the “add to Subversion” checkbox, entered the necessary credentials and created the project in my SVN repository.

anhksvnWhen in Visual Studio, the AnkhSVN controls are located on a tab at the bottom of the IDE, alongside other solution-wide functionality like the To-do list, output window etc.  This pane tracks all of the changes (adds, deletes and updates) that you’ve made to the solution files.  This is extra handy as a review when you’re ready to make your commits back to the repository.  By quickly scanning the list of changes you’re able to write solid commit comments to provide some decent documentation for you, or those who come after you.

I’m still relatively new to Subversion and AnkhSVN, but I’m looking forward to exploring them in more detail — maybe I’ll even do a podcast episode about it!