Archive for the ‘Programmeren’ Category
Alles over programmeren
Implicit conversions in C#
Yesterday I came across an interesting article: What’s the opposite of Nullable. While the solution for Non-Nullability is interesting, the reason i’m blogging this is because the article also used a C# feature which i didn’t know of: implicit conversions.
And guess what? Today I had a situation where I could use these implicit conversions. My app reads data from a CSV-file, so all the input are just strings. Until now that was just fine. However, at one part of my code I had to process one of the fields which has a fixed format. Say a field is a phonenumber and i need the country-prefix. So I created a PhoneNumber-class like this:
public class PhoneNumber { private readonly string _number; public PhoneNumber(string number) { if (!new Regex(PHONE_NUMBER_REGEX).IsMatch(number)) { throw new ArgumentException("Invalid phone number", "number"); } _number = number; } public string Prefix { get { return GetPrefixFromNumber(...); } } } |
Now it is possible to create a (helper-)method in another class to get the prefix like this (this is just a simple example):
public string GetPrefix(PhoneNumber phoneNumber) { return phoneNumber.Prefix; } |
But when you only have the phonenumber as a string, you’ll still have to create an instance of PhoneNumber to be able to call the GetPrefix method:
var prefix = GetPrefix(new PhoneNumber("+32485123456")); |
That is, until we add a method for implicit conversion to the PhoneNumber-class:
public class PhoneNumber { private string _number; public PhoneNumber(string number) { if (!new Regex(PHONE_NUMBER_REGEX).IsMatch(number) { throw new ArgumentException("Invalid phone number", "number"); } _number = number; } public string Prefix { get { return GetPrefixFromNumber(...); } } public static implicit operator PhoneNumber(string number) { return new PhoneNumber(number); } } |
Now it is possible to call the GetPrefix method with just a string. The string will be automagically converted to a PhoneNumber, unless it is invalid, in which case the ArgumentException will be thrown:
var prefix = GetPrefix("+32485123456"); |
Wednesday, March 25th, 2009 - Posted in Programmeren - Comments Off
Techdays
Gisteren mocht ik voor het werk een dagje van de Microsoft Techdays bijwonen. Het was de eerste keer dat ik naar een Microsoft-event ging en had me eerlijk gezegd aan veel show en weinig inhoud verwacht. In sterk contrast tot JavaPolis Devoxx, waar er veel inhoud en weinig show is. Achteraf bekeken viel dit eigenlijk enorm goed mee. Enkel de keynote ging er wat over. Een Developer Evangelist of Regional Director moet echt niet als één of andere superster worden aangekondigd met loeiharde muziek en lichtshow, maar dat is mijn mening natuurlijk.
Na de keynote heb ik de sessie over C# 4.0 gevolgd en dat was erg interessant. C# 4.0 belooft weer een versie te worden om naar uit te kijken. Eén van de nieuwe features is het dynamic keyword. Dit zorgt ervoor dat je methodes op het object kan oproepen waarbij de compiler niet checked of deze effectief bestaan. De check wordt pas ‘at runtime’ gedaan. Dit is erg handig om te integreren met andere talen zoals Javascript en Ruby.
Een andere interessante feature is ‘Generic co- and contra- variance’. Hiermee kan je in speciale gevallen casts uitvoeren zoals:
var list = new List<string>(); (List<object>)list; </object></string> |
Ook optional en named parameters behoren tot de nieuwe features. Dit is ook erg interessant. Bekijk dit voorbeeldje:
public void Order(string name, int amount = 2, int discount = 0) { ... } Order("jeroen", dicount: 5); // the first param - name - is not optional so a value must be supplied. // the second param - amount - has default value of 2 which we want to use // the third param - discount - has a default value of 0 but we supply 5 |
Enkele belangrijke punten bij het gebruikt van deze feature zijn wel dat je parameters absoluut als constante moet beschouwen. Je kan na verloop van tijd in veel gevallen immers niet zomaar de standaard waarde van een parameter aanpassen. Als je in voorgaand voorbeeld de default value van amount zou wijzigen in 3 zouden er plots veel klanten 3 items bestellen in plaats van 2. Ook is het wijzigen van de naam van een parameter niet meer zo vanzelfsprekend omdat deze naam nu ook buiten de methode wordt gebruikt.
Thursday, March 12th, 2009 - Posted in Programmeren - 2 Comments
Boekenfestijn

Net zoals vorig jaar is het weer boekenfestijn in het bouwcentrum in Antwerpen. Ook dit jaar waren er weer ?norm veel interessante boeken. Je zou er echt gemakkelijk een grote winkelkar volkrijgen met stuk voor stuk interessante boeken. (sommige mensen doen dat dan ook
). Zelf heb ik het bij 3 boeken gehouden: JUnit, Tapestry en XDoclet, allemaal van Manning.
Thursday, May 26th, 2005 - Posted in Programmeren - 3 Comments
Standardizing BeanShell
A while ago, when I first wrote about Groovy, I started wondering why there wasn’t a JSR for Beanshell. As you might know, Beanshell is, just like Groovy, a scripting language for Java. Today Pat, the main Beanshell developer, announced that Beanshell will be standardized in JSR 274. You can read more about this on his blog.
Thursday, May 26th, 2005 - Posted in Programmeren - Comments Off
Debugging with Eclipse
Ofcourse Eclipse has a debugger which you can use to debug your Java applications. Like most other debuggers you can set breakpoints, view variables, use watch expressions, inspect objects and use actions like ’step into’, ’step over’ and so on.
But Friday I discovered another nice feature while debugging: When you change some of your code while debugging you don’t need to restart your application. Eclipse simply uses your new code. Something similar is possible with the debugger of Visual Studio 6 and will be possible again in Visual Studio 2005.
Here is a, rather stupid, example to illustrate it:
I created a method ‘getList()’ in the class ‘Foobar’ which should return a List with two elements. Ofcourse i create a UnitTest for it:
Sunday, May 22nd, 2005 - Posted in Programmeren - Comments Off
Eclipse, not just an IDE

In the past half year i have been doing two projects, of which one in .NET (C#) and one in Java. As far as i can tell now I certainly prefer Java over .NET. Although with “out-of-the-box”-programming in .NET (with “out-of-the-box” i mean simply installing Visual Studio) you will get a result in less time, .NET misses the big (Open Source) community that Java has. While there are expensive IDE’s for Java, like IntelliJ, there are also various Open Source alternatives, of which NetBeans and Eclipse are the two most well known.
Especially Eclipse is an extremely beautiful IDE, if not the best IDE ever created. But Eclipse is more then just an IDE. In fact it is a platform to create IDE’s. And by default, when you download the official Eclipse release, it comes with JDT, the Java Development Tools. The architecture of Eclipse is completely build on the idea of plugins. Almost every part of Eclipse is a plugin, including the Eclipse core. This is one of the reasons why Eclipse is so nice. It is very easy to add new functionality to the IDE, or even to completely create a new IDE build on top of the platform. Because of the license of the Eclipse platform, both Open Source and commercial plugins are allowed. And as you can see on sites like eclipse-plugins.info there are *a lot* of plugins available, of which a vast amount is developped as Open Source.
Although a lot of people use Eclipse to develop Java you can use it for a lot more because of the plugins. Examples include: C++, COBOL, PHP & Python, Haskell, NSIS or even UML and more…
Thursday, May 19th, 2005 - Posted in Programmeren - 6 Comments
Groovy: Java Scripting (NL)
Dit is de Nederlandse vertaling van een artikel dat ik een tijdje terug in het engels schreef. Bedankt Vicky voor de vertaling!
Een paar dagen geleden struikelde ik over een artikel dat bij onjava.com Groovy introduceert. Groovy is een scripting taal voor Java, dit betekent dat de groovy-scripts kunnen worden uitgevoerd zonder dat ze eerst moeten worden gecompileerd. Dit maakt het zeer gemakkelijk om kleine scriptjes te schrijven die vanaf de commandoregel of van binnenin een Java-applicatie kunnen worden uitgevoerd.
Tuesday, May 17th, 2005 - Posted in Programmeren - Comments Off
uit de oude doos: MasterMind 2001

Zonet was ik wat opkuis aan het houden in ??n van de vele mappen op m’n harde schijf. Toevallig kwam ik wat oude spulletjes tegen die ik vroeger eens had geschreven. E?ntje daarvan is een eenvoudige MasterMind, geschreven in VB6. En ja hij werkte zelfs
. Omdat hij toch maar op m’n schijf ’stof stond te vangen’ heb ik hem online gezet, met de broncodes erbij (GPL-ed).
Ik wil er overigens wel bijzeggen dat deze code van 2001 dateert en het design van het programmaatje op zo goed als niets trekt. Vandaag de dag programmeer ik wel net iets properder
En ook: als het ding uw computer laat crashen, ontploffen of begint op te eten, ik ben NIET verantwoordelijk
Dat gezegd zijnde:
http://dev.budts.be/releases/old/
De zip downloaden, uitpakken en MasterMind.exe starten en je bent vertrokken ![]()
Wednesday, May 11th, 2005 - Posted in Programmeren - 5 Comments
Groovy: Java Scripting
See below for an explanation why this is written in English

A few days ago I stumbled across an article at onjava.com introducing Groovy. Groovy is a scripting language for Java, meaning that Groovy-scripts can be executed without the need of first being compiled. This makes it very easy to write little scripts which can be executed from the command-line or from inside a java application.
Groovy is not the only scripting language for Java. Others include Beanshell, Jython, JRuby and some more. A nice overview can be found here. People that use jEdit will certainly have heard of Beanshell since it is the language used by jEdit to write Macro’s.
Both Groovy and Beanshell use a syntax based on Java, but their goals are a bit different. Beanshell tries to interprete normal java files and seems to do this very well. So by using Beanshell it should be possible to run a real Java application by feeding the source files to Beanshell, instead of compiling them into .class-files. The goal of Groovy is to provide an easy to use scripting language, by adding some “sugar code”, to make it easier to develop with. An example:
System.out.println("this is a foobar!");
println "this is another foobar!"
Both lines are valid Groovy-syntax and do exactly the same: print a line to the screen. However the second line, in “Groovy-style” is much easier then the first line, which uses regular Java syntax.
Another example of sugar code is some kind of ‘foreach’ using ‘closures’:
l = [1,2,"foo"];
l.each {
println "item ${it}";
};
What happens in this code is the following: first a list ‘l’ is created with elements 1, 2 and a string “foo”. This list is iterated and for every item a string is printed to the screen which results in:
item 1
item 2
item foo
(and it also illustrates autoboxing).
Two other nice things about Groovy are that
- It is possible to compile Groovy scripts into regular Java code. This gives you classes that can be used in any regular Java application.
- It is very easy to run Groovy scripts from the command-line. Simply typing
$ groovy myscript.groovylaunches the script. When you’re using a *nix environment (includig cygwin) it is even easier: start the script file with#!/usr/bin/env groovyand you can start your script with$ ./myscript.groovy
Something else that’s special about Groovy is that it is accepted as a JSR (Java Specification Request). That makes me wonder why Beanshell doesn’t have a JSR, since it is much closer to Java than Groovy.
The reason why the article at onjava.com attracted my attention was that i had already read about Groovy a few months earlier. However that was in a complete different context: an open letter from Cedric Beust (who has an interesting blog btw) to the main developer of Groovy telling him he was making a mess of Groovy by not using a roadmap and always adding new features instead of working towards a 1.0 release. This gave me a rather negative feeling about Groovy and made me think Beanshell was a lot better then Groovy. But now that i have been playing a bit with Groovy i really like that too and think it is just ‘different’ from Beanshell. As an end user i’m very happy with what i have seen from Groovy until now. But ofcourse this negative criticism worries me a bit. I’d rather not invest my time in learning a language which will be dead in the future… Until now i think i’ll just continue learning both the languages, Beanshell to write macro’s in jEdit (although that’s also possible with Groovy) and Groovy to write scripts for the command-line (and this is also possible with Beanshell)
So why is this item written in english?
Since a few months now i’m considering to write my tech-related articles in english to make them available to more people. For my ‘regular’ blog items i think Dutch is just fine and i will keep writing them in Dutch. I think i will just give it a try and see if i (and others) like it. (and you are free to correct my spelling mistakes, it can only have a positive influence
)
A nice presentation of Groovy can be found here
Tuesday, May 3rd, 2005 - Posted in Programmeren - 4 Comments
darcs: versiebeheer op een nieuwe manier

Toen Andreas Gohr enkele maanden geleden opzoek ging naar een geschikte source control-tool voor DokuWiki koos hij voor darcs. Het was de eerste keer dat ik van darcs hoorde, maar sindsdien ben ik het meer en meer tegengekomen.
In vergelijking met ‘klassieke’ SCM-tools, zoals CVS, Subversion en SourceSafe, werkt darcs opmerkelijk anders. Bij deze klassieke systemen heb je meestal enerzijds een centrale server met een ‘repository’ waar al de code wordt bewaard en anderzijds een sandbox op je lokale computer die alle bestanden bevat waaraan je kan werken. darcs echter is een gedistribueerd systeem. Hierbij is dus iedere ’sandbox’ ook meteen een ‘repository’. Dat heeft aardig wat voordelen. Zo moet je bijvoorbeeld geen verbinding hebben met een server om bewerkingen op de repository uit te voeren.
In tegenstelling tot CVS werkt darcs niet met versies van bestanden maar met ‘patchsets’. Zo’n patchset wordt gemaakt aan de hand van alle wijzigingen die je aanbracht ten opzichte van je lokale repository. Dit is handig want op die manier zitten alle wijzigingen die bij elkaar horen in ??n patchset samen. Het delen van je wijzigingen in de vorm van patchsets is dan ook erg makkelijk.
Een ander groot verschil tussen systemen zoals CVS enerzijds en darcs anderzijds is het updaten van de code met de recentste code van andere mensen die mee aan het project werken. Met CVS doe je dit met behulp van het commando cvs update Dit commando haalt de meest recente versies van alle bestanden op vanop de CVS-server. Omdat darcs een gedistribueerd systeem is en dus niet met een centrale server werkt gebeurt het synchroniseren op een aantal andere manieren. Zo zijn er de commando’s darcs pull en darcs push Deze commando’s brengen wijzigingen uit een andere repository over naar jouw repository (’overtrekken’), respectievelijk voeren de wijzigingen uit jouw repository door op een andere repository (duwen dus). Deze methodes werken natuurlijk enkel als je de nodige lees- ?n schrijfrechten hebt op die andere repository (schrijfrechten zijn enkel nodig voor een push). Een andere manier om te synchroniseren, waarbij je geen toegang moet hebben tot de andere ‘repo’ is darcs send Dit verstuurt de patch via e-mail naar de eigenaar van de andere repository. Ook darcs send -o patchfile is erg handig. In plaats van de patch te mailen schrijft het alle data weg naar ‘patchfile’, zodat je het manueel kan mailen of bijvoorbeeld als attachment in je bugtracker kan stoppen. Wanneer je een patch ontvangt kan je die h??l eenvoudig op je repository toepassen: darcs apply patchfile
Erg handig aan darcs is dat je een repository online kan plaatsen via een eenvoudige webserver. Nadat je een repository op je webserver hebt gezet, bijvoorbeeld via ftp, kan iedereen een darcs get http://example.org/my/darcs/repo doen met de url. Op die manier heeft iedereen die dat wenst leestoegang tot je repository en kan men lokaal wijzigingen maken. Deze wijzigingen kunnen dan eventueel weer in de vorm van een patchfile naar de eigenaar van de repository gemaild worden. Door een repository op een server beschikbaar te maken maak je het ook makkelijker om te synchroniseren omdat deze repository dan beschouwd kan worden als de ‘moederrepository’. Iedereen is echter steeds vrij om z’n lokale repository al dan niet te synchroniseren met deze moederrepository.
Vermits darcs met patches werkt is het ook perfect mogelijk om een eigen aangepaste versie te maken van een applicatie die je toch automatisch kan updaten met offici?le code zonder je eigen wijzigingen te verliezen.
Hoewel ik nog niet zo heel veel heb kunnen experimenteren met darcs vind ik het tot hiertoe een zeer knap systeem. Zeker voor kleinere projecten lijkt het me erg geschikt. Ik plan dan ook om voor m’n eigen kleinere projectjes, zoals Nucleus Plugins, CVS in te ruilen voor darcs. Ik heb alvast een plekje opgezet waar ik al m’n repo’s online zal plaatsen: http://dev.budts.be/darcs/. Momenteel vind je daar al een repo met de meest recente code van AttachFile, een Nucleus plugin waaraan ik momenteel werk.
Wat momenteel nog wel als een nadeel kan overkomen, vooral dan voor (windows)gebruikers die niet gewend zijn aan de commandline, is dat er nog geen enkele grafische interface beschikbaar is voor darcs. Hieraan wordt echter wel gewerkt door afzonderlijke projecten, zoals bijvoorbeeld EclipseDarcs.
Het overzetten van een bestaande repository, bijvoorbeeld je CVS repository, is vrij makkelijk met de verschillende tooltjes die beschikbaar zijn. Het overzetten van mijn CVS repository voor de AttachFile-plugin naar een darcs repository ging bijvoorbeeld vrij gemakkelijk met het cvs2darcs tooltje. Hoewel ik hiervoor wel even heb voor moeten prutsen met Cygwin en enkele benodigde programma’tjes zelf heb moeten compileren.
Een kort voorbeeld van hoe je de repo van AttachFile zou kunnen gebruiken vind je in de versie van dit artikel op m’n Dev-Blog.
Wednesday, April 27th, 2005 - Posted in Programmeren - 3 Comments
