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.

The Map Man Says GOTO is Evil

In the days before the C# language became popular, many people coded in BASIC.

BASIC has a rather controversial statement in the family of jump statements called GOTO. The GOTO statement lets you move the flow of control to any point in the source code that you desire. It was usually used within the THEN or ELSE clause of an IF statement. But, if you had numerous nested-IFs it could be difficult—if not impossible—to look at the source code and have any idea what it was going to do. So, you had to be careful how you wrote your code. You still do.

At the time, there was some debate on the use of this statement with respect to the maintainability of source code. And, it was much frowned upon in many circles to the point where one would be judged badly for using it.

Well, this morning, as I sifted through the changes in the C# Language Specification 5.0, I came across the goto statement. I smiled and thought to myself, “They haven’t killed you yet?” I kind of like the GOTO statement. It lets you do what you want to do; however you like . . . make up your own rules . . . you can be a rebel if you like (kind of like commas, semi-colons, em-dashes and ellipses in grammar),

Then I remembered this essay, “A Case against the GO TO Statement,”   by Edsger W. Dijkstra, the creator of the algorithm for finding the shortest path from a to b in a directed graph; or in layman’s terms, the initial ancestor of the algorithm that Google maps or your GPS receiver uses when it plots how to get from your house to the grocery store within the shortest amount of time or distance—kind of a hard thing to do.

Having studied and used some of his algorithms in college, he certainly had and still has my respect. And, if you’ve ever used a GPS, he should have yours as well. He kind of knows what he’s doing wouldn’t you say?

Back in school when I came up on the essay, I gave it strong consideration when he said:

The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one’s program.

With the exception of using, say, JMPs in assembly language (the equivalent of a GOTO in BASIC), I have tried to avoid using them as much as I can for the very same reason he states above.

But, I am happy that you can still GOTO in C#. So, if you feel like being a bit of a rebel . . . GOTO for it! Just don’t over do it . . . the Map Man might come looking for you (and he knows the quickest way to get to your house!)

Anders Hejlsberg—My Favorite Geek

Aside from Bill Gates, Anders Hejlsberg is my favorite geek.

In the mid-80s, my black-and-white TV had the phrase, “UNDER LICENSE FROM MICROSOFT” burned into the screen. It looked like the image to the right here (only in B&W).

But, once I grew out of my TRS-80 MC-10 and my TRS-80 Color Computer, BASIC was no longer a viable language to be coding in. I was in the PC world now with my AT&T 6300 on loan from my employer. And to code on that, I needed Turbo Pascal.

I loved Pascal. It was how I thought about software. And, Borland’s books taught me how to code in it and how to think about software. Later, Borland came out with Object Pascal, and Anders taught me object-oriented programming. And, along with Charles Petzold, I learned event-driven programming in Windows 3.1. Petzold and CompuServe was Stackoverflow and Google all rolled into one.

And then Anders created Delphi and combined windows and Pascal into one GUI that produced tiny, fast .exe’s that blew away Visual Basic in so many ways. Delphi was wonderfully easy to program in.

Well, Bill Gates was having none of that. So, he swooped into Borland and stole Anders from Philippe Kahn—killing Delphi. Then, Gates let Anders work his magic at Microsoft. And, when VB.NET and C# came out with the new Visual Studio and the .NET libraries, I felt the same feeling as I had felt when I experienced Delphi.

This was right. This was the way it’s supposed to be. Anders hasn’t let us down.

And now . . . he’s working on TypeScript . . . listen to “the man” talk about it . . . and download it if you like it . . . from my favorite geek, Anders . . . he’s making JavaScript better these days . . . too . . . cool . . .

Vision First . . . Then Innovation Follows . . .

I found this old video of Steve Jobs in the early years of Apple; at the time of this video, there were about 50 employees.

I think you’ll enjoy seeing a young, undaunted man trying to convince himself of his vision.

I like hearing about the journey driven by his vision which drove his innovation.

I like this quote too:

Simplicity is the ultimate sophistication

as well as other quotes about how the Apple II won’t be obsolete (18:42) and that they won’t make people buy newer versions of everything again.

Knowing the truth, you can seem him struggling with the decision to either try and educate a stranger about the necessity of making things obsolete or tell the stranger what he wanted to hear; which is what Jobs did.

Here’s the video . . . enjoy:

Entity Framework Books

Where can I find free ebooks on Entity Frameworks?

IT eBooks has several. You can find them below (just click on the image). They are a little dated but still contain a lot of good information.

There are a lot more on the site. These are just a few to get you started . . . enjoy.

Error Logging for MVC

What’s a good error logging tool for MVC applications?

There is a neat system called ELMAH that you can use. It’s web based and installs as a part of your entire web application.

Through configuration files, you can require authentication and assign roles pretty easily to protect the pages. It will show you all the logging information your application is producing.

You can install it into your MVC application with NuGet.

Here’s more information about ELMAH.