Delegates and Optional Parameters
C# 4.0 adds a bunch of new features, amongst which there is the Optional Parameters. If you don’t know about them and their drawbacks, please read this.
So, using this new syntax, you can specify so of the parameters to be optional, and have a default value in case they are not specified. Did you know that this applies to delegates too?
Here’s a simple delegate type declaration:
delegate void PrintName(String name = "Philippe");
Given this delegate type, you could think that any method receiving an optional String parameter and returning void would fit. Actually, it’s not the case. Any method receiving a String parameter and returning void will fit. If the parameter is not given when invoking the delegate, the optional value of the parameter will be used.
Here is a working example:
static void PrintToScreen(String name) { Console.WriteLine("Hello in console from {0}", name); } static void Main(string[] args) { PrintName p = OptionalsParametersOnDelegates.PrintToScreen; p(); }
This code will call the methods PrintToScreen with “Philippe” as argument for the name parameter. The delegate instance p can also be called with any String argument, which will be passed to the methods in the delegate invocation list.
Now, what happens if the method PrintToScreen also has an optional argument?
static void PrintToScreen(String name = "Bill") { Console.WriteLine("Hello in console from {0}", name); }
Making the name parameter optional and giving it a default value will not change the output of the previous code. It’s actually quite easy to explain: when the delegate instance is called with no parameter, the methods in the invocation list are called with the default value of parameter defined in the delegate type, in this case “Philippe”. If it is called with an argument, then the argument is passed along to the methods in the invocation list.
Delete Unused Code Instead of Commenting It Out
While reading The Art Of Unit Testing, I was stuck on a particular sentence:
You should then remove (not comment out) the invalid requirement and its tests.
This made me think about something that happen very often: when modifying code, we are tempted to comment out some of the code that is not used anymore.
I suppose that we do that for two reasons:
- If we need to get something from that old code, it is easy to get it back as it is still there.
- Deleting code is a very hard psychological action for a developer. In Code Complete (well, I think it’s in there, I don’t have a copy here to confirm), Steve says that for a developer, throwing away code that he wrote is very difficult.
However, remember that as your source code is under version control (it is, right? If not, read this and this), so you should not worry about deleting stuff. If you accidentally delete something that you later want to get back, use undo or have a look at the previous version of the file. If some code is not used anymore, just get rid of it.
Comments should be there to help the next developer to understand the code. If you comment out a line of code but leave it because it makes the code more clear, that’s fine. Commenting a whole method that is not used anymore doesn’t help to understand the code, so it should be deleted.
Constant in Eclipse and in Visual Studio
Java naming conventions states that constants, declared as static and final, must be named all uppercase. Eclipse will automatically turn these guys in blue and italic. Here is an example of what it will look like in Eclipse:
public class Sandbox { public static final String CONSTANT_VALUE = "Hello World!"; public static void main(String[] args) { System.out.println(CONSTANT_VALUE); } }
That’s pretty neat. You cannot miss a constant, even if it is not fully qualified (it has to be if it is from another class, of course).
According to Microsoft’s naming conventions, constants in C# (that use the const keyword, being compile time constants) have to be named using Pascal Case. Visual Studio doesn’t color it any special way, meaning that if it is not qualified, you cannot know if it is a constant, a propertie, a variable…
Here is what it looks like:
class Sandbox { const String ConstantValue = "Hello World!"); static void Main(string[] args) { Console.WriteLine(ConstantValue); } }
I find this a bit disturbing, as constant should always be easy to recognize. Well, at least that what I think. I was not able to find any option in Visual Studio to change this. A good start is to always fully qualify constants, in this case using Sandbox.ConstantValue (same advice goes for this keyword, I always use it, even when unnecessary).
You can find some more details on this subject here on stackoverflow.
On String Comparison Conditions…
When I was doing String comparison, I always use to do it this way:
String s = "Philippe"; if (s.Equals("Philippe")) { //Not relevant }
For me, it seems the natural way to write that condition. You can read it out loud as “if s is equal to Philippe”.
However, as the String could come as null, I always ended up with something like this:
if (s != null && s.Equals("Philippe")) { //Not relevant }
This way, the s.Equals wouldn’t throw a NullReferenceException in case of s being null. Again, reading it out loud would be: “if s is not null and s is equal to Philippe”.
And what about doing this:
if ("Philippe".Equals(s)) { //Not relevant }
It feels a lot less natural to me, but the benefits are worth it. Only one condition and no risk of NullReferenceException. If s is null, this will simply return false. It would read as “if Philippe is equal to s”, which doesn’t sound too good, but doesn’t hurt code’s readability.
Just a small trick I wanted to share. I figured it out while reading someone else’s code, which is always interesting.
Bing doesn’t defeat Google yet…
Two month ago, I set Bing as my default search engine in Chrome. I did this after reading one of Scott Hanselman’s blog post. The idea was to give the new Microsoft search engine a try, see if it could handle my daily needs.
During the first month, everything went right, I didn’t have to go back to Google once as far as I remember.
However, for the last few weeks, I had to go back to Google for some searches… I’m sure you know the feeling. You search for something on a search engine that is not Google, and when you look at the results you feel like something is hidden from you. Then you head to Google, try the same keywords, and get exactly what you were looking for.
I’m a bit disappointed. I really liked Bing’s overall look and feel, the changing background image, and the fact that it was a competitor to Google supremacy. I thought they finally put together a good search algorithm. In fact they are quite close, but not there yet.
I’ll give it another try in a few months, but for now I’m back to Google.
