Holy Smokes!

Sunday, April 26, 2009

Never Underestimate the power of Google

Ok, I guess we’ve all been there before.  Need to know something?  Where should I look?

Where should I ask… where else?  Google of course!

This actually has nothing to do with programming, though maybe a programming subculture.

I have a Wii, wanted to play the Wii, and crap, i was denied the service.  Couldn’t understand it.  The burger just wouldn't turn on.  Not power LED, no nothing.

As a professional analyst, I naturally went through the best diagnostic checklist I could come up with after 5 or so beers.

  • plugged in?
  • outlet giving juice?
  • too drunk to turn it on?

Nope, it’s messed.

So.. naturally, I considered my resolution options.

  • Tech Support.
  • Warranty
  • Attempt to return it at Walmart (even though it was bought elsewhere)

None of these options left me with dignity.  I had only one course of action, my old friend Google.

Turns out, you must unplug power source from outlet AND unit for 90 seconds.  Such an act causes an internal breaker to reset, its a safety mechanism.

This leaves me with a few ideas on a new thesis.

  • DRM (Drunk operation Retention Management)
  • maybe I shouldn’t plug the unit into an outlet controlled by a light switch?
  • this is obviously the type of user interaction I need to implement in my own products

Sorry for the rant.  I’m actually happy I didn’t have to resort to any of the above.  I just thought this bit of info would either be enjoyable to you, or possibly helpful should you ever find yourself 5 beers in, and wanting to play Mario Kart or something.

Night.

Friday, March 6, 2009

CLS Compliant Code

How important is it to make certain your .NET code is CLS Compliant?

For the uninformed, CLS, or Common Language Specification, refers to the claim that CLS rules and restrictions are being followed.  However, the concept has a more specific meaning depending on whether you are describing CLS-compliant code or CLS-Compliant development tools, such as a compiler.  CLS-compliant tools can help you write CLS-compliant code.

FTA Writing CLS-Compliant Code [msdn]

I have personally been taking modest steps to ensure that my code is being validated CLS compliant for some time.  I do this almost automatically.

In the real world however, how important is this really?

Technically speaking, I believe there are a lot of reasons you should be concerning yourself with this.  But realistically, is it really that important?

Say you’re one of the, I don't know, millions of developers out there working with Visual Basic.  And like most .NET developers, sooner or later you are going to consume a class library that another developer wrote.  What if the Class Library was written in another language?

Lets not forget, this is .NET, and the language used shouldn't really matter.  But let’s say the library was written in C#. and a class was defined like this.

public class TestClass : Object {
public void doSomething() {
return;
}

public void DoSomething() {
return;
}
}




Yeah, I know, this example is stupid.  But bear with me, this is an important topic.  Simplicity makes this easier to understand.



VB Developers, this is allowed in C#.  But how would VB understand this?  After all, VB is NOT case sensitive, and it would get confused.  So how could VB possibly consume this without some sort of ambiguity?



The answer is, it simply cannot.  This is where the CLS comes in.  While forcing a validation does not DO anything to your code, it will prevent you from doing ‘stupid’ things.  Lets tag up that code a little.



[CLSCompliant(true)]
public class TestClass : Object {
public void doSomething() {
return;
}

public void DoSomething() {
return;
}
}


And how does Visual Studio take this?


clscompliantfail 



Note this does not prevent the code from building, But it is telling you that the the class fails CLS compliance.  Meaning, this would probably not work against all languages.



You could however expect normal operation if it were consumed by a C# project.



This is really as hard as it gets to validate your code.



In order to use this validation in your own code, you need to be sure to mark the assembly as CLSCompliant.  This is normally done in the AssemblyInfo.cs (replace with .vb or whatever).  Note to mark an assembly, the attribute is a little different,



[assembly: System.CLSCompliant(true)]




You do not need to specify the System namespace, but the AssemblyInfo.cs does not reference the System namespace by default.  So, pick your poison, import the namespace, or specify the namespace.



So, this was just something I wanted to share, I personally find it useful, but I welcome your thoughts.



On one more side note, I would like to mention, that this validation is only really concerned with aspects of your code that can be consumed by other assemblies.  This means that if one or both of these two methods were private, it would pass validation, no problem-O.

Thursday, March 5, 2009

Just wasting everyone's time

It seems like I’ve been to busy or lazy to manage this blog lately.  Not intentional, just to lazy.  So I figured I would waist your time, and just share something meaningless.

Any of you have a ‘favorite’ new feature of C#? or .NET in general?  They pushed a fair amount into the last release.

Personally I think the smallest, least important language updates are my favorite ones.  Two of which:

Automatic Properties is one of them.  Take the following code.

public class TestClass : Object {   
private int m_intValue;

public int Value {
get {
return m_intValue;
}
set {
m_intValue = value;
}
}
}




using automatic properties, you could shorten that to this.








public class TestClass : Object {   
public int Value { get; set; }
}



The end result is a virtually identical IL.  So.. there is no performance hit, the code is cleaner and easier to read and maintain, and as an added bonus, it saves typing.



Another update I thoroughly enjoy is the new method of assigning values/references to object properties, upon initialization.  Take this example:




TestClass cls = new TestClass();   
cls.Value = 55;





You could replace that with this:



TestClass cls = new TestClass() { Value = 55 };





In that example, perhaps it doesn’t look like it’s doing much, and perhaps you’re right.  But you aren’t limited to assigning just one property at a time, you can assign as many as you like.  And it’s still just considered one line of code, even if you split it across multiple lines, so if you’re the type who doesn’t like to use braces excessively, this can really help keep the code trimmed down.




foreach (String strPath in Paths)    
trv.Nodes.Add( new TreeNode() {
Text = strPath,
Tag = new FileInfo(strPath)
});



Anyways, I just wanted to share.  What are you guys’ favorite new features?

Followers