More Than Nothing

“You mean you can't take less; it's very easy to take more than nothing.”

Archive for the ‘C#’ Category

Relation Tables

Posted on March 6, 2008 by [ICR] under C#, Mathematics, Programming

I quite often find myself needing to store relationships between objects and as such have developed a small library for defining relation tables. What sort of things would you need to store relationships about? The similarity between music tracks. The number of words spoken in an IM conversation between people. The position of a playoff [...]

No Comments | Read the rest of this entry »

Falling through gracefully on switch statements

Posted on February 24, 2008 by [ICR] under C#, Programming

Quite often I end up doing something like this in my code: Type returnType; switch (function.ReturnType) { case ReturnType.Boolean : returnType = typeof(bool); break; case ReturnType.State: returnType = stateEnum; break; case ReturnType.Tape: returnType = typeof(Tape); break; } This covers every value in the ReturnType enum, yet (for a very good reason that I forgot) the [...]

No Comments | Read the rest of this entry »

Further conditional methods using C#

Posted on February 18, 2008 by [ICR] under C#, Programming

Previously I’ve posted about a technique to create conditional methods on generics, that is methods on a generic class that only appear if the generic type implements some interface. Perhaps not the best solution to the problem, but an interesting one. Last night I had a brainwave – perhaps you can create a much cleaner [...]

No Comments | Read the rest of this entry »

Automatic Properties

Posted on February 8, 2008 by [ICR] under C#, Programming

My favourite feature of Visual Studio 2008, I've decided, is automatic properties. I say they're a feature of VS2008 because you can use them in code that targets .NET 2.0 – after all the compiler just expands them out like a code snippet. I've been forced to work with VS2005 recently (for the XNA Studio) [...]

No Comments | Read the rest of this entry »

Exceptions Don't Have To Be Exceptional Just Because

Posted on January 25, 2008 by [ICR] under C#, Programming, Python, Rants

Error handling is a big problem in programming – how should it be done efficiently and clearly? There are several methods, return codes and exceptions being the most popular. Both have their pro’s and con’s and differing methods of use with valid arguments on all sides. However, all too often I see implementation details creeping [...]

No Comments | Read the rest of this entry »

A proposal for const functions in C#

Posted on January 15, 2008 by [ICR] under C#, Languages, Programming

As I move more and more towards a functional style of programming I often find myself using referentially transparent functions in amongst the rest of my C# code. A referentially transparent function is basically one that guarantees that it has no side-effects (modifies anything outside of the function) such that every time you call it [...]

1 Comment | Read the rest of this entry »

Using indexes whilst still using foreach in C# 3.5

Posted on January 2, 2008 by [ICR] under C#, Programming

Over the past few weeks I’ve been building a steadily growing collection of helper functions. I’ve gotten to the stage where I’ve had to write enough reduce functions, manually fill enough arrays and rewritten enough foreach as for loops to get sick of it and begin writing my own helper library. Below is what I [...]

No Comments | Read the rest of this entry »

C# hokus-pokus

Posted on December 22, 2007 by [ICR] under C#, Programming, Python

Amir regularly makes posts on his blog to show off the wonders of conciseness and clarity in Python. His latest offering is an interesting one. As with many of his examples, it shows off more a certain programming style promoted by Python over language features themselves. You can write the same thing in Java or [...]

No Comments | Read the rest of this entry »

Conditional methods using generics in C#

Posted on November 24, 2007 by [ICR] under C#, Programming

In one of the projects I am currently working in I have a generic class defined like so: public class MyClass<T> { //… } I also have a method that only really makes sense if T implements IComparable, but the class also makes sense if it doesn’t. The usual way to solve it would be: [...]

No Comments | Read the rest of this entry »

Swapping variables in C#

Posted on September 8, 2007 by [ICR] under C#, Programming

In the project I am currently working on I often need to swap two non-integer variables around. It’s not a difficult thing to do with a temp variable, but the frequency in which the current project uses it violates the DRY principle somewhat, so I decided to refactor it into a method. What makes it [...]

No Comments | Read the rest of this entry »