Greg Beech's Website

Blogs

  • Function currying in C#

    C# does not automatically curry functions, so a function that multiplies two double s together and returns the result must be assigned to a delegate that takes two arguments and returns a result: Func < double , double , double > multiply = (a, b) => a * b; This seems reasonable, but unfortunately it means that if we want to define a partial application of the function where one argument is...
    Posted to Greg Beech's Tech Blog by Greg Beech on 09-16-2008
  • Images wrong or missing in blog posts

    Recently I've been using more images to illustrate blog posts, and unfortunately have run into an issue with Windows Live Writer and Community Server 2008 . The full details are in that forum, including a workaround and a fix, but essentially if the image is pasted from the clipboard without saving it to disk first Writer generates file names that overlap, and Community Server uses the same folder...
    Posted to Greg Beech's Tech Blog by Greg Beech on 09-09-2008
    Filed under:
  • Determining the bias of a shuffle algorithm

    Last time I wrote Shuffle and TakeRandom extension methods for IEnumerable<T> which use the Fisher-Yates algorithm to perform an unbiased shuffle of a sequence and take a random subset from it. In order to optimise the performance of the TakeRandom method I had to reverse the documented implementation of the shuffle so it produced the results at the start of the sequence rather than the end,...
    Posted to Greg Beech's Tech Blog by Greg Beech on 09-09-2008
  • Shuffle and TakeRandom extension methods for IEnumerable<T>

    It's a fairly common practice on web sites to display sets of randomly ordered items, whether it's a selection of your friends, items related to the one you're looking at, or even something as simple as a tag cloud. Sometimes the entire set of items is randomised, and sometimes you want to take a random selection from the set and display that (the latter is typically done so you can cache...
    Posted to Greg Beech's Tech Blog by Greg Beech on 09-03-2008
    Filed under: , ,
  • Optimised IP address to country code mapping

    One of the things that web sites tend to do frequently is look up the country a user is from based on their IP address, for example to direct them to the appropriate site for their country or to restrict content that cannot be legally shown to them. There are numerous solutions to perform this mapping such as calling third-party web services or looking up the country from a database, but as it is such...
    Posted to Greg Beech's Tech Blog by Greg Beech on 09-01-2008
    Filed under: ,
  • Disposing and finalizing partially constructed objects

    On MSDN forums there was a question about whether class constructors should be permitted to do a lot of work or not : I have a dilemma and a dispute with some colleagues: should the constructor of a C# class do a lot or the minimum and latter in a method call put a lot of logic? I am coming from a C++ background and there it is better to do just simple data initialization in the constructor, throwing...
    Posted to Greg Beech's Tech Blog by Greg Beech on 08-27-2008
  • Computers don't care whether your resources are directly addressable

    The article Roots of the REST/SOAP Debate (along with many others like it) claims that SOAP web services have a limitation in that resources they work with are not directly addressable via a URI, but that you have to connect to the SOAP endpoint and then call a method on it. The analogy used is as follows: Let me offer an analogy. Suppose you were living temporarily in a hotel. The hotel might not...
    Posted to Greg Beech's Tech Blog by Greg Beech on 08-24-2008
  • Emitting primitive type conversions with Reflection.Emit

    Conversion between primitive types is trivial in high-level languages like C# as you can use the cast operator, optionally surrounded with checked or unchecked to force the integer overflow semantics. At CIL level the conversion is less trivial, involving the choice of no fewer than 27 different op-codes, which are required to be used in combination with each other under some circumstances. This is...
    Posted to Greg Beech's Tech Blog by Greg Beech on 08-20-2008
    Filed under: , ,
  • Code without comments is code that doesn't work

    The other week Jeff Atwood posted a blog entry named Coding Without Comments that stated... well I'm actually not sure. It started by making the valid point that comments should indicate why your code works the way it does and shouldn't be needed to explain what it does, but then went entirely off track and ended up pretty much stating that all comments are detrimental to code quality (emphasis...
    Posted to Greg Beech's Tech Blog by Greg Beech on 08-04-2008
    Filed under:
  • Tips & Tricks: Use DebuggerDisplayAttribute for easier debugging

    When debugging, it's common to hover over an object to get a quick view of what its properties are. With a single object this is fine because you can easily expand it to view its contents, but when you have a collection of objects it's painful to see the contents as you have to expand every element. The typical view in the debugger of a collection of simple movie classes is like this: public...
    Posted to Greg Beech's Tech Blog by Greg Beech on 07-28-2008
    Filed under: ,
  • Dynamic sorting and paging in SQL Server with the CASE expression

    The normal use for the CASE expression in T-SQL is to perform simple modifications to one of the columns in a SELECT statement; the type of example given in MSDN show it being used to expand state abbreviations such as 'CA' to 'California', or to categorise products into 'Cheap' and 'Expensive' depending on their price. However, the CASE expression is a lot more flexible...
    Posted to Greg Beech's Tech Blog by Greg Beech on 07-21-2008
    Filed under: ,
  • Emitting arbitrary constants with Reflection.Emit

    I've been writing a fair bit of Reflection.Emit code recently, and along the way have developed quite a number of extension methods for the ILGenerator class to handle the complex semantics of some seemingly trivial operations , and choose the most efficient op-codes (e.g. when can the short forms be used). I had already written EmitLdc methods to handle constant values for every type that can...
    Posted to Greg Beech's Tech Blog by Greg Beech on 07-20-2008
    Filed under: ,
  • Service-orientation is inevitable for performant database applications

    Databases cause two performance problems for applications. Firstly they are difficult to scale out compared to other server roles which means they can easily become the scalability bottleneck, and secondly they are on a remote machine which means that each query made to them involves network latency. To reduce processing overhead and latency, a common approach is to make as few queries as possible...
    Posted to Greg Beech's Tech Blog by Greg Beech on 07-16-2008
  • Object-Relational Mapping frameworks won't improve your timescales

    On every database-backed project I've worked on, which is quite a few ranging from 4 to over 50 developers, the number of people working on the database is relatively small. Typically in a three-tier application the ratio of developers on UI:services:data is around 5:2:1 because high quality user interface development requires a lot of work, and it is far more time consuming to display data in...
    Posted to Greg Beech's Tech Blog by Greg Beech on 07-10-2008
  • Emitting decimal pseudo-constants with Reflection.Emit

    Some languages such as C# let you treat the System.Decimal type as an integral type most of the time, even having the keyword decimal , and will let you declare constant fields of the type under some circumstances. However, decimal isn't a primitive type in the same way as the other numerics, and is implemented quite differently from them. At CIL level, you can't actually declare a constant...
    Posted to Greg Beech's Tech Blog by Greg Beech on 07-07-2008
    Filed under: , ,
1 2 3 4 5 Next >
Copyright (C) Greg Beech. All rights reserved.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems