Sunday, April 5, 2009

Language Extensions

Here are a few features that I would like to see built into later versions of C#/Java (actually, two of the ideas below were suggested by a friend, but I figured that I would blog them).

The "Block" Statement

Switch blocks are a very nice way of avoiding complicated nested-if structures.

For example:

Switch (currentCount)
{
case 0:
DoOneThing;
case 1:
DoAnotherThing;
case else:
throw Exception;
}

 But this can only be used in situations where the conditions equate to constants (okay, C# allows some lenience, such as supporting strings as well). But, if you need to have quite complicated evaluations of conditionals, then Switch blocks normally don't work. 

So we go into "nested-if" land:

if (GetCurrentCount() == 0)
{
DoOneThing;
}
else 
{
if (GetCurrentCount() == 1)
{
DoAnotherThing;
}
else
{
throw Exception;
}
}

This can work, but can also get messy, and is prone to bugs.

A workaround is to use some form of block mechanism, for example:

do 
{
if (currentCount == 0)
{
DoOneThing;
break;
}
if (currentCount == 1)
{
DoAnotherThing;
break;
}

// currentCount must be an illegal value
throw Exception;
}
while (false)

So, this works, but is a bit messy in terms of a redundant do-while loop (and dangerous if the loop doesn't terminate). A simple language extension could be the "block" statement, as follows:

block 
{
if (currentCount == 0)
{
DoOneThing;
break;
}
if (currentCount == 1)
{
DoAnotherThing;
break;
}

// currentCount must be an illegal value
throw Exception;
}

This simply allows you to code up those complicated logic statements that sometimes you just can't avoid/simplify/restructure. Sure, this is basically the same as a do-while loop, but makes the purpose clearer, and is slightly more efficient too (both for the compiler and the runtime). It also avoids have to use nasty nested-ifs, which can be the source of plenty of bugs. 

Pre and Post Conditions

Annotations (or Attributes in C# land) are a useful construct. In Java EE, annotations can be used on domain classes (EJBs) to specify constraints on properties. For example, you can specify that the "age" of a person must be a positive integer up to a value of 100; if this constraint is violated, an exception is raised. Here is an example taken from wikipedia (http://en.wikipedia.org/wiki/Java_annotation):



Following on from this, has anyone seen the Eiffel programming language? Eiffel is fairly funky, and is very focused on correctness of design (which I like!). The language has the concept of pre and post conditions... in other words, it has runtime checks to make sure, for example, that a reference passed in to a method is not void, that the object is in a correct state for the method to be called in the first place, and that after the method has been executed, everything is in order as well. Here is an example taken from wikipedia:



So, how about some form of merger between annotations (C#/Java) and Eiffel's pre/post conditions mechanism? This all comes down to the idea of Design by Contract, which I think is fundamentally a good one (formally specifying what the software should do, with runtime checks to guarantee these assertions). As an aside, language level assertions I feel are great as well, but typically are turned off in production, and are a fairly coarse-grained approach to enforcing design by contract.

The idea here is to have (in modern languages, such as C#/Java), some inbuilt support for pre and post conditions. The logical place for this (for me) seems to be annotations/attributes. Now, I can certainly write custom annotations, but it will require runtime support within the VM as well... any takers out there?

The New Keyword

This is a simple enhancement, just to reduce the amount of coding required on a day to day basis. 

In Java/C#, a new object is created as follows:

X myX = new X();

Why can't this be reduced to 

X myX = X();

or even

X myX();

To me, it seems like the "new" keyword in this context is redundant. 

How About Yours?

Are there any other enhancements that you would like to see in Java/C#? Let me know!

No comments:

Post a Comment