Taswar Bhatti
The synonyms of software simplicity
GhostDoc

Recently have been going through some old code to review the comments in them from other developers and what I find out is developers tend to have really bad comments & documentation in their code

Example:

From the above code it is obvious that it is the constructor but does the comment tell me anything? I understand that sometimes it may be hard to document or comment certain method, thus I really strongly suggest at least use GhostDoc

What is GhostDoc?
GhostDoc is a Visual Studio extension that automatically generates XML documentation comments for methods and properties based on their type, parameters, name, and other contextual information.

Here is the result of using GhostDoc on the Person Class

As one can see Ghostdoc does a better job, don’t get me wrong GhostDoc still requires one to go through the comments here is an example where it tries to make sense.

Riches the text selection changed. ????? As you can see it didn’t do one of the best job for this comment, but at least one can modify it and it may become “RichTextBox selection has changed event handler”

Personally I would recommend using the tool but just take it with a grain of salt and always check the documentation that it generates.

linq

Here is a LINQ tip where you may wish to order a collection with an existing ordering of another collection.

Example:

Currently as it stands our data is stored in the order of { 5, 13, 7, 1, 8 } and we wish to order them in terms of { 1, 8, 5, 7, 13 }, we can use LINQ OrderBy to do so.

By doing so we end up with { 1, 8, 5, 7, 13 }, now if we wanted to put elements that dont have matching at the end of the list we can modify the OrderBy like this.

dotnet C#

Obsolete or Deprecated?

How do you mark a class or method as deprecated/obsolete?

By using the Obsolete attribute

dotnet C#

The ?? operator is called the null coalescing operator. It is used for providing a default value for Nullable types or reference types.

Example:

One of the disadvantage of ?? is it can create code that is not that readable.
e.g a ?? b ?? c ?? d ?? e

dotnet C#

In C# you can have property with different scope (property accessors). Properties in C# can be marked as public, private, protected, internal, or protected internal.

Example:

dotnet C#

C# 4.0 allows one to use named and optional parameters.

Example:

One can call the method in multiple ways

Additional note, one cannot provide a gap in arguments.
Example:

dotnet C#

The using statement in C# is a form of shortcut for try and finally block of code. Things to note is in order to use the using statement the object needs to implement the IDisposable interface, and using does not catch Exception, it just guarantees the call of Dispose.

Example:

Can be replaced with

One will not need to call the Close method of sql connection, since the dispose will do that.

interview

In interviews there is always the Singleton question, I personally refuse to answer it on interview. The reason is very simple I don’t code singletons anymore. I use my IoC Container to provide me with a singleton. Sorry if you dont know what an IoC (Inversion of Control Container) is, then you are still in the old ages of coding.

For once and for all here is the hand written code of a singleton in multi-thread environment.

And here is in an IoC container Unity & StructureMap

As you can see one can have a singleton with one line of code in Unity and say 2 lines of code in StructureMap, so I really wonder if one needs to proof that a double check is needed in a multi thread environment for singleton.

End rant……

dotnet C#

C# has the as keyword, it is mainly used to cast object of one type to another, and if it fails it should return a null rather than crashing the program or throwing exception.

From MSDN documentation it states:
You can use the as operator to perform certain types of conversions between compatible reference types or nullable types. The as operator is like a cast operation. However, if the conversion isn’t possible, as returns null instead of raising an exception.

Differences between as and cast ( ) and when to use it:

  • Using “as“, I think the object is of the type, but if it isn’t give me a null, don’t crash the application.
  • Using cast, I know the object is of this type, and if it isn’t then crash the application.
  • Using is and cast is more costly than just using as
dotnet C#

So C# what is the yield keyword? The yield keyword in C# is basically an iterator, its a form of syntactic sugar added from C# 2.0 to create IEnumerable and IEnumerator objects, thus returning one element at a time.

From MSDN documentation it states.

When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. You use an iterator to perform a custom iteration over a collection. The following example shows the two forms of the yield statement.

Personally I have found using yield helpful in XML streaming where I have a very large document but would like to combine the use of XElement with XMLReader, such that the memory is not consumed like XDocument (where it loads the entire document into memory).

From the above code it will return an IEnumerable of XElement for us to process.

Some notes on using yield.

The declaration of an iterator must meet the following requirements:

  • The return type must be IEnumerable, IEnumerable<T>, IEnumerator, or IEnumerator<T>.
  • The declaration can’t have any ref or out parameters.

Also one cannot use yield in:

  • Anonymous methods
  • Methods that contain unsafe blocks
UA-4524639-2