How important is coding style? This debate is something that really draws out the "artist" and critic in every programmer. In many ways the style of writing code is similar to writing poetry, and what makes the subject so hotly debated is that the beauty of the code, sentence or poem is all in the eye of the beholder. Just like those critics of of prose and poetry, the serious critiques of coding style come from understanding the limits and history of the form. Practice reading and writing lots of code, in different languages if possible, and acknowledging how many ways there are to write the same functionality.
In the same way that experimentation in poetry and prose makes for a better understanding of the overall form, understanding how different coding standards can make for better (or worse) code. With every coding standard the code will be limited but also potentially clarified in a new way.
Organization of code one way can make code reviews easier, but potentially making maintanability more difficult, code may be separated into different sections that are ordered in a certain way that causes significant scrolling, opening of multiple files and jumping through layers of code, that structured a different way may minimize bouncing around.
Coding standards for teams is especially important, and should be strictly adhered to as best as possible, for the overall benefit of the team. Consistency is very important as being able to quickly jump into a new class, module or section of the code "should" be as easy as jumping into a section that you wrote yourself. Coding standards are an agreement between programmers to create a cohesive work, much like any collaborative effort, the final product should be a uniform work of art.
The most diplomatic way to handle this is often through adopting a tool that lets catches errors quickly and consistently. This also frees up code reviews to be on more important aspects like architecture, re-usability, error checking, design patterns etc. as non-adherence to coding standards often ends up becoming the main focus if the code strays too far from the standards outlined. These tools should also be part of the commit process, so that the code is rejected if it does not pass the coding standards (in certain cases some of these tools may have to have slight adjustments, as desired, but with a focus on consistency).
For those C# Unity or XNA projects some guys really like Resharper (30 day trial at http://www.jetbrains.com/resharper/) For a free style specific checker I like StyleCop: http://stylecop.codeplex.com/ there is a good amount of customization and easily embeds into Visual Studio. The information that is provided from the code checks make it easy to know the issue and change the code to follow the coding standards that have been setup.
For python there is pep8 http://pypi.python.org/pypi/pep8 and pychecker http://pychecker.sourceforge.net/ as well as a few more.
In general the style and syntax checkers are becoming quite popular (especially with scripting languages that don't compile) but regardless a good "style cop" will improve code quality, readability of all sections of the code, and allows everyone to focus on writing great code, instead of what it looks like in the end.
Until next time, StyleCop: that is all.
Michael Hubbard
http://michaelhubbard.ca
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
Monday, October 18, 2010
Tuesday, April 27, 2010
Book Review: Professional Refactoring in C#
I would recommend the book Professional Refactoring in C# & ASP.Net by Danijel Arsenovski for any developer interested in refactoring code. Arsenovski gives clear and documented refactoring improvements for a variety of common code structures, as well as some specific C# version 3.0 features. The book deals strongly with the concept of code "smells" which are the simple heuristics for finding and improving parts of code.
The code smells include finding and refactoring such things as dead code, overexposure, overburdened temporary variable, long methods, procedural design and many others. The book explores setting up a unit testing framework with NUnit to verify the same functionality after refactoring has taken place. This draws some parallels with the Foundations of Programming book I reviewed earlier on its approach to unit testing. While I do enjoy the appeal of a unit testing design and programming strategy it is a bit more difficult to apply to all aspects of game programming (such as player/object synchronization etc), as well as sometimes it can be an issue to test if you are dealing with a tool set that does not take the code directly (like the Unreal scripting).
This book would be useful for any intermediate to expert developers with an interest in C# as it will surely give you food for thought on how to improve your code (which I will see if I can apply to the XNA project (which I will be mentioning later).
Bye for now,
http://michaelhubbard.ca
The code smells include finding and refactoring such things as dead code, overexposure, overburdened temporary variable, long methods, procedural design and many others. The book explores setting up a unit testing framework with NUnit to verify the same functionality after refactoring has taken place. This draws some parallels with the Foundations of Programming book I reviewed earlier on its approach to unit testing. While I do enjoy the appeal of a unit testing design and programming strategy it is a bit more difficult to apply to all aspects of game programming (such as player/object synchronization etc), as well as sometimes it can be an issue to test if you are dealing with a tool set that does not take the code directly (like the Unreal scripting).
This book would be useful for any intermediate to expert developers with an interest in C# as it will surely give you food for thought on how to improve your code (which I will see if I can apply to the XNA project (which I will be mentioning later).
Bye for now,
http://michaelhubbard.ca
Sunday, April 11, 2010
XNA C# Item Class Template
I thought I would share a simple class template for XNA classes that are not Game Components (for classes that do not require an update etc.). If you copy the code below to a new (empty class called Class1.cs) and then go to File->Export Template (choose Item Template in the wizard) and the class you copied the template info to. After restarting Visual Studio you can now use the template ( Add->New Item-> My Templates).
The above "code" is free and can be modified, updated and used without consent.
Bye for now,
Michael Hubbard
http://michaelhubbard.ca
//-----------------------------------------------------------------------
//Copyright (c) "$safeitemname$". All rights reserved.
//$guid1$
//$username$ $time$
//
// $safeitemname$ :
//
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace XNAGame
{
class Class1
{
}
}
The above "code" is free and can be modified, updated and used without consent.
Bye for now,
Michael Hubbard
http://michaelhubbard.ca
Subscribe to:
Posts (Atom)