Designing Client Facing APIs – Best Practices

September 30th, 2009

With the popularity of service oriented architectures and other buzz phrases related to software as service, good API design has become a significant selling point for any software platform in the past 5-10 years.  People make purchasing decisions based on how easy it is to interoperate with your applications and code and as such the number of client / public facing APIs attached to software has skyrocketed.  I’d like to believe the days of dropping strategic text files in directories to trigger some action or another in an application are behind us.

In this article I’m going to talk about the following things

  • Why you should choose your method names carefully, and what to call
  • Why pretending to be a data access layer is a terrible thing for an API to do
  • Talk about the dangers of leaky abstractions in an API
  • Explain the benefits of creating a data contract between you and the calling code
  • Explain why it’s vital to support standards
  • Make sure that your users can retrieve values they’re going to want to modify
  • Suggest supplying compiled libraries alongside your API documentation
  • Explain why it’s important to keep your API implementation clean
  • Talk about the benefits of dogfooding your API
  • Consider supporting atomic operations including rollbacks on failure
  • Discuss bulk operations
  • Try and convince you that both logging and security should be first class citizens
  • Beg you to maintain integration tests and most of all to keep it simple!

Your API Sucks

You’ve probably used an API and there’s a good chance you’ve had to write one.  This probably won’t surprise you; most APIs suck.  They’re horrible to use and built around illogical leaky abstractions that leave you flicking through huge wads of documentation just to make the most rudimentary feature work.

About 18 months ago, after a year of struggling with a broken third party API that almost brought a business to it’s knees by placing significant roadblocks in front of in house development, I was part of a team tasked with designing our own client facing API.  With no desire to expose other developers to the cruel and unusual punishments of software design we’d had to endure, we came to the conclusion that it was really important that we god this piece of the system right.  People say first impressions are everything, and your API design can make or break the faith other developers have in your ability to produce software.  Show somebody a shitty API and they’ll perhaps correctly assume the rest of your code sucks too.

The Best Man For The Job

There’s a bit of a trend that I’ve noticed with some of the worst APIs I’ve worked with: they seem to be designed by the wrong people.  The wrong people to design an API are 1) the guy that wrote the internal code to do the job the API is providing access to and 2) the consumer of the API. 

The guy that wrote the code that the API is calling under the hood will be inherently slanted to implement an API which exposes this functionality and will have a predisposition to creating a leaky abstraction.  This is especially bad for the consumer APIs designed by the internal implementer tend to assume the consumer knows far more than he really does, or has access to internal data that in reality, he doesn’t.

Conversely, an API designed by the consumer of the API will have a tendency towards solving problems that are not the concern of the API itself.  The consumer will, either accidently or by intention, attempt to offload some of the work that should be the responsibility of the calling code into the API.

Ideally, the person that’s writing the API will have knowledge of the system internals, but not be the guy that wrote them. A fellow team member with passive experience to the code would be a good person, or ideally, a pair design exercise between the person that originally wrote the code and an API designer, with the consumer as a consultant.

Speaking The Same Language

Like a lot of software development, you make good progress when you get your terminology right and understand exactly what you’re trying to produce.  I’ve consistently found that the best way to think of a client facing API is as a orchestrating thin wrapper that summarises, in code, a set of business processes that you wish to expose to the public.

In order to get your API design right, you need to clearly define and agree on the boundaries of the system with both your internal team, and your consumers.  It’s important that you have a clear understanding of the following:

  • The responsibility of the calling code
  • The responsibility of the API layer
  • The responsibility of the internal code the API makes calls to

This might sound like a really simple suggestion but I’ve taken part in countless discussions where people on both sides of the API just “presumed” that either the calling code or the API would perform specific functions (data cleansing, logging etc) when in fact, this confusion had lead to none of the implementers bothering to write the required functionality.  Make sure you know for certain what your API is responsible for doing.

Defining Your API – Tips and Tricks

Defining your API methods (or the “contract” of the API) is the most important thing to get right and there are several vital things to remember.

  • Choose Your Names Wisely Using the language of the business

    It’s vital that your API methods speak in terms that the caller is going to understand.  Your API should be readable.  If your users go hunting for the documentation every time they want to use a method, then you’re probably doing it wrong.

    Clarity in naming is exceptionally important.  The names of your API methods should succinctly state what action that method call is going to perform.  Don’t fear using long method names, embrace them for clarity.  As a general rule, your pmethods should probably always be in the form DoSomething(object withThis);

    Ensure that when naming methods you reflect business operations in the method names, not the underlying implementation.

    Bad example:     void InsertToTblCustomer(string[] custDataValues);
    Good example:   void AddCustomer(Customer customer);
    Good example:   void DisableAccount(string accountId); 

  • Don’t pretend to be a data access layer

    APIs should summarise business operations in a logical and meaningful fashion.  You are not a public facing data access layer and you should never pretend to be.  If your users want raw database access give them read only permissions on some tables and a copy of SQL Management Studio.  So don’t write methods for CRUD operations in your API (unless you’re writing some kind of online file management utility).

    Bad example:     void InsertToTblCustomer(string[] custDataValues);
    Bad example:     void UpdateTblCustomer(string[] custDataValues);
    There are no good examples!

  • Avoid leaky abstractions

    This is a fundamental and simple rule – don’t expose your callers to anything that they’re not interested in or won’t understand.  If it’s not important, don’t show it.  Don’t code for things nobody will ever need and don’t require your callers to have intimate knowledge of data types or internal categories in your system.

  • Create a data contract between you and the calling code

    I’m going to borrow some of the terminology from WCF here because I’ve found it an appropriate label.  Create a Data Contract library for use in your API.  This library should summarise the business process and the outward facing view of your software.  It might contain terminology that doesn’t actually exist in the software itself, but in the business processes that the software models.  Either way, this, and only this, should be the language that the API talks to your callers.

    Where possible, create this data contract in a separate assembly that’s entirely decoupled from your core system and distribute it to people that want to use your API.  This is especially beneficial when using WCF as your clients can generate a service proxy and deal in the same data types that you are in your API code.

    It should be the responsibility of the API layer to marshal the data from your data contract into the domain model of your internal components.

    You data contract should contain every type used to communicate with your API and the object model should be named in a way which is meaningful for the consumers.

    Because your data contract is NOT the object model of your internal components, you’re able to add properties and objects that don’t logically exist in your internal components.  This means that you can perform an operation using some internal component, gather the output in your API layer and then compose the output data in a meaningful way using classes written specifically for the data contract.  This way, by the time the user has access to the output data, it’s in a format and language which they understand.

  • Support standards!  Don’t reinvent the wheel!

    Here’s a true story; while working with an API, my team was faced with the following API method:
    object Run(string request);

    It was the only method on the API, and “covered up” for around 30 methods all made available through one giant black hole in the side of the system.  Underneath that there was an XML format that the request had to be in in order to call the appropriate method.

    If you’re writing an API, stick to some kind of standards.  Ideally, expose a web service endpoint with an accurate WSDL that people can call or use a simple and obvious REST endpoint.  

    Please, please, please, do not make other developer suffer by rolling your own delivery mechanism.  We have enough of them, don’t confuse people by adding some more.  If you’re going to use a raw sockets connection, supply a calling library and stick to some standard middleware like WCF rather than rolling your own. 

    Thousands of people have spent thousands of man years writing code based on existing techniques, you’re not better than all of them combined.

    Reinventing the wheel is never good for anyone.

  • Ensure that the user can retrieve values they’re going to want to modify

    The number of times I’ve used APIs that let me set or update objects without returning the current state of the object is mind blowing.  So always remember: If you’re going to let them set it, let them get it.  Providing an update or “upsert” method without allowing your consumers to query the current state of data in the system is a complete waste of time.

  • Supply compiled libraries to work with your API documentation

    This may not always be possible, but it’s a really good idea to supply a sample implementation and compiled binaries with your API that covers the most common scenarios of usage.  Not only does this prevent the consumer from struggling to get to grips with you API, but it allows you to outline and illustrate a set of best practices for usage.  In an ideal world, the user could just use your sample code in their application, so ensure you license the code appropriately.

    This is an exceptionally good way to deal with any authentication your API may require as you have the ability to provide additional helper classes to perform some common tasks (authenticate –> perform action –> logout, for example).

  • Keep your implementation clean

    Delegate the API logic to your middleware components / reusable libraries.  Do your best to ensure the API layer doesn’t actually contain the logic required to perform operations, just the logic required to marshal the data from the API format into your internal data structures.  The API should simply orchestrate calls to one or more internal methods because your API should simply be exposing existing functionality.

    If the API is exposing some new, API specific functionality, consider splitting this behaviour into a separate assembly or binary to aid testability.

  • Consider dogfooding

    Dogfooding is the act of using the software you’re creating.  I worked on a project where we were developing an order placement system in ASP.net MVC, and as part of the design process we decided that we wanted to have an API that was a first class citizen.  It then dawned on us that in order to produce the API in a way that accurately mirrored the functionality of the website, we should have the website consume the API like any other client would.  The website had it’s own concept of user authentication, and when a user logged in, the web application logged in to the API as the current user.

    Doing this not only ensured that our security model was watertight, but that any additional web functionality would immediately be available to API users because they were actually the same thing.  On top of that, you gain confidence in your own API because you know that it’s called often by your own code, reducing the likelihood of users discovering bugs in your API because it’s not a product you actually use.

  • Support atomic operations including rollbacks on failure

    When implementing your API methods, ensure that if an exception occurs or an operation doesn’t complete, the all the changes made by your API call are reversed.  Consider explicitly supporting transaction scopes in your API to let your consumers compose their own “set” of operations.

  • Support bulk operations where appropriate

    Building support for bulk operations into your API can often prevent performance issues occurring later when a user tries to, for example, insert 10,000 customers sequentially.  Consider pluralising your methods, so instead of providing an AddCustomer(Customer customer) method, provide only a AddCustomers(List<Customer> customers); method.  Doing this prevents callers from overloading your system by bulking data through your API in unintended ways, allowing you to properly cache required data and cater for these bulk operations.

    This isn’t always appropriate, however I’d always strongly suggest offering pluralised versions of methods that you suspect may be used in bulk, in order to help optimise your API calls and reduce the amount of data being transferred over the wire.

  • Logging as a first class citizen

    Don’t wait until somebody asks about API usage to decide to log it.  Build logging into your API wrapper, from the start, at the point of every method call.  It doesn’t need to be fancy, and you can use a number of freely available components to handle these logs and log rotation (consider using log4net or log4j for simple log rotation).

    Log each method call and some summary or identifiable element of the data passed to it.  This’ll help you profile API usage, and identify how data changed in your previously closed system.

  • Security as a first class citizen

    Consider the security of your API from the start of the project.  Understand who will have access to your API, which organisations and which individuals.  Do you require roll based security?  Do you need a way to disable API support for specific customers?  Are you transferring data that needs to be encrypted over the wire?

    Beware of over baking your security.  WS-* offers some very robust packet level security features, but if your API doesn’t need them, or is restricted to an internal network, then don’t bog down your implementation in unneeded security.  Beware of making security choices that tie you down to a specific protocol or technology stack – you want to keep your API usable for the consumers.  Do the simplest thing that works.

  • Have integration tests!

    Make sure you have integration tests with mocking at the business logic layer. These tests are for your API wrappers, NOT your logic. The logic should be tested independently, you’re just ensuring your API layer, marshalling and method calls operate correctly at this level.

    With any luck, your business logic should already be tested as part of your existing test suite (which you have right?) but if not, ensure the business logic is tested separate from the API code.

    Consider using a TDD or BDD approach to designing your API calls, designing the specification first in the form of some calling code, then write the code required to make your usage examples compile.  This will help you understand exactly what calls the client will have to make for to your API to achieve specific functionality.  These tests can happily double up as regression tests when you make changes to the API.

  • Keep it simple!  If all else fails, do what the big boys do.

    Always strive to keep your API simple.  Pretend your the consumer at all times.  If you’re unsure of how to proceed, I’ve always found inspiration, both for what to do and what not to do, from reading the API documentation of some large companies that have widely used APIs.  It’s safe to say that the likes of Amazon, Google and Microsoft have had to put some thought into their API designs.  Beware of trusting their decisions blindly, but liberally borrow anything you, as a consumer, would find pleasing in your API.

I’m not going to try and convince you that by following my advice that your API design will be flawless.  I’m really hoping for a little discussion on this topic as it seems like something that is rarely covered and often “felt out” by the people left to implement APIs for the public.  These are just some lessons I’ve learnt on the way while implementing several public facing APIs.

Want to talk about APIs?  Send me an email!

In defence of current-gen game design

September 5th, 2009

I feel frustrated at the moment by the endless cyclical debate on the internet (in this case, a comment on Kotaku) claiming games are creatively plateauxing due to their middle age.  People seem incredibly pent up on the games-as-art debate to the extent that they seem to deride anything that the "it’s just a game" argument could possibly be justified by. 

I want to open this up with a really simple sentiment: I love progressive, narrative driven games, but I sure as hell enjoy playing games that are just a game.  Sometimes I just want to be entertained.

The precise comment on Kotaku (by HarlequinRiot) "The industry needs to rethink what a game can be and use all this amazing technology to make new experiences that may not be so stratified as "this is you, go here, do x, listen to y". Gaming needs it’s Moby Dick or its Brothers Karamazov, as its Citizen Kane’s come every few years." actually struck me as opposing the notion of a "game as a game".  Games have rules and boundaries, a game without a good rule set is just "fucking about", and that’s why these kinds of games are either experimental or don’t exist - they most certainly don’t make money. 

I don’t always need a new experience.  I actually believe that amidst all these calls for innovation and change, that gaming is in the midst of a renaissance of innovation.  The industry is old enough now to learn from the successes of the past, while incrementing in small steps every year.  It’s really a shame to suggest that just because the core gameplay mechanic of two games are the same that they haven’t innovated in subtle ways that will be gathered into the collective unconsciousness of game development.  The simplest examples of this in action are the incremental games - look at any yearly sports title or Halo title or Unreal title, and you’ll notice little innovation year on year, but over 3 or 5 years there’s no way you can argue that you’re playing the same game.  I think people drastically undersell the innovation in game design that happens every day.

On Michael Abbots Brainy Gamer confab (part 2) posted yesterday (which I’d strongly recommend), one of the participants was strongly arguing the point of authorial intent and influence over a game property, implying that regardless of agenda, that a controlling authorial voice on a game imparts part of their world view on a product.  While I agree this is unavoidable in narrative works (especially in the likes of books and cinema), I honestly believe it’s a stretch to state that the guys that made Trials HD or Rocket Riot somehow accidently imprinted their world view on their products.  Even if the product is an inadvertent result of personal ideals, the strict confines of a game system oppose accidental messages in game design.

When the games that gain critical acclaim are generally none of the things that the critics seem to desire, it certainly shines a light on the critics themselves.  You can’t celebrate Super Smash Brothers Brawl or Trials HD, while complaining that a games like GTA4 or Gears of War is thematically simple, and then in the same breath complain that games aren’t progressive or sophisticated enough.  Doing so is pretty much the height of hypocrisy.  If you send these kinds of mixed messages to the people who write games without realising that there’s a natural path of development that must be travelled to get the medium to where you’d love it to be.  Games like GTA4 and Gears, even Bioshock, might not be "all you want them to be" but they are progressive thinking games that try to push both technology and storytelling forwards, even if their respective stories suck.

Likewise, it’s unfair to say that games aren’t sophisticated because they ape cinema, when actually, the most successful and celebrated "intelligent games" do so by emulating cinema and books.  Nobody seems to have a clear vision of what they think "sophistication" in games should really be.  Sophistication is often seen as production values by the mainstream gaming-media when really, the sophistication of the games rules and systems are a "purer" sign of an progressive game. 

I love narrative in games, but you can’t criticise a game for aping the conventions of cinema to achieve a strong narrative without stopping to realise that they only reason they ape that ape these conventions is because they’re effective, you can’t have your narrative without it.  This is especially noticeable when games attempt to forgo book / cinema narrative conventions in exchange for something more game oriented, when this happens you end up with Braid.  I loved Braid and I thought that it was fairly unique from a story perspective but flawed in the telling. The way it told its story played right into game mechanics, and as a result, the story came across as fragmented and confusing to the majority of players because the game mechanics used to present the story don’t naturally lend themselves to story telling.  When backed up by the authors refusal give anyone the answer, much of the message was lost. Regardless of authorial intention, if lots of your audience don’t understand the story or narrative you’re trying to convey, you’ve failed as a storyteller and end up being accused of pretention for doing something "different".

A more recent example of "emergent storytelling" would be the well reviewed "The Path".  I enjoyed playing the path, but honestly, it utterly fails as a game.  It’s a terrible game.  Interesting and thought provoking as an experience, but an horrible game.  The controls are awful (practically digital, PS1 era 3d game controls), the interface is counter intuitive and the tasks are deliberately obtuse.  I still enjoyed it and rate it highly, but it failed at being a quality game while succeeding as being a quality experience. 

I think that really sums up a lot of the progressive discussion on videogames - people want something more from their entertainment, but it isn’t games.  Maybe the engineer in me is being pedantic about naming, but I’d much rather "interactive entertainment" for software like "The Path" than "videogame".  Game is a loaded term, and along with it come certain expectations; a set of rules, some gameplay mechanics and a way to progress.  I believe games can be more, I believe games can tell stories, make you feel and make you think, but I don’t think those are required aspects of a good game, games can be "good" outside of those constraints, as an exercise of entertainment through gameplay mechanics.  You can’t criticise a game for being too much like a game, it’s like criticising a book for not being a film.  I really believe that a game can involve art, but if the game "becomes" art?  Well, then you’ve got art that’s art first, game second.  That’s valid expression, but it’s intention is to be art.

On the same podcast there was more talk of the recent debate around Shadow Complex, and the reaction of people towards Orson Scott Cards involvement in regard to his personal politics.  Just a quick note really; the mainstream doesn’t care about your protests.  I really mean that.  You’re telling me that you REALLY think people will boycott Activision because of their (sexist) Sin To Win advertising campaign?  Really?  It’s "just" marketing.  That doesn’t mean I don’t think it was excluding, but what it does mean is that I see sex and inequality used to sell products every single day of the week.  If you stopped buying everything that a person you disagreed with had worked on or had sex used in it’s sales material you would run out of things to buy and games to play pretty quickly.  And that’d be just you, because of the people that know about some perceived protest-able injustice so insignificant, not only will half of them not care, but most of the people that do care will do whatever is most convenient to them when a product comes along that they’re interested in.  This goes doubly for marketing, an industry that frequently abuses both it’s position and people to sell product.

While this may seem like a negative response to the confab podcast, it really isn’t.  I enjoyed listening to it, as I have in the past, and have plenty of respect for all of the people involved, some really interesting thought provoking stuff on the direction of gaming comes out of it and I’m really looking forward to the following 3 episodes.  The Brainy Gamer is still one of my favourite gaming resources on the web and certainly occupies the most prominent position in an interesting discussion.

There are plenty of quality, innovation filled games on the market, many of them produced recently.  As for most of the critics?  Part of the problem and not part of the solution.  It’s all well and good to criticise game design inadequacies, but until you’ve really considered the design of compelling, fun, game systems as part of your argument, I’ll write off your "innovation is dead!" arguments as hot air.

Localizing ASP.Net MVC Pages without the need to RunAt=”server”

August 20th, 2009

A common complaint when faced with localizing ASP.net MVC pages is that littering your code with tonnes of runat=”server” tags breaks the “purity” of the MVC model.  Regardless of how meaningful that debate is, there is a way to achieve globalisation without server controls.

I’m going to walk you through a sample implementation which should hopefully make this clearer.  As a standard disclaimer, none of this code has been tested in a production environment and I wouldn’t advise implementing it blindly.

I’m going to attempt to play this out mostly in screenshots…

The Idea

  • The required language is stored in a database / session / extrapolated from the Url route information
  • Your view pages derive from the type TranslatableViewPage
  • This page adds support for a LanguageCode property that you can make use of inside the view along with adding a class implementing ITranslator, a class that provides hooks into a translation database.
  • Your controller derives from the type TranslatableController.
    • This controller add a method called ViewInLanguage(string languageCode) that you use instead of View() or View(model) to return your Asp.net MVC view.
  • At application start up in the Global.asmx file, you register a default language code for failure conditions, and specify the implementation of ITranslator you wish to use to acquire localized strings.
  • If your TranslatableViewPage when rendering your output, you simple need to use the embedded <%=Translator.Translate(propertyName) %> method call to load a translated string while the page output is rendering.
  • Class Layout

    The following is built upon the standard Asp.net MVC starter project for the sake of illustration.

    image

    Implementation

    First, configure the translation settings and register your translator…

    image

    You need to configure a fail-safe default language code and a type that implements ITranslator.  This translator will be responsible for doing the heavy lifting.  Ideally we’d add some inversion of control here to allow you to define the translator implementation at configuration time, but that’s outside of the scope of this example.  I’ve implemented a very crude resource .resx translator for the example.

    Next, make your controllers inherit from TranslatableController

    image

    Once inherited, switch from using the View(); method to ViewInLanguage() passing in your desired language code (gathered from the user session / database / Url route).

    Then set your view type (or derive your view from) TranslatableViewPage

    image

    Once your view is inheriting from TranslatableViewPage, you’ll have access to an instance of your specified Translator and the LanguageCode inside the view which you can use in a manner similar to the built in HtmlHelper class to access your translated strings.

    How It Works

    The TranslatableController provides you with ViewInLanguage and when called generates the standard MVC ActionResult.  This ActionResult will be a ViewResult, which is then wrapped in a LocalizedViewResult wrapper class adding a LanguageCode.

    When ExecuteResult is called on the LocalizedViewResult to render the view, the LanguageCode is placed in the TempData array in the TranslatableViewPage.  Then, when the OnInit(EventArgs e) method is called on the TranslatableViewPage, this LanguageCode is extracted and placed in the LanguageCode property on the page.  In addition to this the page provides a constructed instance of ITranslator which you can use in your views to source translated data as the page is rendered.

    Source Code

    Download Here

    Xml Comment Hell – A Software anti-pattern

    July 6th, 2009

    One of the most valued practices in software development is brevity.  Writing code is bad.  When you write code you create bugs, and creating bugs is bad.  The solution?  Don’t write much code.  Commenting your code however, has traditionally been seen as a “good thing”.  So much so that most modern programming environments make some provision for document generation and offer style-guidelines for commenting your code.

    I believe, however, that excessive commenting is actually an anti-pattern and should treated with caution, and as far as possible, avoided.  I’m going to illustrate my point with a (somewhat contrived, but in no way unusual or outlandish) example.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace XmlDocumentationAntiPattern
    {
        /// <summary>
        /// XmlCommentHell
        /// </summary>
        /// <example>var xmlCommentHell = new XmlCommentHell</example>

        public class XmlCommentHell
        {
            /// <summary>
            /// Description
            /// </summary>
            /// <remarks>String description of XmlCommentHell.</remarks>
            /// <example>instance.Description = "some description";</example>

            public string Description { get; set; }

            /// <summary>
            /// XmlCommentHell ctor
            /// </summary>
            /// <param name="description">Description string</param>

            public XmlCommentHell(string description)
            {
                // Assign description
                Description = description;
            }

            /// <summary>
            /// Gets the description property, but in reverse!
            /// </summary>
            /// <returns>Reversed description</returns>
            public string GetReversedDescription()
            {
                return Description.Reverse().ToString();
            }
        }
    }

    The context of this example is C# but similarly applies in other programming languages.

    If you find yourself engaging in XML comments like the above example, I really believe “you’re doing it wrong”.  Why?

    • Reduced code readability

      The Xml comment clutter in the code above actually reduces it’s usefulness.  The code now takes three times as much screen real estate to maintain and understand.

    • Low signal to noise ratio - too many characters that add no value

      When you’re maintaining systems however large, the act of actually writing code takes time and adds maintenance overhead.  Past the visual unpleasantness the amount of mark up required to document relatively simple methods actually reduces the codes utility.  Only ever type ANYTHING in your IDE if it adds value.

    • Incredibly obvious comments

      There’s a wonderful quote to the tune of “always pretend that the person that’s going to maintain your code is an axe wielding manic who knows where you live”.  I’d like to extend that by adding “so don’t insult their intelligence”.  There are few things I find as frustrating as reading comments that not only add no value but also make me feel like I’ve wasted a tiny piece of my life reading them.  Don’t waste your time or mine.

    • Violating the DRY principle

      The DRY principle is very well agreed upon in software development.  Don’t repeat yourself.  If your XML comments just repeat your method signatures, you’re not only repeating yourself (and thus wasting time) but you’re leaving yourself open to a maintained nightmare as the code changes and the comments are left unchanged leading to confusion and misinformation.

    • Possible misuse – the generation of utterly useless documentation

      When I see projects documented in this way I often joke that someone should run Sandcastle or javaDoc over the code to gain some tangible value in the form of MSDN-esq documentation.  I’m actually wrong.  This is a terrible idea.  Pop quiz time!  What’s worse than tedious documentation that adds no value?  Tonnes of hyperlinked documentation that adds no value!  Think of the poor developer wasting minutes to hours searching for the value in generated documentation before painfully realising that there’s none to be had.

    I occasionally get met with some resistance when I state my opinion on this for a couple of reasons.  People often argue that they do it for the sake of intellisense and IDE tooling, that they do it for generated documentation or just out of habit.  I feel like these are all dangerous reasons.

    Firstly, if you’re generating documentation for documentations sake, you’re either trying to please some form of middle management that doesn’t understand what documentation really exists for (hint: to help developers develop), or you’re wasting time.

    Secondly, if you’re documenting methods for intellisense you’re probably missing the point.  See, intellisense and other IDE self-help mechanisms are very good; they’ll display method signatures and method names with minimal fuss, any extra comments you glue on top of every method will either clutter your GUI, or become ignored white noise, potentially leading to that one important comment going ignored as your developers slowly desensitise themselves to reading the human generated “auto-doc” garbage.

    Finally, if you find that you need to cover your methods in comments because they legitimately don’t make too much sense at a glance then you’re falling foul of a different problem: you have code that’s not legible maintainable and clear.  In this case you’d be far better served by ensuring your methods are named well, take logical parameters and have single clearly defined purposes.  In my experience, whenever somebody says that they need to comment their methods because the method is unclear, they really should be refactoring, not documenting.

    Please DON’T bury your code in comments, just keep it all readable ok?

    How far has A.I. in games really come?

    June 1st, 2009

    There was a piece on Kotaku this weekend looking at the development at AI in games, It specifically set out to ask leading figures what they thought of the development of virtual “beings” in games,

    “Kotaku set out to ask experts in the fields of Hollywood movie magic, theme park creators, robotics experts and AI specialists to answer the question: Do the AI-controlled characters in games qualify as robots or some other form of artificial life. Are those creatures who are at the player’s mercy in Lionhead Studio’s Black & White games truly virtual beings?”

    Inside the article was a rather worrying quote from self declared futurist Thomas Frey, executive director of the DaVinci Institute,

    "In short, our games have indeed evolved into crude life forms," said Frey. "Innovations in the digital world are happening exponentially faster than in the material world, so the digital beings in games will soon become far more lifelike, and will eventually step out of the screens and exist as 3D avatars, interacting with us, much like other people."

    I always get a little upset and slightly concerned when people from places like the DaVinci Institute (from their "about us" page: "Launched in 1997 as a non-profit futurist think tank, the Institute has emerged as a centre of visionary thought, attracting both a national and international following of idea junkies and business leaders alike.") start to talk about things like programming and AI.  I always feel like these kind of think tanks highlight a bit of a problem with the development of new technology.

    All you learn is that "futurist thinkers" actually have no grasp on the practical implementation of the technology.  I’m all for creative thinking and speculation, but somehow implying that very simple game mechanics are going to develop rapidly and with little to no point into some Skynet like Ai is simply absurd. 

    Thankfully the Kotaku article actually asked someone with half a clue (Chris Darken, conference chair for Artificial Intelligence and Interactive Digital Entertainment and an associate professor of computer science at the Naval Postgraduate School) who equated videogame AI to an expert system.

    In all honesty it’s a stretch to even call them that.  The problem domain that your average game AI lives in is so rudimentary small that I’d rather use a phrase closer to "novice system".

    Game AI is nowhere near close to simulating human behaviour, near all "humans" are scripted, and the most simulated behaviour comes from games like Spore or The Sims where the actual behaviour is so generalised (eat/sleep/mate/die) that any nuance of actual behaviour is lost and what results is little more than a general abstraction to suite the purpose of game mechanics; which is utterly perfect for the task at hand, because honestly, developing something more sophisticated would be a huge undertaking with no practical benefit to the game project.

    I’m honestly perplexed as to why some gamers seem to think building “AI” is appropriate for a game.  The term AI is exceptionally misleading and would likely never be appropriate unless you were working on something that wasn’t as strictly defined as your average “regular” game environment.

    Imagine playing a game where a key NPC suddenly decides that they just weren’t into the plot anymore and just stopped playing, a “real AI” of this nature just isn’t for purpose.  I’m not suggesting that what we perceive as “AI” in games doesn’t have room for improvement, far from it, I am however suggesting that games will never lead to developing a “true” AI, simply because it’s not an appropriate avenue to approach game design from.

    People need to realise that good path finding is not the same thing as intelligence, and that they probably wouldn’t actually enjoy a game where the AI attempted to simulate sentience.  It’s work enough dealing with other players in a multiplayer game, let alone having to contend with a simulated intelligence in a single player oriented experience.

    C# Extension Methods To Get Enum DescriptionAttributes And Other Custom Attributes

    April 23rd, 2009

    A quick code snippet.  These two extension methods (C# 3.0+) enables you to return custom attributes from types, both on a specific type and any data type.  They both return null if the attribute is not present.  

    The first extension is useful for extending a type of your choice.  It’s especially useful when considering Enums and the DescriptionAttribute, or other enumeration attributes (especially custom attributes).  The usage examples below should make the use more obvious.

    The second extension allows you to return an Attribute from ANY type.  Use with caution, but I find it interesting that it’s actually possible to write an extension method that works that way.

        public static class ElementExtensions
        {    

            public static T GetAttributeValue<T>(this SomeConcreteType val)
            {
                var attributes = val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(T), false);

                T response = default(T);
                if(attributes.Length > 0)
                {
                    response = (T)attributes[0];
                }

                return response;
            }

            public static TReturnType GetAttributeValueFromType<TInstanceType, TReturnType>(this TInstanceType val)
            {
                var attributes = val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(TReturnType), false);

                TReturnType response = default(TReturnType);
                if(attributes.Length > 0)
                {
                    response = (TReturnType)attributes[0];
                }

                return response;
            }       
        }   

    Some usage examples….

        public enum RegexEnum
        {
            [RandomAttribute("^(([a-zA-Z]{1}[0-9]{3,4})|([a-zA-Z]{2}[0-9]{2,3})|[0-9]{4})$")]
            SomeEnumValue,
        }   

        public class RandomAttribute: Attribute
        {
            public string Expression { get; set; }

            public RandomAttribute(string expression)
            {
                Expression = expression;
            }
        }

        private static void Example()
        {
            // Both of these are equivalent
            foreach(var enumMember in Enum.GetValues(typeof(RegexEnum)))
            {
                var value = enumMember.GetAttributeValue();   
                var valueFromAnyType = enumMember.GetAttributeValueFromType<RegexEnum, RandomAttribute>();
            }       
        }

    Some nice syntactic sugar for use with enumerations.

    Installing certificates using WiX / Voltive (A Code Sample)

    April 22nd, 2009

    I’ve previously provided a code snippet illustrating how to use WiX (the “new” Windows Installer framework) to install certificates onto the target machine.

    I’ve had some good feedback about the post, but also had a request for a working example (in order to illustrate how the code fragment is used in an Installer.wxs file).

    I’ve cooked up a sample visual studio solution that installs a test certificate (generated using the MSDN makecert certificate example “makecert -sk XYZ -n “CN=XYZ Company” testXYZ.cer “).

    You’ll probably need to re-path references to the WixIISExtension and WixUIExtension in relation to your development environment but hopefully this should help you.

    The previous post is the third most accessed post in my blog (which probably speaks as much for the number of people that read this as it does it’s utility) so this should be useful to someone.

    It’s worth noting that this example installs the junk certificate as a Root CA so you might want to change that or ensure you remove it once you’re done.

    Download the Sample Solution

    Using A Wiki To Replace Requirements And Produce Documentation For Your Agile Projects

    April 7th, 2009

    I Know It Sucks, But You Need Requirements To Work

    There are two parts of software development that really suck. Traditional requirements analysis and documentation. This isn’t some silly joke, both practices are terribly antiquated and essentially useless, not least in an agile (read: rapidly changing) project.

    However, one of the common misconceptions I often hear when working on agile projects is stakeholders not quite “getting it” and thinking that agile is the same thing as requirement-less development.

    However you paint it, you need something that resembles requirements to write code with. Requirements needn’t be exhaustive, and in an agile environment are rarely ever final, but you need to know what you’re working on- the average user story isn’t going to cut it.  User stories are designed to start conversation between the developer and the stakeholder but you need to do something with the detail once you have it.

    Make The Requirements As Accountable As You Are

    You know what else sucks? The fact that most businesses don’t have any kind of document management strategy. Sure, you do, you’re a developer. You have source control (and if you don’t, go take care of that now), you’re accountable and auditable line by line, check-in by check-in. This same rigor rarely extends to other areas of businesses (though it should).

    When all is said and done, if you’re maintaining a traditional requirements document for your project you’re probably already making a big mistake. Who owns the document? How do you track changes? How do you know people haven’t changed the requirements without a reasonable discussion? What happens when somebody accidentally deletes it?

    Requirements documents are the “Excel Applications” of development. They’re unreliable, often so large that nobody will ever read them, and as such fundamentally useless. It’s a bit of a trick question really, monolithic requirements documents are about as “Write-Once-Read-Never” as most Perl code.

    I Think We Have a Problem…

    So lets recap. Requirements documents are evil and nobody reads them, but you need requirements to develop. Requirements documents aren’t auditable yet it’d really be a good idea if they were. Oh and documentation sucks.

    You Need Wikimentation!

    Wikimentation is a particularly amusing portmanteau but what you need is a wiki and if you do it right, you’ll never have to write documentation again.

    In an agile project, the requirements are meant to evolve and change as you go, so they deserve to be stored in a format that naturally leans towards allowing the users to do just that.

    There’s also an immediate bonus to either you or your business analysts in the way requirements are captured as a traditional requirements capturing exercise is eschewed in favour of documenting requirements as they become relevant. This is a very natural way of working and allows you to prototype or stub out components until you need to delve into detail (in fact, doing this is very much the SCRUM way).

    When I approach a new project I like to keep a few things in mind, I’ll call them the Holy Trinity of software maintainability…

    The Holy Trinity Of Software Maintainability

    • Readable Code
    • Good Developer Documentation
    • Source Control

    So how will a wiki help you get there?
    I’d argue that after the implementation of a set of requirements, the requirement and it’s details have effectively been superseded by the precedent set in the code.

    Those requirements you coded to, after implementation are useless. If the code doesn’t adhere to the requirement the code is buggy, and your bug tracking system should contain information explaining why.

    If the code is correct, the requirements would be classified as obsolete. Keeping them around only serves the purpose of misleading anyone who attempts to read the requirements to establish the coded behaviour of an application.

    So what we do is we maintain the list of requirements in a wiki in a slightly less formal manner. This is hypertext remember, big long lists of stuff are so 1990. Use links and divide the content into requirements for logical components, leaving a well maintained index on the project homepage in the wiki.

    Now as features are completed instead of the requirements eventually rotting away into irrelevance, they can be replaced by a simple developer commentary of the implementation. As you follow this pattern, your requirements will develop into a solid set of searchable developer documentation at the same rate as your project progresses.

    Constructing Your Own Evolving Requirements Wiki

    There are plenty of FOSS and OSS Wiki platforms out there. I have a personal preference for ScrewTurn Wiki on Windows and you can be up and running in literally under 5 minutes (web server not even required).

    A few simple guidelines

    • Ensure that the home page of your wiki clearly links to the discrete sub projects or components you’re developing
    • It’s encouraged to create wiki links for content that doesn’t exist but needs elaborating on by yourself or your team, to serve as a visual reminder of what is left to do
    • Create a standard template for a project and component wiki page
    • Populate each of your component wiki pages with the top level requirements for that component, along with “pre-links” to any sub-components of specific note or percieved complexity
    • Encourage your developers to document as they write
    • Don’t duplicate code comments or huge chunks of code in the wiki - that’s what clean and readable code is for
    • Do encourage the posting of examples for reusable or common components - share individual developer knowledge

    The wonderful thing about using a wiki is that it breeds accountability. Modifications are logged and audited; you can always see the history of any given page, from requirements to documentation.

    A wiki will provide you with a sound and adaptable framework for producing project documentation that evolves with your project. Used in conjunction with a good bug tracking system, a good version control system and some good developers, you’ll have a good chance at edging closer to that holy grail of maintainable well documented code at low cost.

    Zero Friction Documentation

    Your developers won’t resent a little bit of writing as they finish off logical units of work, and it could be even encourage to keep a “scratchpad” of information on the wiki as opposed to paper-notebooks. It’s also good to remember that you can always generate a traditional requirements document from the content stored in the wiki if an occasion calls for it.

    I’ve had a lot of success implementing these ideas in past projects (both agile and none agile) and I’d certainly recommend it.

    Why We Need Male Dominated Space Marine Action Games

    April 7th, 2009

    This is a round about response to both Leigh Alexander’s response to Heather Chaplin’s GDC rant session.  It’s worth noting that I wasn’t at GDC and that all my information is second hand, and as such, susceptible to Chinese whispers-style misinformation.  To quote Leigh’s post (as a good source of a direct quote)

    ‘She argued that medium’s age is not the correct source of blame for the often insultingly juvenile nature of games, the tiresome prevalence of space marines, bikini girls and typified young male power fantasies. Her point: Games aren’t adolescent. Game developers are a bunch of, in her words, "fucking adolescents."’

    This is really not a response to Leigh’s post at all, but more to the original subject that it made me aware of. 

    I always feel a bit defensive when people imply that the only video games that ever get produced are space marine staring male power-fantasies. 

    Just as a case point, lets take a look at the top selling games of last year (via a quick Google search)

    Wii Play
    Mario Kart Wii
    Wii Fit
    Super Smash Bros Brawl
    Grand Theft Auto IV
    Call of Duty: World at War
    Gears Of War 2
    New Super Mario Bros
    Madden NFL 09

    It’s difficult to claim that all of those titles fit into that narrow criteria.

    There’s often talk of cinema reaching a comparative maturity with the release of Citizen Kane, and The Beatles representing the maturity of pop music by a similar timeframe in their respective mediums development.

    I really don’t think that the respective age of the medium is a safe measure to judge it’s maturity.  By the time of Citizen Kane and the Beatles arrived in their respective mediums the supporting technology had plateaued to a degree that allowed for artists to have unencumbered technical freedom in comparison to the respective "technical freedom" that game designers have now.

    There are a lot of critics that underplay the difficulty of perfectly nailing both concept and execution of a game.  It’s difficult, stuff, even more difficult to make the game work well, be playable and be great.  If you manage artistic integrity and ingenuity on top of that you’ve managed something exceptional. 

    It’s no surprise that some of the most celebrated titles in video gaming are often technically flawed "classics" that get lauded for being "near misses".  Taking risks on titles is difficult and expensive on top of the desire to be artistically vibrant.

    I certainly don’t think Citizen Kane would have had the budget and resource to be "great" if the camera men were still learning how to use cameras and I really think that we’ve got a way to go before we can expect every game made to be a high concept piece. 

    Even after the technology reaches this point (as it has in the film industry) there’ll still be more summer action films / games made than artistic victories.

    It’s also worth nothing that in this constant strive for something else, it’s easy to loose the purity and fun of more "pure" game play experiences, including male power fantasies and computerised D&D implementations.  Some people find game mechanics fun, to some people that is the point and it always feels that some critics always call so loudly for something different that they miss some of the point of what we already have.

    The artistic indie film vision of influential games is a laudable one, but without the Gear of War’s there just isn’t the ecosystem to support them.  Much like when I watch films, I don’t always want to watch Capote or Chinatown.  Sometimes I want to watch Die Hard, and sometimes I want to watch Star Wars.  And sometimes I’d quite like to watch Twelve Monkeys or Being John Malkovich, but without Star Wars, the framework to support those often loss-leading productions just wouldn’t exist.

    I don’t pretend to play at “legitimate game criticism” however I certainly follow the area with more than a casual interest and I’m often very surprised at how the desire for strong artistic games in the “niche of a niche” press seems to forgo the need for any other kind of game. 

    The last thing I’d want is for the games press to take a Pitchfork media style pretentious view of the world, reducing anything that isn’t trendy or progressive to dirt.  There’s tonnes of very good games writing out there that I very much enjoy reading and I really hope that people allow at least some games to still be “just games” alongside the more “worthwhile” and “deep” experiences available.

    Controlling Your Software Development Environment And Release Cycle In An Agile Way

    March 22nd, 2009

    Fear Of Deployment

    When you start working on a new project there are four really important pieces of information you should be aware of regarding the deployment of your software

    1. Are you unafraid of deploying your code to production?
    2. Do you know how your code will react in your production environment with production configuration?
    3. Does your code react in the same way in your production environment as it does in development?
    4. Are you confident in the repeatability of your installation procedure?

    Fear and uncertainty in the deployment of software projects is a common theme I’ve seen across all the companies where I’ve worked and it’s never good for your project.  The fear of deployment is always a good indicator of the confidence your staff have in the project they are working on.

    A project with unknown qualities and suspicious (or known but unreported) bugs often causes an serious fear of deployment.  There’s nothing like the fear of deploying an application and reducing a business critical system to it’s knees.

    I’ve worked on a handful of projects where I’ve taken it upon myself to reign in this deployment fear and chaos, and more often than not, the fear has it’s roots in an uncontrolled development environment.

    Configuring A Development Environment

    I have very few suggestions regarding which tools you should use to develop your software.  I think that you gain the maximum benefit by allowing developers to pick and choose the absolute best tools for the job, be that technically or by preference.  Allowing developers to use their tools of choice makes happy developers.

    By contrast however, the one thing I’m really certain about is that every development environment should start with an absolute base configuration that should not be changed.  This is the first way to solve the eternal “works on my machine” developer problem.  In essence, if all the development environments start off the same, there’s no excuse for a “works on my machine” error.  Not only will this reduce friction between team members, but it’ll speed up the development process because you’re all working to one known configuration.

    Furthermore, the development environments alone shouldn’t share this configuration, they should share the base configuration with your production servers.  A huge percentage of software release issues are relating to the differences between production and test and development and the only way to solve what amounts to human error is to make this discrepancy go away.

    In the current IT landscape where virtualisation is painfully simple (and often free) there are no excuses not to have a development environment (or at the very least development staging virtual machine) that accurately and completely mirrors your production server.  If you’re struggling with licenses of third party software, produce mocks / proxies / approximations.  I can assure you that the time you spend building these glorified testing rigs will work healthily towards eradicating development-to-production errors.

    If It’s Worth Doing Once, It’s Worth Doing Three Times

    Another nasty pattern I see frequently is the development-to-production pattern.  If you’re compiling software on your development machines and releasing it to production, “you’re doing it wrong”.  What’s worse than a “works-on-my-machine” conversation with a colleague?  You got it, a “works-on-my-machine” conversation with a system administrator.  It’s not an excuse.  Stop it.

    You need to start dog-fooding your deployments, and this means environment cloning and real test servers.  I always follow a simple pattern; developer machine -> developer test box -> system-test box -> user acceptance test box -> production.

    In order to do this you’ll need a series of environments (physical or virtual)

    • Developer machine
    • Developer environment
    • Test environment
    • UAT environment
    • Production environment

    That might sound like a lot of hardware but it really isn’t.  Use virtualisation and use it well.  Clone your virtual environments if need be.  Use snapshots and rollbacks to test deployments.  It shouldn’t take any time to administer (and I’ll explain why in the next section) and by the time your code is running on production you’ll have moved it through three different environments.  This is absolutely key to having faith in your deployment procedures.  When you’ve deployed your code so many times that you’re sure it works, in an environment that’s a mirror image of production, production is a far less scary place to be deploying to.

    The key to this technique is that the environments all have very clearly defined purposes.  The developer produces his code changes to a system on his development box.  The code seems to work so it’s deployed to the developer environment and checked into your source control repository.  The development environment should be treated as a place where things could potentially get messed up, but it’s also a good place to host joint resources (a database, some middleware) that all the developers make use of.  If the code works in the developer environment it can be promoted to the test environment for the system tester (or a developer wearing a system testers hat) to test.  Once passed on the test box, the code changes can be promoted to the UAT environment for users to preview coming code releases.  Once these changes have been accepted, the code is released into production.

    Beware of release insanity.  It makes perfect sense to frequently deploy to the developer environment but only occasionally promote code up to test and UAT.  This should fit around your way of working, just be sure to keep the conceptual integrity of the environments.  If you find a bug in test, don’t fix it in the test environment, fix it in development. It’s up to you to ensure you know what code is running across your environments to avoid any nasty surprises.

    One Install To Rule Them All

    At this point you’re probably thinking that this all sounds like a huge amount of effort but it isn’t.  The reason it isn’t is that you should, for the most part, be able to deploy your entire system in about 5 clicks / commands.  Don’t let your deployment mechanism be the week link in your application.  There are tonnes of install technologies on the market, both free and costly, to match whichever platform and tool chain you’re working in.

    I wish I could remember where I read it, but one of the pieces of setup advice I read long ago that always stuck with me is that you should aim for a one-click install.  You might not achieve it, but you should aim to get very very close.

    You need to invest time in configuration management for your application.  Build tools to auto-generate configurations for your deployment environments, build tools to automate setup and deployment.  These tools are worthwhile, because from the moment they’re built, you never need to worry about deploying your software.  They remove risk, and anything that removes risk from software development should be embraced.

    Once you’ve build these tools and installers you’ll find your able to do things that you only thought were possible in large companies with seemingly limitless resources.  You’ll be able to configure nightly builds using a continuous integration package of your choice and have the nightly builds install themselves in your developer environment,  You’ll be confident that at any point you can provide a working copy of your software on request.

    No Fear Of The Unexpected

    The wonderful benefit you get at the end of this process is confidence in your software.

    So you’ve released your system to production, do you then feel the need to slavishly test the system in the live environment? 

    Imagine that urge going away, knowing that by the time the code hits production that any environmental glitches that could manifest have had the ability to fail three times already.  Any amount of cursory testing you could perform at this point should pale in comparison to the testing you did in the test environment and the user testing environment.  If your “at a glance” testing somehow discovers a bug, you’ve done something very very wrong in the lead up to release.  You should be confident in the fact that your code has been run exhaustively and often by the time it’s reached production.

    The world isn’t perfect however, and it’s obviously advisable to a quick audit of the software once installed, just don’t force yourself to repeat your system tests in production (an obvious anti-pattern) and you’ll gain no benefits from doing so.

    If you can devise an automated tool to verify your deployments after install, you’ll be able to round off your sense of deployment-security.

    A Zen-like State Of Satisfaction

    I opened this piece with a few simple questions.

    • Are you unafraid of deploying your code to production?
    • Do you know how your code will react in your production environment with production configuration?
    • Does your code react in the same way in your production environment as it does in development?
    • Are you confident in the repeatability of your installation procedure?

    I can answer yes to all three questions because I understand my environments and I hope you can too.