Wednesday, 12 February 2014

Other people's code - Suteki Shop

It occurred to me a while ago it would be a good idea to have a dig through some open source code. Why? Primarily because in the last few years I've spent the majority of my time working on two codebases, one at home and the other at work. I feel very much in my own little code bubble. Working with the same code day in day out you become familiar with the conventions and patterns chosen and you can get into a rut. 

So, the point of this exercise is to get me out of my comfort zone and learn something new. How have other people solved problems I've faced? What is their code like? How to they handle errors? What new ideas or ways of working can I apply to my code?


Suteki Shop

Suteki Shop, written by Mike Hadlow using NHibernate and .NET MVC, is an open source eCommerce site aimed at the fashion and retail industry. I figured it was a good place to start because looking through an unfamiliar codebase is difficult but.NET MVC is my day job so that will give me a head start.


Observations

  • The UnitOfWorkAttribute is a great way of encapsulating all database operations in a single action into a transaction and committing on success or rolling back on exception
  • Exception handling is minimal. It seems to only be added where it's really required resulting in really clean code
  • Really nice use of fluent interfaces to create more readable code when mapping data to view models 
  • Generic repositories make CRUD operations ridiculously simple
  • On the whole the controllers are short. Unnecessary code hasn't leaked into them which I find often happens in my work codebase
  • Some domain objects fire events whose handlers call methods on services classes, which is a mechanism I've never considered using. E.g. The Order object has no dependencies on any other class, but if you call Confirm() it will set OrderStatus to OrderStatus.Created and fire an OrderConfirmed event whose handler calls EmailService.SendOrderConfirmation. It's a nice way of running non critical tasks
  • Object values can be retrieved from the database as if by magic using the power of the EntityModelBinder

ScaffoldController<T>

This is really interesting. It's CRUD done right. Several controllers inherit from this and they contain a couple of attributes and no other code

Repository operations are possible because the generic class that applies to the controller also applies to the repository. That generic class is also used to build the view which can store a single item, list of items or return data for a drop down list.

Obviously there are down sides to this approach. It can't be used if you want to do any non standard operations and you still need to create the view.

What can I take from this?

  • Chaining methods with fluent interfaces is great for creating readable code, I don't use it enough
  • There's really no reason to put any kind of logic in the controller, it can always go elsewhere
  • I don't understand generics quite as well as I'd like 
  • Model binding is something I should read up on
  • Not all actions in a web application need to be done synchronously. If something isn't critical it can be carried out using events or some other async process

Saturday, 28 December 2013

Learning 2D in Unity3D


In the past few months I’ve been teaching myself how to use the Unity3D game engine, partly because I’ve always wanted to write a game and partly because I wanted to learn something very different from web development. I thought it would be useful to share the resource helped me. My focus has been on 2D games and iOS devices so the links reflect that.


Tutorials and tips

  • Unity best practices - An assortment of useful tips
  • Unity3D Student - A large number of free, short tutorial videos. Typically 5-10 minutes which is great for picking up things quickly
  • 2D in Unity - A short overview of working with 2D in Unity 
  • Created 2D games with Unity (part2part 3part 4part 5video) - A much more in depth tutorial with covers making a simple 2D game
  • Setting up version control - Essential if you’re doing a project of any size. It’s also worth setting your Asset Serialization mode to Force Text in Edit -> Project Settings -> Editor as binary files cannot be merged
  • Coding standards in Unity - I don't agree with all of these but it's a good overview
  • Unity3D wiki - It's worth digging around on this site. There's a wealth of information and useful links. In particular the scripts is packed full of useful code 

Useful plugins

  • iTween (free) - The de-facto tweening framework. It’s very simple to use and the documentation is great
  • 2D Toolkit (paid) - This plugin provides a lot of features to make creating 2D games in Unity much more straight forward. With the release of version 4.3 Unity comes native 2D tools. There is considerable crossover in functionality but, as you would expect, some of the native tools are Pro only so I’m sticking with 2D toolkit for the time being
  • NGUI (paid) - A popular framework for creating GUIs. In my experience it’s easy to create basic GUIs and has some great features, but it's not cheap

Testing

  • TestFlight - An incredibly useful way of distributing your app to multiple devices for testing. That’s all I’ve used it for and I’ve barely scratched the surface of what it can do

iOS specific


General


  • Unity3D blog - It's worth keeping an eye as to find out about details of future work, new features and releases



Help