?? Operator

If you use nullable types in C# (e.g., int?, double?, boolean?, etc.) you may be interested to know that C# has a special operator for handling what to do if a value is null. It is called the null-coalescing operator. It works like this . . .

int? x = null;
int y = x ?? -1;

If x is null, then the value on the right side of the ?? will be used; otherwise, it will use the value of x to assign to y in the example above.

So, you don’t need to do things like this anymore . . .

int y = (x == null) ? x : -1;

Neat huh?

Conflicting Assemblies

In Visual Studio, when compiling solutions, you may encounter the following error message:

Found conflicts between different versions of the same dependent assembly. Please set the “AutoGenerateBindingRedirects” property to true in the project file. For more information, see http://go.microsoft.com/fwlink/?LinkId=294190.

Or you might see a similar version of this when trying to browse to an ASP.NET website that looks like:

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

To see exactly the path and the assemblies that are causing the problem, you’ll need to turn on logging for the .NET binding process. The information above in the error message is not sufficient to enable this though. Here’s the full information needed in order to see the assembly binding (from Stackoverflow):

DWORD ForceLog set value to 1
DWORD LogFailures set value to 1
DWORD LogResourceBinds set value to 1
String LogPath set value to folder for logs (e.g. C:\FusionLog\)

Make sure you have that trailing backslash in the log path.

Then, restart your application or build and inspect the details in the log file or the output from ASP.Net!

Autofocus in Angular.js

Add an autofocus attribute to your input like this:

<input autofocus>

Then, set the focus on the first visible input with autofocus when angular is ready like this:

angular.element(document).ready(function() {

Let me know if you have another, better way.

JavaScript Design Patterns

Where can I learn about JavaScript design patterns?

Here is a really good book by Addy Osmani title, “Learning JavaScript Design Patterns”.

Here’s a snapshot of the table of contents . . . good stuff . . .

What is a Pattern?
“Pattern”-ity Testing, Proto-Patterns & The Rule Of Three
The Structure Of A Design Pattern
Writing Design Patterns
Categories Of Design Pattern
Summary Table Of Design Pattern Categorization
JavaScript Design Patterns
  Constructor Pattern
  Module Pattern
  Revealing Module Pattern
  Singleton Pattern
  Observer Pattern
  Mediator Pattern
  Prototype Pattern
  Command Pattern
  Facade Pattern
  Factory Pattern
  Mixin Pattern
  Decorator Pattern
  Flyweight Pattern
JavaScript MV* Patterns
  MVC Pattern
  MVP Pattern
  MVVM Pattern
Modern Modular JavaScript Design Patterns
  ES Harmony
Design Patterns In jQuery
  Composite Pattern
  Adapter Pattern
  Facade Pattern
  Observer Pattern
  Iterator Pattern
  Lazy Initialization Pattern
  Proxy Pattern
  Builder Pattern
jQuery Plugin Design Patterns
JavaScript Namespacing Patterns

MVC and View Engines

If you’re coding MVC and using the Razor view engine exclusively, consider changing the default behavior of your ASP.NET web application by modifying the Global.asax.cs’s Application_Start and adding the following lines at the top of the routine:

ViewEngines.Engines.Add(new RazorViewEngine());

By default, both the WebForms and Razor processor are created. The above clears the engines out and then adds back only the Razor view engine. This will increase the performance of your site by not running your pages through the WebForms processor first.

Defaults in C# 4.0 Method Parameters

C# 4.0 was released in April 11, 2010. By that point, C# was 8 years old and there were A LOT of developers out there that knew C# already and didn’t pay much attention to the changes and didn’t incorporate them into their current code. Many were able to keep chugging along happily doing what they had been doing without having to learn about the new features.

That’s too bad.

One of my favorite, 4.0 additions is named-arguments and optional-parameters. I think they are my favorites because of my stint as a Visual Basic programmer during my time between PowerBuilder and C#. You got used to not supplying parameters if you didn’t care to pass them or didn’t need them.

You can easily tell when a developer doesn’t know about this feature too. They tend to have a lot of overloading of a method that essentially hides parameters. And, they make calls to variants of the methods when they want to have defaults and not supply them. Or, they don’t want to have to supply them everywhere.

Their code will look something like this:

public void Broadcast(string group, int unitId) { . . . }

public void Broadcast(string group) { Broadcast(group, 5); }

public void Broadcast() { Broadcast(“Red Leader”); }

From above, you can see that the first method takes two parameters. If this were the only method, you’d have to supply defaults everywhere. So, in the event that you just want to pass the group and always default to 5 in the unitId when you do, you could just call the second routine. Or, if you want to default both parameters, you could call the third variation, which calls the second, which then calls the first. That’s a lot of pushing and popping on the stack just to have defaults.

After C# 4.0, these three methods can be collapsed into just this one method by simply using defaults. Here’s what it looks like

public class Broadcaster
    public static void Broadcast(string group = "Red Leader",
       int unitId = 5) { Console.WriteLine("This is {0} {1}", group, unitId); } } class Program { static void Main(string[] args) { Broadcaster.Broadcast(); Broadcaster.Broadcast(group: "Blue Leader"); Broadcaster.Broadcast(unitId: 2); } }

So, if you just wanted to supply a different unitId, you simply pass in a unitId: 6 in the method call. The group would default, and the effect is as if you called the method with the parameters “Red Leader” and 5.

Defaults make these sorts of libraries much leaner to write.

I hope this helps you.