Categories
.net c# cpp iola Programming

C# from a C++ developer perspective

Been wanting to get this out of my system for a while and since I can’t sleep I might as well do something productive.

During my time in iola I’ve begun writing code in C#. I’ve written quite a lot of C++ in the past and still does, so it was quite interesting for me to see what the next iteration of C would be like when I started out with this. This next section is written in a very practical sense, since at the end of the day, that is really what matters.

The nice stuff:

  • Fast compiler. I really think this is big. The slow compile time in C++ is contributed by several things, templates are really big here. And since they need to live in header files changing them can easily trigger a cascade effect of recompilation that is extremely detrimental to the whole coding experience. There are hacks around this but they are not really optimal solutions. When I did almost all my coding in C++ I didn’t think of this as a big problem, but once you’ve been seduced by the dark side (Take that Siebel ;-)) you really start to notice it a lot. And the fast compile time of C# makes it much more bare able to write in a statically typed language again.
  • Portable library with a lot of useful stuff like threading, network communication, File System abstraction, serialization etc.
  • No more dreaded header files. While this may seems strange at first (since header files are a nice way of describing the interface of the classes with comments that don’t get lost in the smoke of implementations), it’s actually really nice not having the headache of keeping
    the header files in sync with the implementation, making it much easier to move code around. Another big thing here is the whole template thing.
  • Delegates (or function pointers if you will) are extremely powerful and having them built into the language makes them very easy to use.
  • Support for yield.
  • Being able to use the power of .net (Newer in a million years thought utter such words) to mix and match languages. It’s actually quite trivial to write a piece of your program in Boo and have it work with the rest of the code. This is in particularly useful for writing unit tests.
  • Perhaps the biggest improvement, although this varies greatly from ones perspective, is that the whole language raises the bar. In the sense that a lot of the BAD programming practice that has been associated with C++, mostly caused by people simply using the language in the wrong way (char pointers, not taking care of memory management, generally pointers comes to mind here) has been stripped away from the language. A really good example of this is to compare the abomination that is pre .net C++ windows code and .net C# code.
  • C# is constantly improving and is doing so quite rapidly. Compared to the extremely slow pace of C++ (still hoping for a new standard in this decade!) it’s really refreshing to see a lot of good new ideas being pushed out to people.

The bad stuff:

  • Two thumbs up to the designer of C# for stepping up and actually implementing generics in C#. That being said, they are just a shadow of templates in C++ and the whole I-Implement-an-Interface crap is just plain dumb. Templates in C++ compared to generics in C# feels much more like macros in lisp, which if one thinks about it is quite the irony.
  • C# is clearly very proud of their garbage collection and thus sees no problem in demanding that everything must be created using the dreaded new syntax. While writing something like: List<string> l = new List<string>(); is not that big of a problem in something like monodevelop or visual studio. But it’s a big pain in the ass if you’re using the one and true editor. Another thing related to the GC is that needed to invent new syntax (using) to support the good programming practice of RAII instead of just doing it by creating objects on the stack and letting them fall out of scope and thus become deconstructed automatically.
  • C# falls into the trap of thinking that everything must be an object and that object orientation is the true path to righteousness. Namespaces was invented for a reason you know?
  • The built in libraries and especially the documentation is sometimes really really bad. Like methods returning results in different threads than the caller and not writing anything in the documentation about this.

Overall the experience so far has been quite positive and as far as I can see it will only continue to improve. That being said, there are still use cases where C++ is much more optimal and combined with a lot of legacy code C++ will continue to be used for a foreseeable future.

Categories
On the web Personal

Cleaned up the blog a little

Had some time tonight to do some winter cleaning of my blog. I found a new theme that is a little lighter (Thanks Bob :)) And finally found a plugin called Google Code Prettify which allows one to easily display code inside blogs (Something that is utterly pain to do in vanilla WordPress). I also added a related blog posts at the buttom of each blog entry so that it’s easier to find other blogs with similar content. Enjoy 🙂

Categories
boo c# lisp Programming python

The power of lisp part 2

As mentioned earlier, lisp is incredible at abstracting away small everyday stuff you write over and over. A good example is this way one has to define a local variable in order to make variables sucked into a delegates from the surroundings static in C#. As mentioned briefly in this blog post, one needs to change the loop to read like the following code for it to print 0 to 9 instead of 10 times 9:

foreach (int i in numbers) {

        int j = i;

        l.Add(delegate() { System.Console.WriteLine(j); };
}

In lisp one would see that this is a pattern and simply abstract it away like this. As one can see, the macro static-loop is more or less exactly the same as the pattern which luckily made it quite easy to write.

(defmacro static-loop ((i container) &body body)
 (with-gensyms (j)
  `(loop for ,j in ,container do
    (let ((,i ,j))
     ,@body))))

The only small trick is the with-gensyms macro which prevents leaking the temporary variable j into the scope of where it will be substituted into. So in that regard the macro is even nicer than rolling your own temp variable hack 🙂

For reference the python example works the same way, but allows one to use the lambda function to redefine variables, although one must note that the static-loop solution is more general in that it will make the loop variable local for the whole scope and not just for one lambda function. Funny thing is that I had originally written it like this, which actually worked, although that means that the closure binding is not only by reference but by name.

I tried writing the macro in boo but I failed. Apparently their macro support is still quite new and there’s close to no examples to draw inspiration from. If anyone could help me out with the solution please mail it to me or even better add it as a comment.

Categories
boo Books Programming

The power of lisp part 1

Lately I’ve been reading the book Practical Common Lisp. My earlier experience with lisp was at the university where I briefly touched lisp in the form of the Scheme dialect. After having read Paul Grahams excellent Hackers and Painters twice, I thought it would probably be a good idea to see if lisp lived up to the hype. After seeing Practical Common Lisp being recommended by bamboo of boo fame and seeing this google tech talk I was convinced that the book was the best way to see all the wonderful stuff lisp has to offer.

So far I’ve been quite impressed with some of the things one can do in the language in a very elegant way. And I suddenly understand why closures work the way it does in C# and python. The problem with moving stuff from a really dynamic language like lisp to a static language like C# is that not everything fits just as fine and you’ll find yourself wondering why the hell code like this returns 10 times 9 instead of 0 to 9. Another thing is that I often find myself remembering small code idioms that just completely goes away when one can build abstractions so easily. The first example is if you want to break out of a double loop. A situation more often that one would think.

I C# one could write a double loop something like this. Notice the introduction of the found variable which is needed to make sure that the loop doesn’t do needless work. Even with an anonymous function it’s quite ugly (and the same is true for a goto solution :)). In lisp it would look something like this (my first common lisp program :-)). The abstraction is in the block concept and in the generalized return construct return-from.

Edit: Can be simplyfied to this:

(let ((l (list (list 1 2 3 4) (list 5 6 7))))
  (loop named outer for i in l do
	(loop for j in i do
	      (when (= j 3)
		(format t "found value, yeah!")
		(return-from outer)))))
Categories
On the web

Nokia n810

3 words baby, gimme one now

Categories
fowa iola

Future of Web Applications postmortem

FOWA is now over and it has been a great conference. On the way back in the plane I got to thinking that almost all the new web applications work a lot like open source projects. In the way that they are first and foremost open, encourage external contributions (using API´s), encourages the users to drive the development of new functionality, and are actively encouraging feedback from their users in an open way where users can help other users solve problems.

Categories
fowa

FOWA day 2

Day 2 is great, won a red?! 8 gig ipod. Great and funny talks about startups and ducks among other thing so far… 🙂

Categories
fowa iola

FOWA day 1

Day one was really great. A lot of awesome talks. The most interesting guy was perhaps Umair Haque talking about edgeeconomy.  I havn’t  heard the economic  perspectives of open  distributed  production, really interesting. Steve Souder showed a very nice tool called Yslow, that can really help you find speed bottlenecks in your web apps. Anyway next up is Paul Graham so I better stop here 🙂

Categories
iola Personal

Future of Web Apps

I’ll be attending the Future of Web Applications next week. The conference seems to have quite a few interesting talks, I’m particularly looking forward to hear Paul Graham talk since I enjoy his writings immensely.

Categories
On the web

UI design

Saw this very interesting UI-video today called Away with Applications: The Death of the Desktop by Aza Raskin (part of the google tech talks). It shows an interesting application enso, very similar to quicksilver. The interesting part is of course the abstract (what it is a solution to) and the conclusion (and why). Especially things like application silos and how enso marries the CLI with the UI gives some food for thought.