August 2007 - Posts
Let me start out my saying that I simply love the writings of Joel Spolsky [more about him], CEO of FogCreek Software, and have been a follower of his well written and well thought out essays on software development and management on his Weblog "Joel on Software" (http://www.joelonsoftware.com).
He has also published a number of great books on building good software and good software companies, many of which already have a home on my bookshelf. One of his earlier articles he posted on his weblog was about hiring developers which subsequently lead to "Smart people who get things done" becoming a mantra with many hiring managers in the know when looking for developers; in fact the same can be said for hiring any staff.
This is a guy that doesn't mince his words when it comes to the things he is passionate about.
Here is an interview given with ACM Queue, "A conversation with Joel Spolsky", where he shares his tell-it-like-it-is insights on things like Software development, management, The Google culture etc. Once again, an awesome read!
Some thoughts on management;
"I always think of it [Management] as more of a support role - like moving furniture out of the way so they can get things done - than an actual leadership role...
To the extent that managers can actually make decisions for programmers, they are very low-level, mundane decisions that allow the programmer to move on to the next task. In terms of decisions about algorithms, about the big picture, structure, code, it's probably a very bad idea for management to make those decisions. I see two roles for a manager of developers: number one, keeping a useful and assorted queue of things for the developers to be working on, so that they can pick things off the top of the queue and do them next; and, second, basically being there to answer developers' questions."
Some thoughts on a concept called EBS (evidence-based scheduling) they are using in their product Fogbugz, a web-based project management system, to help software developers better predict release dates really
"In evidence-based scheduling, you come up with a schedule, and a bunch of people create estimates. And then, instead of adding up their estimates - instead of taking them on faith - you do a ... simulation where you look at what you had in the past and run a simulation of all of your futures. What you get, instead of a date, is a probability distribution curve that shows the probability that the product will ship on such-and-such a date."
"It is mathematically incorrect to add two estimates to get the estimate of those combined features. You have to do something like evidence-based scheduling, where you use historical data - and this is actually risk analysis."
This really struck home with me because accurate estimations is something I really struggle with.
Either this guy just knows how to push my buttons as a developer, or he really knows his stuff.
Why are there not more software development companies like this out there?
Dear Joel; pretty please with a heap of cherries (and sprinkles) on top can I come work for you? I'd make a fabulous dishwasher! :D
I was doing some research for a talk I am planning on IAD when I came across this article talking about whether or not it should be classified as a real addiction.
Quoted from this article is the following snippet ~
"Young, who treats people with Internet problems, is executive director of the Center for On-line Addiction (www.netaddiction.com). Hers is the first behavioral health-care firm to specialize in Internet-related disorders, offering online treatment."
Whilst this is becoming a serious problem all over the world, with reports of a person dying from too much online gaming in China, I found it rather ironic that a group would create an online community to help people suffering from being online too much. It's like hosting the local AA meeting at a pub during happy hour, or offering free membership to the nastiest porn sites out there to people who suffer from porn addiction.
Surely it would make more sense to setup a support group where suffers of IAD can meet and speak to real people face-to-face?
Or offer to mail them a book to read, or anything else but for heavens sake keep them away from the web.
I got asked the questions recently;
"What is the difference between the Is and the As operators in C#, when would you use each? Are they interchangeable? What are the performance implications of each?"
I had an idea but I didn’t know the exact answer to all of the above, and neither did some mates I ping'd so I thought I'd look into this in a bit more detail. The purpose of this post then is partly for my reference and partly to educate them (and those out there who are in their shoes too but embarrassed to admit it.
I will be using the following two interfaces in my examples, trivial yes, but they should suffice in helping me illustrate the differences and the usage of each operator.
interface IEncryptable
{
void Encrypt();
void Decrypt();
}
interface ICompressible
{
void Compress();
void Decompress();
}
Now that we have our two interfaces, let's create some classes to implement these interfaces. For the sake of keeping this post as brief as possible I will not flesh out each method. If you want the full source code that badly just ask and I'll post a link to the solution.
public class Document: IEncryptable
{
#region IEncryptable Members
public void Encrypt()
{
}
public void Decrypt()
{
}
#endregion
}
//Create a new specialized type of Document that is compressible
public class CompressibleDocument : Document, ICompressible
{
#region ICompressible Members
public void Compress()
{
}
public void Decompress()
{
}
#endregion
}
So, we have some demo classes (really intense and complicate, I know ....) now let's stop the waffle and finally get to the meat of this post.
I guess the question is....
How do you know what interface(s) a class implements at runtime? Well there are two methods.... yip, you guessed them... using "is" and "as".
But why would the good folk at Redmond provide two operators to do the same thing you ask, well they didn't. There is a subtle, but important, difference between the two and understanding this difference can lead to better performance; and we all want to perform better, don't we?
"Is":
Consider the following code ~
static void Main(string[] args)
{
//A collection of different types of documents
Document[] docs = new Document[2];
//First element is a regular Document
docs[0] = new Document();
//Second element is a special document, one that be compressed
docs[1] = new CompressibleDocument();
//Let's loop through our collection of documents
//Remember at this point we don't know whether we've got a Document
//that can be compressed or not, all we know is that we
//have a document of some form.
foreach (Document doc in docs)
{
//Only compressible documents will pass here
if (doc is ICompressible)
{
ICompressible compressibleDoc = (ICompressible)doc;
compressibleDoc.Compress();
}
}
}
Simply put this friendly chap, the "Is" operator offers a nice way to ask a class are you something else, or, can you do something because you implement a particular interface or derive from a particular class.
Let's dig into some MSIL code quickly to have a look at what is happening when we do the "Is" checks above.
|
IL_0035: isinst Is_Vs_As.ICompressible IL_003a: ldnull IL_003b: cgt.un IL_003d: ldc.i4.0 IL_003e: ceq IL_0040: stloc.s CS$4$0001 IL_0042: ldloc.s CS$4$0001 IL_0044: brtrue.s IL_0056 IL_0046: nop IL_0047: ldloc.0 IL_0048: castclass Is_Vs_As.ICompressible IL_004d: stloc.2 IL_004e: ldloc.2
IL_004f: callvirt instance void Is_Vs_As.ICompressible::Compress()
|
I have marked the important lines for this example in bold.
The keyword, on line 35, isinst is the MSIL code translation of the “Is” operator. The cast is done and the test is done on line 44 with the brtrue.s IL_0056 instruction. If the test passes the code will continue down to the next bold line, 48, where castclass is called.
As you will see when we get to the “As” operator in a bit the castclass operation also performs a check on the type before a cast is made, if the type is not valid null is returned from the cast operation. So we're actually checking the type twice.
In effect the code (doc is ICompressible) can be translated into ( (doc is as ICompressible)!= null) )
Now let's move on
“As":
Consider the following code ~
//Let's loop through our collection of documents
//Remember at this point we don't know whether we've got a Document
//that can be compressed or not, all we know is that we have a
//document of some form.
foreach (Document doc in docs)
{
//Only compressible documents will pass here
ICompressible compressibleDoc = doc as ICompressible;
if (compressibleDoc != null)
{
compressibleDoc.Compress();
}
}
Here we are using the As operator to do a direct cast to the ICompressible interface. If the type of the class we're trying to cast is not valid for this cast the As operation will return a null reference. So all we need to do is check for the null reference before attempting an operation that is specific to the ICompressible interface.
Let's have a look at what is happening this time around in the MSIL code
|
IL_0035: isinst Is_Vs_As.ICompressible IL_003a: stloc.2 IL_003b: ldloc.2 IL_003c: ldnull IL_003d: ceq IL_003f: stloc.s CS$4$0001 IL_0041: ldloc.s CS$4$0001 IL_0043: brtrue.s IL_004e IL_0045: nop IL_0046: ldloc.2 IL_0047: callvirt instance void Is_Vs_As.ICompressible::Compress()
|
Again I have marked the important bits in bold. You will notice when comparing to the MSIL code from the Is operator that there is a distinct lack of a castclass instruction.
Clearly more efficient than the Is operator from above.
|
As a side note, ICompressible compressibleDoc = (ICompressible) doc;
Will throw a InvalidCastException if the type of the class is invalid for the specific cast as opposed to the As operator that will simply return a null for invalid casts, making the As operator really powerful in situations like this .... much easier than trying to handle the possible InvalidCastException. |
So if the As operator is more efficient than the Is operator as we've shown above then why would they include both?
Well simply put if your design is to check the type and do a cast immediately, as done here and most often the case, then the As operator is more efficient.
However, if you simply want to test the type without casting it at all, as shown below, then the Is operator would be a better choice....
ArrayList someList = new ArrayList();
foreach (Document doc in docs)
{
//Only compressible documents will pass here
ICompressible compressibleDoc = doc as ICompressible;
if (compressibleDoc != null)
{
someList.Add(doc);
}
}
|
IL_0026: isinst Is_Vs_As.ICompressible IL_002b: ldnull IL_002c: cgt.un IL_002e: ldc.i4.0 IL_002f: ceq IL_0031: stloc.3 IL_0032: ldloc.3 IL_0033: brtrue.s IL_003f IL_0035: nop IL_0036: ldloc.0 IL_0037: ldloc.1 IL_0038: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
|
Ok so we know which is now more efficient and when to use which.... But how more effecient is the As operator against the Is when doing a check followed immediately by a cast?
Well let's check.
I used the classes above and created a few more, up to 100,000,000 [yes that is 100 MILLION], documents instead of 2. I then ran through each set of worker functions, one using the Is operator and the other using the As.
I added some timing to each and ran the tests a few times to try average out the results; below are my results. (Results will obviously be different depending on your machine etc.)
| Documents In Collection |
DoIs time in Ticks |
DoAs time in Ticks |
% Diff |
| 1,000 |
859 |
272 |
68.34 |
| 10,000 |
844 |
691 |
18.13 |
| 100,000 |
5,283 |
5,165 |
2.23 |
| 1,000,000 |
50,839 |
47,733 |
6.11 |
| 10,000,000 |
508,821 |
476,447 |
6.36 |
| 100,000,000 |
5,102,190 |
4,761,784 |
6.67 |
What I find very interesting is that in relation to the Is operator the As is quicker the smaller the collection; Anybody know why?
As you can see there is a difference but I mean really ... we’re talking ticks here;
So if 1 tick = 100 nanoseconds And 1 millisecond = 1,000,000 nanoseconds then are 10,000 ticks in a millisecond.
Therefore even with 100 MILLION ( *said in best Dr Evil voice* ) we’re looking at 0.51 seconds vs 0.47 seconds and a “massive” saving of 0.034 seconds!
So should we really split hairs over this?
I feel a little deflated now I must admit.
I know this probably old news to most of you .... but I only found it today, courtesy of Armand's post here, and think it rocks!
quoted from Symbian Tools, the developers of this little gem....
"Guardian is the new anti-theft system for Symbian Series 60 devices.
Every time you switch on your mobile telephone, Guardian proceeds with authentication of the inserted sim card; if authentication fails, Guardian sends a notification sms message to a previously set telephone number."
So simply put, if some scourge kindly relieves you of your phone, the next time the phone is booted up (switched on for those less inclined to understand IT speak) Guardian will check the new Sim card against a list of preconfigured cards, if it does not match it will notify you and proceed to switch off the phone....
Guardian is available for download ( currently free of charge, donations accepted though and for something this good they deserve donations ) here;
If you make a donation other functionality will be unlocked to you, like;
"It allows also to gain information about the thief, like his sms sent & received, calls made & received, and contacts added by the thief."
Styling if you happen to own one of the following phones ...
Nokia: 3230 / 6260 / 6600 / 6620 / 6630 / 6670 / 6680 / 6681 / 6682 / 7610 / n-Series
Samsung: SGH-D720 / SGH-D730
To those of us "lucky" enough to own a Microsoft Windows Mobile based phone like my brand spanking new Htc S710, well we're not so lucky ...
So, when next I suffer from insomnia guess what I shall be building as my first .NET CF application 
Then when Red Five Labs's release the final version of their runtime for the Symbian S60 platform you can run the same app on both MS and Symbian based phones.
All IMHO opinion of course, but I think this is completley and utterly insane.
Take a look at the commerical below and tell me if you think there is something wrong, insensitive, offensive or any of the other choice words used to describe it.
Yes it features multiple black men in a sprinting pose .... Yes it features a white man standing there with folded arms.
But is this racist? I mean really?
It simply projects sprinters as black men .... is this derogatory? NO! Take a look at the latest IAAF circuit and tell me how many 100m sprinters out there are white?
These sprinters are not bowing down to a white man .... they're not submitting to the oppression handed out them by thousands of years of imperialism .... it is no way saying we white men stand above you whilst you black man can grovel on the floor ....
In fact these guys are powerful machines and in pristine condition poised ready to explode with power and grace as opposed to the regular middle aged blue collar white man fading away in middle management!
Why is it that the proverbial race card is played so easily ?
As quoted from Intel:
"Intel's intent of our ad titled "Multiply Computing Performance and Maximize the Power of Your Employees" was to convey the performance capabilities of our processors through the visual metaphor of a sprinter. We have used the visual of sprinters in the past successfully."
I think Intel handled it well considering.... A brief apology, without groveling.
OK this post isn't really about this, so lemme back track a little and then you'll see where I am coming from with this.
I am the proud owner of Programming C# by Jesse Liberty (Great book on the whole). Whilst skimming through it this morning looking for something particular I noticed an interesting sidebar on page 73, Creating Objects, shown below for those who have not got the book on hand to flip to...
"VB programmers take note: while there is a performance penalty in using the VB keywords Dim and New on the same line, in C# this penalty is removed"
Huh? OK, I don't profess to be an expert in VB.NET but I was most interested in this. Never happy to simply take things at face value I thought I'd do a little digging and see what was going on under the hood, after all if there was some kind of performance issue in VB.NET then surely it would show up in IL? So off to my trusty friend ILDASM.EXE
This VB.NET code;
Sub Main()
Dim doc As New System.Xml.XmlDocument
End Sub
Produces the following IL;
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 9 (0x9)
.maxstack 1
.locals init ([0] class [System.Xml]System.Xml.XmlDocument x)
IL_0000: nop
IL_0001: newobj instance void [System.Xml]System.Xml.XmlDocument::.ctor()
IL_0006: stloc.0
IL_0007: nop
IL_0008: ret
} // end of method Module1::Main
And this C# code;
static void Main(string[] args)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
}
Produces the following IL code;
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 7 (0x7)
.maxstack 1
.locals init ([0] class [System.Xml]System.Xml.XmlDocument doc)
IL_0000: newobj instance void [System.Xml]System.Xml.XmlDocument::.ctor()
IL_0005: stloc.0
IL_0006: ret
} // end of method Class1::Main
So no rocket science here at all .... just creating a new instance of a XmlDocument to see why there would be a difference.
Any VB.NET expert wanna explain to me what Mr Liberty was referring to with his comments, I don't see it.All I see is a few "nop" statements in the VB.NET IL. Surely "nop" don't constitute a performance penalty, do they?
I went one step further and compared the IL for
Dim x as New System.Xml.XmlDocument
To
Dim x as System.Xml.XmlDocument = New System.Xml.XmlDocument
To
Dim x as System.Xml.XmlDocument
x = New System.Xml.XmlDocument
And in each of these three cases the IL produced was pretty much identical.
....
And now that we've gone through all of that i hear you screaming but what about C++. Quite observant, I have not mentioned it as yet. The only reason I included that in the title was because whilst I was digging around for the comparison between VB>NET and C# I came across an interesting post by Kate Gregory ( The American from Canada, :D private joke, guess you had to be there for that one .... ) entitled .NET Under the Hood: a Little ILDASM.
She does a similar thing but compares produced by C++, C# and VB.NET and comes to the conclusion that C# is in fact MORE like VB.NET than C++! At least in terms of what happens under the hood. So that puts to rest the argument that C# is better because it's more like C++ than VB. 
Hello Everyone, My Name is Ryan and I am an internet addict ....
I find i suffer from "information overload". there is just too much to cope with! Often I find myself starting out Googling for something relevant only to find myself reading obscure articles about the mating habits of omeboids on planet Krpyton (or something else equally arb ....)
Does this happen to you?
Do you struggle to actually get work done amongst these distractions?
Well it seems I am not alone in this .... Check out this article which has some clever tips on how to Cure Your Internet ADD (Attention Deficit Disorder)
Another great post from Scott Guthrie about installing and using the Linq to SQL Debug Visualizer ... a handy little addition.