Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Sunday, 8 December 2013

Nothing is not null

I have to switch between writing legacy vb.net and c# on a daily basis and generally don't find this to be much of a problem, but a came across an interesting difference when trying to find the equivalent of c#'s default keyword. The default keyword returns the default value for a type T: default(T), zero for numeric types, null for nullables etc. It turns out that Nothing can be used in vb.net in this way. I always assumed null and Nothing where equivalent between vb.net and c#; I was wrong.

null == 0 // False
Nothing = 0 ' True

false == null // False
False = Nothing ' True
Taken from the official documentation: Nothing represents the default value of any data type. For reference types, the default value is the null reference. For value types, the default value depends on whether the value type is nullable.

There doesn't appear to be an equivalent of pure null in vb.net, you would have to check if it was a reference or value type explicitly. I don't like the implicitness of Nothing, but it fits with vb.net's general implicit conversions (which I also don't like).

False = 0 ' True
"1" = 1 ' True
' etc. etc.

Monday, 24 June 2013

Anonymous function in anger with Memoize

About a year ago I blogged about anonymous functions, lambdas and delegates. I've had cause to use these principals in anger to create a memoize function; not exactly original but interesting never the less. I made this memoize function, which takes an anonynous function that takes 2 parameters.

static Func<T1, T2, TResult> Memoize<T1, T2, TResult>(Func<T1, T2, TResult> f)
{
    var dict = new Dictionary<Tuple<T1, T2>, TResult>();
    return (a, b) =>
    {
        TResult result;
        if (!dict.TryGetValue(Tuple.Create(a, b), out result))
        {
            result = f(a, b);
            dict.Add(Tuple.Create(a, b), result);
        }
        return result;
    };
}

N.B. It uses the .net4 Tuple class to store the 2 parameters, for the sake a simplicity I haven't generalized this for 1 or > 2 parameters, or made it an extension method. You then need to wrap the function you want memoizing.

var myFunc1 = Memoize((Func<int, int, MyClass1>)Some.Dot.Namespace.Func1);
var myNewClass = new MySomethingClass();
var myFunc2 = Memoize((Func<string, string, MyClass2>)myNewClass.Func2);

while(true)
{
    var result = myFunc1(1,2);
    var result2 = myFunc2("1","2");
}

The results of the memoized functions are cached inside the function calls, leading to better performance if the function is a called with the same parameters many times in the loop.

Saturday, 27 April 2013

The curious case of null coalescing arithmetic

or how the simple things trip you up

I was writing a quick bit of code to add two nullable integers. To cope with the null, I used the wonderfully simple null coalescing operator ?? in C# to convert the integers to zero, like this:
int result = a ?? 0 + b ?? 0;
The result was wrong as demonstrated below:
int? a = 2;
int? b = 4;

int c = a ?? 0 + b ?? 0;
// Result:
// c = 2;
The obvious answer was a problem with operator precedence and a few brackets later fixed the problem:
int c = (a ?? 0) + (b ?? 0);
// Result:
// c = 6;
But why was it doing in the first instance? A little playing with linqpad later and it turns out the + operator has a much higher precedence than ??, so it was actually doing:
int c = a ?? ((0 + b) ?? 0);
This reads as: a, or if a is null then 0 + b, if 0 + b is null then zero. The mistake I made was because I'm so used to using ?? as a simple substitution of the preceding variable I wasn't thinking of it as a 'real' operator with precedence and ordering.

Thursday, 5 July 2012

C# Anonymous, Lambdas, Delegates and Expressions...phew.

C# and by extension any .net language has complex support for both anonymous functions and lambdas. I found it quite useful to have a basic reference for these; so here it is.
// Anonymous function using the delegate keyword.
Func<string> anon = delegate() { return "x"; });

// Anonymous function from lambda.
Func<string> lambda = () => "x";

// Same but using explicit typing.
var anon2 = new Func<string>(delegate() { return "x"; });
The function references (anon, lambda, anon2) are known in .net as delegates which is nomenclature for function pointer. Because C# is statically typed we have to explicitly type the delegate, hence 'Func'. Func is a generic delegate type: a predefined delegate, rather than having to specify it constantly. We could explicity define the delegate like this.
// Define delegate.
delegate int myDelegate(int i);

// Assign delegate.
myDelegate anon = delegate(int i) { return i };
myDelegate lambda = x => x + 1;

Linq Expressions is where things start to get more complex. Linq expressions allow lambda functions to be reflected and turned into data instead of code, allowing you to change the function. A typical change would be from a .net lambda function to an SQL statement.
// Expression
Expression<Func<string>> exp = () => "x";

// Can't be done.
// Expression<Func<string>> exp2 = delegate() { return "x"; };
However note that Linq Expressions cannot be created using anonymous functions.

OK if you followed all that....lambda can also be written with a statement body rather than an expression body as we have seen so far. Statement bodies allow more complex multiline functions.
// Statement lambda
Func<string> lambda = () => { return "x"; };
Statement and expression body lambdas are functionaly identical, but they are different. Statement body lambdas cannot be made into expression trees. In practice this means that certain linq providers, such as Linq2Sql cannot use lambda functions created with statement bodies.
A lambda expression with a statement body cannot be converted to an expression tree
One final quirk I'll mention is that once you have an explicit Expression it can be used on an IQueryable, but not a standard IEnumerable. You have to 'unwrap' it, converting it back to a standard Func.
// OK, compiler sorts it out
var l0 = new int[] { 1, 2, 3 }.Where(x => x < 2);
var l1 = new int[] { 1, 2, 3 }.AsQueryable<int>().Where(x => x < 2);

// Force exp to be an Expression
Expression<Func<int, bool>> exp = x => x < 2;

// Doesn't work - invalid arguments
//var l2 = new int[] { 1, 2, 3 }.Where(exp);

// Works on IQueryable
var l3 = new int[] { 1, 2, 3 }.AsQueryable<int>().Where(exp);

// Convert Expression back to standard Func to make it work.
var l4 = new int[] { 1, 2, 3 }.Where(exp.Compile());


Thursday, 15 March 2012

C# Optional Arguments Gotcha

I came across an interesting quirk of C#'s optional arguments when coding a MVC site against my Linq2Sql BLL. The BLL handles all my database logic including the creation of the Linq DataContext. In some edge cases I need to recycle an existing DataContext; so I made the DataContext an optional parameter. All good until I instantiated a new instance of the BLL in my shiny new MVC website - new BLL() - and encountered this error:

Error 11 The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced.

Apparently the calling code must know about the optional object type even when it's not specified. After some googling it looks like the optional parameters are baked directly into the IL at compile time. It wouldn't make sense to do this on the callee side, so this seems very odd behaviour.

Tuesday, 7 June 2011

ASP from a Python/Zope perspective.

I have recently changed jobs (hence the lack of posts recently), switching from 3 years of dedicated open source Python development, mainly in the Zopes and Plone, to Microsoft ASP.NET. I thought that a post is some of my observations on the transition would be interesting.

ASP.NET has a very different ethos to any of the Python frameworks I have used in the past. It is far more frontend orientated, with most of the functionality tied in some way to the HTML page. The typical way to construct a page in ASP is to embed (pre-built) ASP html controls into a page’s markup (using XML). Examples of the controls are textbox, label, image, table etc. There are python frameworks that do this, it’s not that unusual, but ASP takes it to a whole new level.

One of the most useful functions of a control is that it is capable of automatically maintaining its own state throughout multiple POSTS and reloads. Fill in a text box and on page reload you text will still be there without you having to code anything.

Control can also disseminate datasources, such as SQL, Arrays or even web services. A table control can be linked to a SQL datasource which will then render as a HTML table of data without writing a single line of actual code.

The power of these controls far surpasses those available in python frameworks, especially if you start buying commercial controls. A table control can by default render a HTML table capable of allowing end user to filter, group, sort and export the data. Nice ajax updates are also relatively simply. Link the table control to an ajax manager control and any functions that would previously have been a POST get transformed to ajax. As a nice example you can generate graphs from a datasource control using the charting controls in about 2 minutes.

So where is the down side, there has to be one right? Yes there is. The main problem with ASP.NET is actually where Python shines: in the elegant control of information from source to page. ASP may well be able to do very clever things quickly and simply on the front end, but controlling the data through a full lifecycle can be a pain as soon if you want the least bit of customisation.

How do you check for specific errors that occur when fetching data on one of those fancy automated datasources? - You have to subscribe to its events. What if I want to change how and when the data is fetched? – You have to hope the control exposes the functionality you want and documents it. In comparison Python frameworks almost universally provide a very consistent model for the organisation of data, from your SQLAlchemy model, to your queries to your View and data marshalling up to the template. It can be done nicely is ASP but it takes more time and design to get it correct.

Python’s organisational architecture is far superior to ASP’s and only getting better with clean and simple design patters (decorators), WSGI, Clean object models, Views, Routes etc. ASP’s out of the box front end elements are equally as superior to anything Python frameworks has to offer, the likes of Deform are a nice clean, Pythonic start but don’t get anywhere near to power of their ASP equivalents.

Tuesday, 10 August 2010

Error checking II

Last month I wrote a blog on why error checking and correction should be documented better and discussed more. In addition the "how" of error checking, it also seems like the "where" to error check can be ill documented and is effected by the tools and frameworks you maybe using.

When designing a function/method one decision that always needs to be made is whether to throw an exception or return a error/default value on an irrecoverable error. The best solution is normally to throw the exception. Returning a default value can make the API more complex, reduces the details about the original error and makes the code difficult to extend and debug. Catching the exception and returning a sensible default value is usually best left to the last point at which you can intercept it. This generally means directly before it hits the user interface.
def myFunction():
"""Allows an errors in doSomething to propagate.
Returns an array or throws an exception."""
return doSomething()

def myDefaultFunction():
"""Returns a default value.
Returns an array.
"""
result = []
try:
result = doSomething()
except StandardError:
pass
return result

A good example of why this can get messy is the MVC architecture pattern. MVC separates the data logic (Model), presentation logic (View) and the Controller which negotiate responses and submissions. The natural place for those pesky unresolved errors is in the View where they can be handled appropriately. The problem occurs because in a lot of frameworks the View is often synonymous with a template that doesn't have the necessary control to handle uncaught exceptions*. So we have to move the logic out, but the View has access to both the Model and the Controller (if it doesn't, it's not MVC) and we don't really want to have to catch exceptions and return default values in the Model. The Model controls the data and is important to the business logic; errors here need to be explicit, not default. So this leaves is with a bit of a mismatch of requirements that can further muddy the waters leading to clear and concise error checking.

* There is a lot of ambiguity in the definition of MVC, but the fact remains that a lot of frameworks explicitly allow the user access to both the View logic, be it template, presentation code, or both, and the Model (context).

Friday, 2 July 2010

Error checking should be a first class citizen.

I don't think that error checking gets the attention it deserves. It should be one of the very first things that you learn to do properly when your developing in a new language and yet documentation on doing it "properly" is thin on the ground. Good error checking is fundamental to good code, but what's considered good changes depending on what your doing and in what language.

Python, for example, has very good exception checking; but do you use exception check all the time or do explicit checks for known errors? Well after some digging around the Python glossary states:
EAFP: Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.
So exception checking is considered a common style in Python, and further checks turn up this blog on the performance on Python's exception checking. After my own checks is seems that the exception block adds only a very minimal amount of overhead to the execution speed. The overhead from a try block is likely to be less than anything other than the most cursory explicit checks and you can error check a loop with a single try, whereas explicit check would have to be called per iteration. On the down side, if an exception in thrown, it's expensive. A good rule of thumb would be to use exception checking when code is unlikely to fail and explicit checks where it is.

Javascript is another language that supports exception checking, however there have been many articles indicating that using it can (under certain circumstances) severely impact performance-critical areas of code and should be avoided. In addition unlike most languages Javascript does not raise an error when trying to access an invalid property (it returns 'undefined') so this type of error would not be caught by a try block anyway. Finally Javascript errors do not cause a unilateral exit, it only stop the execution of of the current function and attempts to continue. This can of course cause cascading errors, but more importantly can lead to a more robust program if it designed to suit this style.

These are just some quick examples, and doesn't begin to cover things like custom exception re-raising, but it took a surprising amount of reading to find out these few scant details. Shouldn't these discussions be formalised right after basic syntax in the manual?