Sunday, November 23, 2008

PHP and .NET

Outside of the “Microsoft’s world”, there are few technologies and programming languages that I enjoy. PHP is definitely on that small group.

Up until now, the only way to load a .NET assembly into PHP and use its types, functions and so on, was to use COM objects. Now, the PHP team is actually developing functions to load a .NET assembly directly without using COM objects (at least, not in code). It has been in testing for quite a while, here’s the link:

Thursday, November 20, 2008

LINQ - Just because you can

Day after day, the .NET Framework evolves in a fantastic way and if you know how to take advantage of the new features, your productivity can actually improve. However, if you don’t have the knowledge, you will do rude mistakes. The latest I encountered was something like this (adapted to the widely used person example and reduced to fit the blogpost):

(click to enlarge)

The idea was to search a collection (in this case, a Person’s collection) and print out the items that did match a certain condition (in the above example, Person’s whose age is over 18). So far so good, the output is what was expected. But the programmer decided that would be “cool” to write it using LINQ. Can you identify the problem on the above example?

Well, as you might expect, the “Where” extension doesn’t actually perform as “an SQL where clause”. The “Where” extension translates in a foreach statement:

(click to enlarge)

So, in the above example, first the user iterates through the collection to find the items that match the condition and then iterates through those items to print them. Two foreach statements, something that could be done with one foreach and one if clause… And that’s not all, but it’s enough to make my point of view.

I know this is a “way too basic” example, but the point is that tools shouldn’t be used “just because you can” or “they’re cool”. LINQ is useful in a wide number of scenarios but not on every task you have.

Thursday, November 6, 2008

Optional Parameters (C#)

“Yes”! This was the word out of my mouth when reading about the new C# 4.0 that will be coming soon. Why? Well, a couple of months ago I wrote a post about optional parameters in VB.NET and how useful they can be. I also questioned why the C# team didn’t implement this feature. My answer is here (quoting a white paper from the C# team):

Named and optional parameters
“Parameters in C# can now be specified as optional by providing a default value for them in a member declaration. When the member is invoked, optional arguments can be omitted. Furthermore, any argument can be passed by parameter name instead of position.”

And the same white paper goes on justifying this new feature much the way I did on my blog post:

COM specific interop features
“Dynamic lookup as well as named and optional parameters both help making programming against COM less painful than today. On top of that, however, we are adding a number of other small features that further improve the interop experience.”

They’ve taken the “optional parameter” idea even further and introduced Named Parameters.
Day after day, the .NET world keeps on surprising me.