Using extenders to abstract details and improve code readability

Extenders are a really cool feature of C#, which can be quite handy sometimes. I found the following extender I've written the most useful. Think for instance how often you have to write conditions like

if(someObject.stringPropertyX.Equals("abc") || someObject.stringPropertyX.Equals("def") || ....){
    //do something
    ...
}else{
   //do something other...
   ....
}
This concatenated or's or also ands may be quite messy to look at and also to change. And in addition it's not very comfortable to write since you repeatedly have to write the object.property etc...(or copy&paste it). Packing all of this inside an extender method for the string object (this is just a special case, you could do that of course also for other kind of objects) makes everything much cleaner:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Some.Namespace.Extenders
{
    public static class StringExtender
    {
        /// <summary>
        /// Evaluates whether the String is contained in AT LEAST one of the passed values (i.e. similar to the "in" SQL clause)
        /// </summary>
        /// <param name="thisString"></param>
        /// <param name="values">list of strings used for comparison</param>
        /// <returns><c>true</c> if the string is contained in AT LEAST one of the passed values</returns>
        public static bool In(this String thisString, params string[] values)
        {
            foreach (string val in values)
            {
                if (thisString.Equals(val, StringComparison.InvariantCultureIgnoreCase))
                    return true;
            }

            return false; //no occurence found
        }
    }
}
So using the extender with the dummy example above would result in the following code
if(someObject.stringPropertyX.In("abc", "def",...,"xyz"){
   //do something
   ...
}else{
  //do something other...
  ....
}
Doesn't this look much nicer?? I find it much more readable. It abstracts and hides all of the logical operators and property calls. By looking at the code you immediately see the important facts without being distracted. You're also much quicker in adding (in this case) new strings for comparison in contrast to the case before where you have to make sure to copy (write) the object + parameter invocation, add the logical connector properly etc... You could easily do the same with the AND logical operator and also for the corresponding negations (I just didn't work out the details).

A thing which I don't really like about the extenders is the fact that you have to explicitly define the extender class's namespace in the new class where you want to use the extended methods. Otherwise you won't see your extended methods. This is kind of stupid because the other developers in the team won't recognize the method until you tell them explicitly to use it (which anyway is the best way to ensure it).


Related links:
http://manfred-ramoser.blogspot.com/2008/10/extension-methods-in-c.html

Webclip: jQuery Intellisense in VS 2008 - ScottGu's Blog

I came across jQuery on the web already several times, but I never took a deep look at it. Now some days ago,  Scott Guthrie posted some steps on his blog that show you how to add Intellisense support jo jQuery inside Visual Studio (and also the Express Editions). So take a look at it, it's quite interesting...

That was the outcome of my try, in just 3 minute it was working:
Here's a related link to Peter's blog where he points out "how to set the focus to the username textbox of asp:LoginView ".

Webclip: Mashups, geocoding with Google Spreadsheets (and Gadgets)

Amazing how people exploit different technologies to achieve their goals and still more amazing how much things you can do combining those freely available services:

C++ linking libraries to build configuration in Eclipse

When programming in C++ you may often have to link at compile time to 3rd-party libraries which you're using.
In Eclipse you can do that on the project properties dialog, as demonstrated in the screenshot below.

C++ experiences

Most of the courses I attended during my Bachelor were Java oriented with some C#, C and Haskell programming. C++ never got really touched, and if, then just to illustrate some theoretical stuff, meaning some C++ code was used to describe a design pattern etc..Well in the Software Engineering Project course we covered C++ pointers, references and how the stack and heap gets managed, but again, just theory. So you always hear about how hard it is to program C++, how awkward pointer management and memory allocation and deallocation is.
Now for the Master course "Data Mining and Data Warehousing" we really have to get in touch with C++. The task of my group is to develop a spectral clustering algorithm and to integrate it into a system called XVDM (developed by the DIS Centre at the Free University of Bolzano). We already presented the first prototype where the different clusters are just printed on the command line. Now after starting with the real integration into the big system (XVDM) we faced quite a lot of difficulties and then you really get a feeling of all this theoretical stuff. So for instance the integration seemed to work well on my collegues Debian installation (running inside a virtual machine on top of windows) but the SAME code produced a segmentation fault on my Ubuntu machine (installed natively).
Using "valgrind" helped to identify the problem (thanks to Prof. Arturas Mazeika for this hint). It allows you to start your program similar as the following

valgrind ./SpectralClustering < ./data.csv
where "data.csv" contains the data to be clustered which is passed via redirection to our program. Valgrind then outputs the memory leaks and what may even be more important, the un-initialized - but used - variables which actually was the problem in our case. It turned out that our module integrated into the XVDM system did not initalize it in the right order meaning we had something like
init glut
create cluster objects
create DBScenery object

DBScenery->addAlgorithm(clustering)

initOpenGL()
(this is just some sort of pseudocode). The problem was that the addAlgorithm(...) method made use of variables which got instantiated inside the initOpenGL() call which then of course produced a segmentation fault. That seems to be clear, but how was the program then able to run on my colleagues machine, making use of a not yet instantiated variable?? I don't know..

Anyway, the clustering integration works now as you can see :)
 
I suppose we have to tune it now, in order to be able to process much larger datasets.