Monday, July 14, 2014

Rules on javascript style - Part 1

So style and standards are very important to me. I'm a bit of a fanatic when it comes to them, reason is that they make for better code that is easier to maintain and easier to get new developers up to speed on.

With the javascript community there are a lot of standards out there and some teams have none, others say they do but they make little sense to people. And when you ask why? you get multiple answers which is a sure sign you have unclear standards.

So to start out with I'm going to pull in an except from one of my favorite standards guide, written by one of mine and your favorite companies, Google!

This is the section on naming:
use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, CONSTANT_VALUES_LIKE_THIS,, and filenameslikethis.js.
 The filenames is the one I see the most common errors on, and people will say it will never happen but I've worked in mixed teams before, and by mixed I mean windows, mac, and ubuntu, and we had issues.

Filenames should be all lowercase in order to avoid confusion on case-sensitive platforms.
Filenames should end in .js, and should contain no punctuation except for - or _ (prefer - to _).

The last part I myself have sort of broken but I'm not sure it really is a break. For unit tests I will do filename.spec.js as the format I feel it makes it easier to spot the test files.

Anyhow more later. Enjoy!

Monday, November 26, 2012

The State of Backbone JS

So over on the DailyJS Alex Young just posted great info on the upcoming Backbone release.

Here is a link to the original post.

To quote a few of the highlights:

There are more bug fixes and and internals, with what I hope are some optimizations.

Friday, June 1, 2012

Self restarting windows services

I recently went through an interview where the interview was running over and the interviewer was trying to kill time so he was telling me about a GDI+ handle bug in .NET. Now for those who know me and have read my published works, you know that I am a UI specialist, so my ears instantly perked up to listen to this issue.

The situation was that they were using a windows service that was creating images and printing them off or something. Anyhow since the service wasn’t a GUI app it was not constantly being started and stopped and so with time it would use up all the available handles.

The solution that this person came up with was to create another service to watch the handles in the other service and when it had to many it would start another service to restart the first service.

To clarify:

Service A creates and prints images that use up handles in GDI+

Service B watches Service A for high handle usage, on detection it starts Service C

Service C restarts Service A

Wow that’s a lot of code and work. How I would deal with it is a combination of configuration and code all in Service A.

So we have Service A keep track of the handle counts, then when we hit the thresh hold we have the service call Environment.Exit with an error code greater than 0, which seems appropriate. Then on install we configure the service to restart on error.

Voila a self restarting service, well sort of, but much less complex than the previous scenario.

Thursday, July 1, 2010

Bing Ajax Control over HTTPS or HTTP Secure

So I have not updated in awhile, why you ask? Because I have been neck deep for months on 5 large projects and one of them has been “Geo Mapping”.


So recently I was integrating the project into the main portal site, which runs under HTTPS. We are using the Bing API  and all I had to do was flip the URL to the Bing control from HTTP to HTTPS.

Woops, wrong there. Not entirely anyhow everything worked except that the map tiles were being pulled from HTTP still. So after hunting about for a day I found an undocumented parameter, well no documentation that I could locate.

So here are the two ways to add the Bing Ajax control to your page in HTTP or HTTPS:


<script src=”” type="text/javascript"></script>


<script src=”” type="text/javascript"></script>

Now on the HTTPS version notice I changed the URL to start with HTTPS, this is what pulls from the secure site over at the good Bing folks. Also notice I added the “s” parameter and set it to 1, default is 0, this tells the Bing API to get the image tiles for the map from HTTPS.

Why? you ask.

Well it seems that the API determines what protocol to use for JavaScript and other parts from the document.location.protocol, however when it comes to the map tiles it looks to a global setting that is set based on the parameter being passed.


Well we all make mistakes but at least I found a solution. And I hope that anyone else that runs into it will find this post. Enjoy!

Friday, July 31, 2009

ASP.NET MVC V2 Preview 1 Released

The Microsoft ASP.NET team just released the first public preview of ASP.NET MVC Version 2.  You can download it here

It includes support for:

  • Templated Helpers - allow you to automatically associate edit and display elements with data types. For example, a date picker UI element can be automatically rendered every time data of type System.DateTime is used. This is similar to Field Templates in ASP.NET Dynamic Data.
  • Areas - provide a means of dividing a large web application into multiple projects, each of which can be developed in relative isolation. This helps developers manage the complexity of building a large application by providing a way to group related controllers and views.
  • Support for Data Annotations - Data Annotations enable attaching validation logic in a central location via metadata attributes applied directly to a model class. First introduced in ASP.NET Dynamic Data, these attributes are now integrated into the default model binder and provide a metadata driven means to validating user input.

These are the big basics. However there are a number of other new teaser features in it. 

  • Default Parameter Values – Just what it says. The syntax is a little clunky but its a start in the write direction. In Visual Studio 2010 you’ll be able to do the same thing but with a less clunky syntax.
  • Binding Binary Data - ASP.NET MVC Preview 1 adds support for binding base64-encoded string values to properties of type byte[] and System.Data.Linq.Binary.  There are now two overloaded versions of Html.Hidden() that can take these data-types.  These can be useful for scenarios where you want to enable concurrency control within your application and want to roundtrip timestamp values of database rows within your forms. 

You can read more at Scott Gu’s post and Phil Haack’s MVC2 post

Tuesday, March 24, 2009

Scott Hanselman rated higher than Scott Gu

So I've been watching all the Keynotes from Mix 09. Mostly because I’m to busy and to poor to go to these types of things. Scott Hanselman gave this talk about Nerd Dinner, during the talk, well actually at the start of the talk he mentioned that if you searched on Scott, Scott Guthrie would come up as 5th and he would be at 11th place. Just having to know how true to the facts that is, and because posed with an unknown I just need to know the answer. I of course Googled it.

Hanselman actually came in 6th and Scott Guthrie came in 8th. How wacky is that. If I were to totally geek out I would say that Scott and Scott need to have a light saber battle to the death for Google rating supremacy. But I won’t do that.

UPDATE: Thinking about this more, I have to say how sick is it that doing a Google search on the name “Scott”, and considering the number of people named Scott in the world and how many times the word Scott must appear on the internet, that Scott Guthrie and Scott Hanselman are in the top 10 search results in Google. I am curious as to what methodology they use as far as SEO is concerned.

Monday, March 9, 2009

Code Contracts in C#

One of the new technologies Microsoft has coming down the pipeline for .NET 4 is Code Contracts. What are code contracts?

Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs. The contracts take the form of preconditions, post-conditions, and object invariants. Contracts act as checked documentation of your external and internal APIs. The contracts are used to improve testing via runtime checking, enable static contract verification, and documentation generation.

But what they are is so much more, they bring design by contract to .NET. Traditionally one would set up some type of argument checking to verify the validity of what was passed in, and probably return some type of invalid argument exception upon failure, like the following:

public class DummyObject
public DummyObject(int n)
if(0 < n)
throw new InvalidArgumentException("n must be greater than 0.");

This isn’t the most elegant of solutions, also it is only verified at runtime. With Code Contracts, you now have static checking of your code conditions to see if you violated the contract at compile time. As far as testability this is great. But even better is that we as developers can tailor our code, to make the compiler a little bit smarter. In the code above everything would compile just fine if we initialized a new instance of our “DummyObject” with a 0, only when we ran it would we see an error. Now if we were to rewrite this with code contracts it would look like this:

public class DummyObject
public DummyObject(int n)
Contract.Requires(0 < n);

Now with static checking turned on we would see an error at compile time telling us that we have violated the contract. This is one of the simplest examples there is a great deal more that can be done. I recommend reading the Microsoft research site and look for other articles out there.

Thursday, March 5, 2009

Microsoft MVC Digg Sample

So I recently was writing an MVC sample just to familiarize myself with the Microsoft MVC Framework. One of my favorite samples recently was one done by Scott Guthrie for Silver Light. It was a Digg sample so I wanted to make one that was similar to that one. So with no further adieu.

The source code can be downloaded here.

I will be updating it with more features as I build it out and will probably be doing posts to explain the different parts, so keep checking for new entries.

Tuesday, March 3, 2009

Syntax Highlighter

So I got an email this morning asking me about the code blocks in my posts.

Hey I noticed that your code you put in your posts has changed, can you tell me what your using to do that I really want it for my blog.

Basically I had received a number of emails stating that my code was impossible to copy and paste easily, so I started writing my own syntax highlighter and then I poked my eye out! Basically I realized that someone had already done this so why was I spending my time on it. And then I set out on my search through the search result jungle of Google.

After reviewing a ton of different methods I found one I liked the best Syntax Highlighter by Alex Gorbatchev. It’s actually very slick, it post processes the tags in your page and does the decoration after it loads via JavaScript. So I don’t have to worry about any real formatting and the like just wrap my code blocks in some simple tags and done.

Anyhow go check it out and donate to him he has done a lot of work and it is really quite good. 

Monday, March 2, 2009

Microsoft MVC Tip #1

Well here is my first tip for MVC, how nice that I actually hit a head scratcher.

So recently I was doing some Model binding in MVC, and couldn’t get the binding to work. I had a simple object:

public class SimpleObject 
public string TestHiddenField;
public SimpleObject
TestHiddenField = "2";

In my Controller I was assigning the instance of my SimpleObject to my Model:

public ActionResult Index() 
ViewData.Model = new SimpleObject();
return View();

And in my markup I had this:

<%= Html.Hidden("TestHiddenField") %> 

The markup that was produced was this:


Now notice the value is blank. The model should have bound the value “2” to this control and that should have been the value. But without knowing it I did something stupid. I have a public field but it seems that the Model Binder expects a property with a getter and a setter. Well at least a getter.

So to fix the problem I had to fix my SimpleObject:

public class SimpleObject 
public string TestHiddenField{ get; set; };
public SimpleObject
TestHiddenField = "2";

So in conclusion it seems that under the covers MVC Model Binder looks for properties and not fields on the object. This is interesting since I would think that it would look for both. Though it is a good practice to use properties rather than fields I just didn’t expect it.