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

Monday, December 11, 2006

Hooray for Open Source!

Jeremy and Ayende (and probably others by now) have been considering the Microsoft attitude to Open Source.
I can see why companies don't like GPL, but it can turn out to be a good thing.
The very fact that Linksys decided to use Debian Linux kernel for their NSLU2 device, and were obliged by the GPL to release (bits of) their code, has meant that a whole community has sprung up around it (http://www.nslu2-linux.org/), who figured out how to run Debian on it, which has resulted in their product being used as a lightweight, low-cost, server. (yeah, 32MB ram and 266Mhz is hardly going to win any performance awards, but for ~£40 + cost of storage device, what do you expect?) [Edit: Thanks to Øyvind Repvik for setting me straight on this]
Jeremy makes the point about Agile development in .Net only being possible with open source tools tools. I don't think that this is just related to OSS. In the places that I've worked (my view of the 'Microsoft world'), people don't seem to want to know about something unless it comes from Microsoft. (The only notable exception is the miles of sub-standard code that's been copy-pasted from the code project).
Agile? Not until MS announced they'd been dabbling with SCRUM.
Unit Testing? Not really, and still not very much (too much poorly written code based on sample provided by Microsoft).
IoC/Dependancy Injection? Despite working with numerous "plug-in" frameworks, based on DLLs, then COM, and now .Net, people still don't see the benefit of taking it that one step further and creating all of their code based around this model.
In my view, the problem in the .Net world is that there are too many .Net developers. Many of these have come to it from VB and from ASP, and have little experience of rich OO architecture, and many wouldn't know a proxy from an abstract factory.
What happened to the Visual C++ developers? Most of the Visual C++ programmers I know are still developing in unmanaged C++, claiming the performance of .Net prevents them from moving.
To be honest, I've been trying to plot ways of leaving .Net, and moving to RoR or Java, as a way to escape the crippling laziness/poor practised that I consistently experience in Microsoft based developments. I'm fed up with having to teach people about loose coupling, dependencies, and other really basic stuff - the kind of things that other development communities seem to have tackled years ago, and from which they are now reaping the benefits.

Wednesday, October 18, 2006

anonymous delegates in .net 2.0

This is a real quick sample of how anonymous delegates work in .Net 2.0.

your simple code:

delegate void test ();
public static void Main()
{
test r = null;
for ( int i = 1; i < 11; ++i )
{
int p = i;
test x = r;
r = delegate { Console.WriteLine ("{0}", p ); if(x != null) x(); };
}

r();
Console.ReadLine ();
}

gets translated to:
class tmpClass
{
test x;
int p;

public void Execute ()
{
Console.WriteLine ("{0}", p );
if(x != null)
x();
}
}

delegate void test ();
public static void Main()
{
test r = null;
for ( int i = 1; i < 11; ++i )
{
tmpClass tmp = new tmpClass();
tmp.x = r;
tmp.p = i;

r = new test(tmp.Execute);
}

r();
Console.ReadLine();
}

So, for this simple (contrived) example, you end up creating 10 temporary objects.
I really like the idea of anonymous delegates, but their use needs to be reviewed carefully.

Thursday, September 21, 2006

Paging In TSQL

There's a lot of crap written about paging in TSQL (SQL Server's SQL). Then I found this article that uses SET ROWCOUNT to do it. Brilliant.
However, there's no error checking, so I've tweaked it a bit, and my results are below. Enjoy.

DECLARE @Start int, @End int

DECLARE @StartId int
DECLARE @NumRows int

SELECT @Start = 11, @End = 20

IF @Start <= @End
BEGIN
SET ROWCOUNT @Start
SELECT @StartId = addressid FROM Person.Address
order by addressid Asc

IF @@ROWCOUNT >= @Start
BEGIN

SELECT @NumRows = ( @End - @Start ) + 1

SET ROWCOUNT @NumRows

SELECT * FROM Person.Address
WHERE addressid >= @startId
order by addressid Asc
END
SET ROWCOUNT 0
END

Friday, August 11, 2006

Cracked it.

Finally, found a way to AppUnload IIS sites using PowerShell.

New-Object System.DirectoryServices.DirectoryEntry "IIS:// servername/W3SVC" | %{ $_.get_Children() } | where { $_.get_SchemaClassName() -eq "IIsWebServer" } | where { $_.serverComment -eq " sitename" } | %{ $site=$_;"-----";$site.get_Name(); $site.ServerComment; $site.get_Children() | where { $_.get_SchemaClassName() -eq "IIsWebVirtualDir" } | %{ $_.Invoke("AppUnload") } }
where { $_.serverComment -eq " sitename" } is useful if you need to pick site via the "server comment", which is the name displayed in IIS. If you only have one site (like on XP), then you can remove/ignore this. Also, it prints out the site "name" (this will be a number) and "server comment", which isn't strictly necessary, but then at least you'll get an ideas what's going on.

The bit I got stuck on was that the full path to a the object that has "AppUnload" is "IIS:/localhost/W3SVC/1/ROOT" - I was trying to do it on the "IISWebServer" element...

Monday, August 07, 2006

nvarchar(max)

Although I can find little information about it, nvarchar(max) is one of my favourite features, to-date, of SQLServer 2005. (It's not a LOB, it's a Large Value Datatype!)

Wednesday, June 28, 2006

Erlang

I had a little look at Erlang over the weekend. I was inspired by some articles on defmacro.org which I found through del.icio.us.
I've not done functional programming since Uni (apart from some XSLT, and that required some deep thought), and I don't think I really appreciated it at the time.
I'm not converted (yet), but there are a few concepts that I really like.
For example, I was debugging a (non-functional) program yesterday. Essentially, it was calling various functions (in some cases recursively) to build up a string. All these functions changed class level variables, rather than each function returning the bits to add onto the string.
The debugging was complicated because I had to step through each function because any function could append something to the class level variable.
It wasn't pretty. (I count this abuse of class variables - basically using them as global variables.)
What would have been better is if the functions returned the string to append to the class level variable, and one controlling function dealt with this.
So, instead of this:

var importantString = '';
function a(i) {
if ( i < 10 ) {
importantString += '' + i;
b(i);
}
}

function b(i) {
importantString += ',';
a ( i + i );
}
it should be this:
function a ( i ) {
if ( i < 10 )
return '' + i + b(i);
}
}


function b (i) {
return ',' + a(i);
}
I realise this is a trival example, but by eliminating the global variable, it makes it a lot easier to find problems, and to test. For example, we can expect a(1) to always return '1' + whatever b(1) returns - if we replace b with a mock version, then we can test it really easily. Similarly, testing b become equally as easy.
In Erlang, variables are immutable so we cannot recreate the first version The second version (easier to test, easier to debug) , therefore, becomes the way to do things.
I'm not saying you can't write some terrible code in Erlang, or with functional programming, but I wanted to demonstrate how features in one language can be usefull constructs in other languages.

Friday, June 23, 2006

Windows PowerShell

Apart from having a crap name, this is quite some tool [link].
Eg: I can add a registry value like this:
new-itemproperty registry::\HKEY_LOCAL_MACHINE\Software\CDS -name test -type string -value "monkey"
Brilliant!
Or I can stop the service that I'm developing without having to remember it's stupid name:
gsv ePIMS* | Stop-Service
Astounding.
There's a steep learning curve, and the WMI stuff is huge, but I think it's coding to be worth the effort.
(oooh, and just to clean up: rp registry::\HKEY_LOCAL_MACHINE\Software\CDS -name test)