BusinessRx Community

Dedicated to the advancement of software, technology and the people who devote their lives to it.

Welcome to BusinessRx Community Sign in | Join | Help
in Search

BusinessRx Reading List

These blog entries are written by industry experts and leaders. We consider this content to be a good read for any software developer or web technologist.

  • OUTPUTing Data from the Just-Inserted, Updated, or Deleted Row(s)

    Between work and diaper changes I've been reading Michael Coles's book Pro T-SQL 2008 Programmer's Guide, and found this little gem (pg. 527-528):

    The OUTPUT Clause
    You can use the OUTPUT clause with the the INSERT, UPDATE, DELETE and MERGE DML statements. ... The OUTPUT clause returns information about the rows affected by the DML statements that can be useful in comparing preupdate and postupdate data, or for troubleshooting and logging purposes. ... You can use the OUTPUT clause to output a SQL result set like that returned by a SELECT statement, or you can combine OUTPUT with the INTO clause to output rows to a table or a table variable.

    This feature is supported in T-SQL 2008, but was initially added to Microsoft SQL Server 2005. And here it is three years later and I'm just learning about it!

    One use of the OUTPUT clause is to grab the just-inserted IDENTITY column value:

    INSERT INTO TableName(ColumnList)
    OUTPUT inserted.IdentityColumnName
    VALUES(Values)

    The above will return the just-inserted IDENTITY value as a result set, just as if you had followed an OUTPUT-less INSERT statement with the statement:

    SELECT SCOPE_IDENTITY()

    You could use the OUTPUT statement to return information about the rows affected by an UPDATE. For example, in these tough economic times you might need to increase prices by 20% for all products that cost less than $10.00. The following statement performs the described update and returns those products whose prices were increased, showing both their old price and their new price:

    UPDATE Products SET
        Price = Price * 1.20
    OUTPUT inserted.ProductID, deleted.Price AS OldPrice, inserted.Price AS NewPrice
    WHERE Price < 10.00

    This UPDATE statement will modify the data and return a three-column result set with a row for each modified product along with its preupdate price (deleted.Price) and its post-update price (inserted.Price).

    Pretty neat, eh?

    In fact, You can string these DML statements together, so you do an UPDATE with an OUTPUT whose results are then automatically INSERTed into another table (such as an audit table). The OUTPUT statement feels like in-place triggers (kind of like how Common Table Expressions are akin to in-place views).

    I'll have to write an article on the OUTPUT statement on 4Guys one of these days...

    Further Reading....

  • October 10th Links: ASP.NET, ASP.NET AJAX, jQuery, IIS

    Here is the latest in my link-listing series.  Also check out my ASP.NET Tips, Tricks and Tutorials page and Silverlight Tutorials page for links to popular articles I've done myself in the past.

    ASP.NET

    • ASP.NET Dynamic Data Videos using VB: Bill Burrows has put together an awesome series of videos that show off how to use the new ASP.NET Dynamic Data support provided in .NET 3.5 SP1.  You can find more links to ASP.NET Dynamic Data tutorials in my last link post here.

    • Routing with WebForms: Wally McClure has a nice podcast that describes how to use the new ASP.NET routing infrastructure in .NET 3.5 SP1 with Web Forms based pages.  A lot of people mistakenly think this feature only works with ASP.NET MVC applications - when in reality it also works with web forms pages (in fact all ASP.NET Dynamic Data sites use it).

    ASP.NET AJAX and jQuery

    • An Introduction to jQuery (Part 1): Rick Strahl has posted an excellent article that introduces jQuery, and walks-through how to take advantage of it within ASP.NET pages.

    • New AJAX Support for Data-Driven Web Apps: Bertrand Le Roy has written a great MSDN article that describes some of the new ASP.NET AJAX features available in preview form today.  Also check out his blog posts here and here to learn more about how the new client-side data templating feature support.

    • ASP.NET AJAX: Enabling Bookmarking and the Browser's Back Button: Scott Mitchell continues his excellent series on ASP.NET AJAX and discusses how to add history points to an AJAX-enabled web page so that visitors can bookmark it, as well as to enable back/forward browser navigation.  This is a new feature added to ASP.NET in .NET 3.5 SP1.

    Microsoft Web Platform

    Hope this helps,

    Scott

  • Going Independent: When Do You Bill Your Clients and What Are The Payment Terms?

    A decision all independent software developers must make early on in their career is when to bill a client and under what terms. This decision, of course, is not the developer's alone to make - it must be discussed and agreed upon by the client, as well. Similarly, a developer must decide how to charge: by hour or by project. Over the years I have received numerous e-mails from fellow developers who are contemplating striking it out on their own, and wonder what suggestions or advice I have for billing.

    My general policy is as follows:

    Software Development:
    For software development I charge per hour. I have never taken on a client and billed per project, and likely never will. My primary concern with billing per project is that my estimation or the client's estimation may be off base, which hurts one of us. Also, when billing by the hour there is no concern about scope creep or other things tacked on by the client late in the game in an attempt to get more bang for his buck. Billing per hour is ideal for the software developer, but customers often prefer a fixed price for the project because it puts a cap on the amount it will cost. A new customer does not know what kind of work they can expect from a developer per unit time. To ameliorate this concern, I usually propose a very simple, short feature or milestone for new clients, so that they can get a good gauge of how long it takes me to complete a task, and what level of quality they can expect. I have also given price caps for customers on tight budgets, but this is more the exception than the rule.

    Another reason that I propose a very short and simple first project for new clients is because I require payment in full and up-front for new customers. This sours some customers and I've certainly lost some business because of this policy. The way I see it, this requirement serves as a vetting process. It shakes out customers who may have shaky cash flow or who are going to later nickel and dime me for the work performed or, worst yet, not pay at all. Once I've had some time to work with a client and have established a good working relationship, I will relax these conditions and bill on a monthly basis (or more often, if requested). If I have not yet established a relationship and do not fully trust the client, I continue requiring payment up front and in full. When I finally end up billing on a monthly basis, my terms are Net 15 (that is, I require payment within 15 days of invoice), but I have some customers who much prefer Net 30 and once that relationship and trust is established I'm willing to agree to those terms.

    I find this process of establishing trust by requiring payment up front at first, and then billing monthly with Net 15 terms to be a surefire approach to ending up with solid clients. To ensure prompt payment, some independent software developers use tactics like hosting the application on their server and not sending the code to the client until payment is received in full. Another, more questionable approach, is to put a backdoor into the application so that if the client does not pay the software developer can "break into" the application and "turn it off" until payment is received. I dislike both of these techniques as it frames the working relationship in adversarial terms.

    I have waived or relaxed these requirements - payment up front for new clients, Net 15 terms, etc. - for very large or very established customers, such as regional businesses with thousands of employees, or Fortune 500 companies. These types of clients are more the exception than the rule, though, as the vast majority of my clientele are small businesses. And some companies or industries have Net 60 terms and won't make any exceptions.

    Training
    Like with software development, I require payment up front or on delivery of services for new clients. I will make exceptions for very large and established clients, but only sparingly. My hesitancy here in due in part to a negative experience with a local training company that was very late with payment. I ended up taking the client to small claims court, but was paid in between the time they were served and when the court date was set. (My decision to go to small claims court and the resulting process is summarized in My Small Claims Court Experience.)

    While I always charge by the hour for software development, I charged for training services using a variety of models. For personal training I usually charge hourly, as it makes the most sense. In this way the customer has direct control over the costs and knows the precise charge for another hour or day's worth of training. For classroom events I usually charge a per-day rate, which depends on a number of factors, including:

    • Class size
    • Number of days of instruction
    • The location relative to my home - is the classroom setting five miles away, or across the country?
    • The material used and the topics covered - am I given the course-wear to teach or do I need to create it?

    When a potential customer asks for a training quote, I use the above criteria to judge how long it will take me to create the course-wear (if needed) and to prepare for the class, how much travel time there will be (if any) and the total hours of instruction. I then multiple that number by my usual hourly rate for software development, then multiply that by a number between 1.0 and 2.0. This coefficient depends on the size of the class (the more students, the higher the number) and based on how excited I am to teach the class. If I'm teaching a two-day class to 5 smart devs who work in a fun company that's headquartered on the beach, and we're having a luau at the end of the second day, then that number will be a lot closer to 1.0 than if it's a class of 25 devs at a stuffy corporate office and I have to wear a suit each day. The figure derived from that equation, plus any travel and entertainment costs, are what I quote the client.

    Writing
    Unlike training and software development, it's hard to get a client to pay you by the hour for an article, tutorial, or book. Also, in my experience, it's rare to be paid by the word. Usually publishers have a pre-determined rate they pay for a written piece with a minimum word count. For print media - books and magazines - there is also a maximum word count. The pre-determined rate for magazines and websites is usually a dollar amount; for books it's a royalty rate and advance. (For a breakdown of compensation on writing a book, see The Economics of Writing a Computer Trade Book.)

    Also unlike training and software development, it's virtually impossible to get a client to pay you anything up front. Book publishers offer advances, sure, but they don't start rolling in until you have completed a significant portion of the work. (This is true for computer trade authors; I'm sure Stephen King gets his advance whenever he wants.) As a result, as a write you have to be willing to get paid after the article or tutorial or book is written and published. As a result, I take a bit of a gamble if I write for a small publisher, small magazine, or small website. Having been bitten in the past, these days I rarely write for small operations unless I know the people running them and trust that I'll be compensated. But if a small publisher contacts me out of the blue and wants me to write five chapters for a book, I'll pass.

    In Summary...
    What's infinitely more important than the decision you make as when to you bill your clients and what terms to use, is to have very frank and clear communication before any work begins. It is imperative that you discuss with your client when you will bill and when you expect payment. They may agree, or they may want to change the terms. But what's vital is that there is a clear understanding. I've talked with many independent software developers who were too uncomfortable bringing the subject up or just assumed that the client understood and agreed with implicit payment terms, and as a result there was no communication prior to the work. This can lead to an unhappy client if his expectations of the payment terms were different than yours.

    Also, if you have sent an invoice to a client and he has not paid by the time the invoice is due, do not hesitate to contact the client and politely inquire about payment. And, lastly, if a client becomes severely overdue, do not hesitate to stop working for him. You are not obligated to work for free.

  • October 2nd Links: ASP.NET, ASP.NET MVC, ASP.NET Dynamic Data

    Here is the latest in my link-listing series.  Also check out my ASP.NET Tips, Tricks and Tutorials page and Silverlight Tutorials page for links to popular articles I've done myself in the past.

    ASP.NET

    • Amazon EC2 Support for Windows and ASP.NET: Big news announced this week: Amazon will be offering Windows Server 2008 as an option in their EC2 service.  This enables you to use ASP.NET, IIS7 and SQL Server in the cloud.

    • Using ASP.NET WebForms, MVC and Dynamic Data in a Single Application: Scott Hanselman has a nice post that demonstrates how you can have a single ASP.NET application that uses ASP.NET WebForms, MVC, WebServices and Dynamic Data.  You have the flexibility to mix and match them however you want, which allows you to always use the right tool depending on the specific job.

    • Unlocking and Approving User Accounts: Scott Mitchell posts another in his great series of articles on ASP.NET security (click here for all the articles in the series).  This article talks about how you can setup administration pages that allow admins to lock out and approve user accounts using the ASP.NET Membership system.

    ASP.NET MVC

    • MVC Membership with Preview 5: Troy Goode posts an update of his popular MVC Membership template that works with ASP.NET MVC Preview 5.  It provides a set of administration pages you can use for user/role management, as well as adds support for OpenID and Windows LiveID.

    • MVC Flickr Xplorer: Mehfuz Hossain has a cool ASP.NET MVC sample application posted that enables a nice picture explorer for FlickR photos.

    ASP.NET Dynamic Data

    • Simple 5 Table Northwind Example: Matt Berseth kicks off his ASP.NET Dynamic Data tutorial series with a nice post that shows how to build a simple 5 table application using ASP.NET Dynamic Data with .NET 3.5 SP1.

    Hope this helps,

    Scott

  • Lazy LINQ and Enumerable Objects

    Someone asked me why LINQ operators return an IEnumerable<T> instead of something more useful, like a List<T>. In other words, in the following code:

    List<Book> books = new List<Book>();
    // ...
    IEnumerable<Book> filteredBooks = 
        books.Where(book => book.Title.StartsWith("R"));

    ... we started with a List<Book>, so why isn’t the Where operator smart enough to return a new List<Book>, or modify the existing list by removing books that don’t match the Where condition?

    Let’s talk about modifying the original list.

    I hope you’ll agree that it would be odd for a query to modify a data source. Imagine sending a SELECT statement to a database and finding out later your SELECT removed all but one record from a table. Although the Where operator is just a method call that could change the underlying list of books, it’s better to return something new and leave the original list intact. You won’t find any LINQ operators that modify input , and this behavior produces many benefits. One obvious benefit is that you can think of the above code as a query, and it won’t surprise you by removing books from your original list.

    What about creating a new List<T>?

    It turns out that creating a new list can be quite expensive, but only because we often don’t need a List<T> returned. Think about the number of lists created in the following query (if each operator created a new list).

    var filteredBooks =
            books.Where(book => book.Title.StartsWith("R"))
                 .OrderBy(book => book.Published.Year)
                 .Select(book => book.Title);

    Here we would have three lists created (one each by the Where, OrderBy, and Select operators). We’d only needed a single list of titles as the result, but since the operators cannot modify their underlying data source (for the reasons outlined above), they would be forced to each create a new list, and most of that work would be wasted as two of the lists are immediately discarded.

    Imagine if you only needed to count the number of books whose title starts with the letter R – in that case you wouldn’t want any of these lists being created and destroyed when you computed the result. It would all be wasted work.

    Laziness Isn’t Always Bad

    One of the principles of LINQ is to be lazy. A LINQ query won’t do any work unless you force the query to do the work. Even when a query does perform work – it does the least amount of work possible. If you wanted a List<Book> as a result, you’d have to force LINQ to create the list:

    List<Book> filteredBooks =
                   books.Where(book => book.Title.StartsWith("R"))
                        .ToList();

     

    Instead of lists, LINQ works with a beautifully pure abstraction called IEnumerable<T>. It’s defined like so:

    public interface IEnumerable<T> : IEnumerable
    {
        IEnumerator<T> GetEnumerator();
    }

    The only thing you can do with an IEnumerable<T> is ask for an enumerator. An enumerator is something that knows how to visit each item in a collection. Some languages call these enumerator things “iterators”, because they iterate over a collection of objects, returning each object and moving to the next.

    The in-memory LINQ operators, like Where, OrderBy, and Select, all work on inputs that implement IEnumerable<T>. That means the operators work on anything that can be enumerated over in a one-by-one fashion. The beauty is that there are so many data sources that these operators can work on, because IEnumerable<T> has such simple demands. Arrays are enumerable, lists are enumerable, dictionaries, trees, stacks, queues, files in a directory, elements in an XML document - all enumerable. Even a simple string is enumerable, since it is composed from a sequence of individual characters.

    These same operators return IEnumerable<T>, because it’s the lowest common denominator for everything you’d ever need from a query. Plus, it’s lazy. You want to count the results? The Count operator will get the enumerator and move through the results, one by one, to sum the total number of items. You want to make a concrete list from the results? The ToList operator will get the enumerator and move through the results, one by one, adding each item to a new list it creates. Do you want just the first item in the results? Then the enumerator does just a little bit of work to find that first item. In most cases it does not need to iterate the entire collection to find the first item. Enumerators are lazy, too.

    The important point is that the enumerator itself doesn’t perform any useful work. It’s you, or the other LINQ operators, that use the enumerator to iterate through the result and produce something meaningful. In the odd case that you never need to look at the result - no enumeration work is performed at all. No lists area created. Pure laziness!

    Summary

    The beauty of IEnumerable<T> is that it only says “you can get something to enumerate this”. To return something that offers the possibility of enumeration is very little work. And no work is needed unless you actually count the results, create a list from the results, or bind the results to a control for display.

    The interface IEnumerable<T> is so wonderfully lazy it inspired me to write a short, short story. If you came to read about LINQ, skip the story as the words are entirely uninteresting and mostly devoid of meaning. 

    The Lazy Leopard and the List

    lazy leopardThe scientist approached the big cat with a notepad and a pencil in her hands. She was worried, of course. The cat was a predator, and likely to be hungry at this hour of the day. “I need to know”, she asked the cat, “do you keep a list of things to do each day?”

    The cat stirred. He was a snow leopard with dark rosettes blotted onto his thick, cream colored fur. The big cat’s eyes were only half open, but he turned and focused them on her.

    “I don’t keep a list, dear lady”, he said, followed by a rumbling yawn. “I keep an enumeration”.

    “An enumeration?”, she asked.

    “Yes, an enumeration”, he replied. “Lists are like the gold bracelet on your wrist, dear lady. Very tangible – very concrete things, lists are. Keeping a list of everything I might want to do is a burden and chore. I’d need to carry your paper, and your pencil”, he said, with his eyes focusing on her hands.

    The scientist’s pencil raced across her notebook as she transcribed every word the leopard spoke. She glanced at him as he began to stare, and instinctively pulled herself a little further away.

    The leopard continued. “With a list I’d have to add things, and remove things, and constantly reorder the things I want to do. Too much work”, he said, shaking his head. “Do you know what I can do with an enumeration?”, he asked.

    She paused at the leopard’s question, and pushed her hair back  - she wore glasses when she worked. After some thought, she asked, “Enumerate it?”

    “Yes, dear lady”, said the leopard. “I can enumerate it. I enumerate the possibilities one by one, and find the perfect fit for this moment in my life. If I’m thirsty, I’ll find water. If I’m sleepy, I’ll find a place to sleep.” He tilted his head slightly to the right. “If I’m hungry, I’ll find food”, he said.

    She finished writing the leopard’s last words and glanced up. Was that a tooth showing? Was he hungry now?

    The cat started speaking again.

    “One of the wonderful things about enumerations is they theoretically last forever. Lists have a beginning and an end – an Omega for every Alpha. With enumerations, you can keep asking for the next thing, over and over and over again. I ask for them when I’m ready to do something. If I’m tired of doing, they’ll still be there tomorrow. You might say it’s unpredictable behavior, I say I’m just being lazy. Either way, I can’t help it, it’s in my genes”. His soft voice trailed off with a tired tone.

    “You intended to live forever?”, she asked. The leopard snarled. Or smiled. She couldn’t quite tell.

    “No, dear lady”, he said. “I said the enumeration theoretically lasts forever. One day I’m sure my enumeration will run out of things to give me, or maybe I’ll just be too tired to ask for the next thing, so I’ll sleep forever. I don’t know how it ends. Maybe I should ask you.”

    She looked at him again. She felt uneasy now, being here with a leopard. He seemed nice enough, as leopards go, and he certainly gave her interesting topics for research, but he was still a leopard. A carnivore. He was not a beast to be trifled with. She could never let her guard down again.

    “I don’t know how it ends either”, she said, and closed her notepad. She tucked her pencil behind her ear, backed away from the cage, and left the leopard alone with his enumeration.

  • jQuery and Microsoft

    jQuery is a lightweight open source JavaScript library (only 15kb in size) that in a relatively short span of time has become one of the most popular libraries on the web.

    A big part of the appeal of jQuery is that it allows you to elegantly (and efficiently) find and manipulate HTML elements with minimum lines of code.  jQuery supports this via a nice "selector" API that allows developers to query for HTML elements, and then apply "commands" to them.  One of the characteristics of jQuery commands is that they can be "chained" together - so that the result of one command can feed into another.  jQuery also includes a built-in set of animation APIs that can be used as commands.  The combination allows you to do some really cool things with only a few keystrokes.

    For example, the below JavaScript uses jQuery to find all <div> elements within a page that have a CSS class of "product", and then animate them to slowly disappear:

    As another example, the JavaScript below uses jQuery to find a specific <table> on the page with an id of "datagrid1", then retrieves every other <tr> row within the datagrid, and sets those <tr> elements to have a CSS class of "even" - which could be used to alternate the background color of each row:

    [Note: both of these samples were adapted from code snippets in the excellent jQuery in Action book]

    Providing the ability to perform selection and animation operations like above is something that a lot of developers have asked us to add to ASP.NET AJAX, and this support was something we listed as a proposed feature in the ASP.NET AJAX Roadmap we published a few months ago.  As the team started to investigate building it, though, they quickly realized that the jQuery support for these scenarios is already excellent, and that there is a huge ecosystem and community built up around it already.  The jQuery library also works well on the same page with ASP.NET AJAX and the ASP.NET AJAX Control Toolkit.

    Rather than duplicate functionality, we thought, wouldn't it be great to just use jQuery as-is, and add it as a standard, supported, library in VS/ASP.NET, and then focus our energy building new features that took advantage of it?  We sent mail the jQuery team to gauge their interest in this, and quickly heard back that they thought that it sounded like an interesting idea too.

    Supporting jQuery

    I'm excited today to announce that Microsoft will be shipping jQuery with Visual Studio going forward.  We will distribute the jQuery JavaScript library as-is, and will not be forking or changing the source from the main jQuery branch.  The files will continue to use and ship under the existing jQuery MIT license.

    We will also distribute intellisense-annotated versions that provide great Visual Studio intellisense and help-integration at design-time.  For example:

    and with a chained command:

    The jQuery intellisense annotation support will be available as a free web-download in a few weeks (and will work great with VS 2008 SP1 and the free Visual Web Developer 2008 Express SP1).  The new ASP.NET MVC download will also distribute it, and add the jQuery library by default to all new projects.

    We will also extend Microsoft product support to jQuery beginning later this year, which will enable developers and enterprises to call and open jQuery support cases 24x7 with Microsoft PSS.

    Going forward we'll use jQuery as one of the libraries used to implement higher-level controls in the ASP.NET AJAX Control Toolkit, as well as to implement new Ajax server-side helper methods for ASP.NET MVC.  New features we add to ASP.NET AJAX (like the new client template support) will be designed to integrate nicely with jQuery as well. 

    We also plan to contribute tests, bug fixes, and patches back to the jQuery open source project.  These will all go through the standard jQuery patch review process.

    Summary

    We are really excited to be able to partner with the jQuery team on this.  jQuery is a fantastic library, and something we think can really benefit ASP.NET and ASP.NET AJAX developers.  We are looking forward to having it work great with Visual Studio and ASP.NET, and to help bring it to an even larger set of developers.

    For more details on today's announcement, please check out John Resig's post on the jQuery team blog.  Scott Hanselman is also about to post a nice tutorial that shows off integrating jQuery with ASP.NET AJAX (including the new client templating engine) as well as ADO.NET Data Services (which shipped in .NET 3.5 SP1 and was previously code-named "Astoria").

    Hope this helps,

    Scott

  • A Twist on the Monty Hall Problem

    Steve Smith has posted a couple of interesting probability problem solvers on his blog recently. His most recent one is a twist on the Monty Hall problem. I mentioned the Monty Hall problem and its solution in an earlier blog entry of mine, For Some Probability Problems, Seeing Can Be Believing:

    When facing a particularly tough probabilty problem sometimes it helps to sit down and draw out the possible outcomes for the given scenarios. In doing so, things that may otherwise seem very complex boil down to a very apparent solution or explanation.

    A great example of simplicity through diagraming is the Monty Hall problem, which goes like this: You are a contestant on the TV game show, Let's Make a Deal and are presented with three doors. Behind one door there is a luxury car. Behind the other two doors there is a poster of a car. The game show host asks you to pick a door. After choosing one, the game show host reveals one of the other doors that has a poster behind it and then asks you if you want to change your door choice to the other unopened door. What is the best strategy?”

    The answer is that you should change the door you had selected, because there's a 2/3 probability that the door you did not select houses the luxury car. In other words, there is a 2/3 probability that the door you picked has the remaining poster. The solution seems backwards. It seems paradoxical that the door the host reveals to have a poster of a car could have any influence on your initial selection. But if you sit down and draw out the various probabilities you can see that there's there are more good outcomes if you make the switch than if you hold fast to your initial choice. The following diagram was created by Rick Block; here, instead of a poster of a car you get a goat. Same difference.

    Steve's most recent puzzle asks what would happen if instead of a single contestent in the Monty Hall problem there were multiple participants:

    I take 3 plain envelopes and put a $100 bill inside one of them, seal them, and give one to you, one to Bob, and one to Carrie, at random.  Then I randomly ask one of you to open an envelope - for the sake of argument let's say I choose Carrie.  Carrie opens her envelope to reveal that it is empty.  Now I offer you the choice to trade envelopes with Bob - should you trade? And now I ask Bob the exact same question.  Should he trade?

    As written, Steve's problem isn't that interesting. If he is randomly choosing one envelope to open then there is no advantage to be gained by changing or not changing your envelope. Now, if Steve were to reword the problem as follows (as I believe was his intention), then things are a bit more interesting:

    I take 3 plain envelopes and put a $100 bill inside one of them. I then let you choose one, and then give the other two to Bob and Carrie.  I then open one of the envelopes (Carrie's or Bob's) knowing that it is empty. Now I offer you the choice to trade envelopes with Bob - should you trade? And now I ask Bob the exact same question.  Should he trade?

    There is a big difference between these two questions. In the first one, everything is random; in the second one, the host (Steve Smith) knowingly opens one of the envelopes that is empty. It's also important that the host is forced to open Bob or Carrie's envelope, and cannot attempt to open yours.

    The answer to this problem can be worked out the same way as the traditional Monty Hall problem - by putting pen to paper and drawing out the probability tree, although I used the name Cal instead of Carrie. (See the full size image...) 

    As you can see, your odds are maximized if you get your hands on your contestant's envelope, while your opponent's best bet is to keep his envelope. Therefore, whoever gets to make the final change is the person with the best odds.

    Another tool for determining how a probability puzzle works out is through empirical means. You can oftentimes write a terse program that simulates the problem at hand thousands of times and tallies up the various outcomes. I created such a program in C#.

        1 for (int iter = 0; iter < ITERATIONS; iter++)

        2 {

        3     int moneyPos = rnd.Next(3);

        4     int bucketChoice = 2;

        5     int openDoor = rnd.Next(2);

        6     if (moneyPos == openDoor)

        7         openDoor = 1 - openDoor;

        8     int oponentsPos = 1 - openDoor;

        9 

       10     bool makeSwap = rnd.Next(2) % 2 == 0;

       11     bool otherContestantMakesSwap = rnd.Next(2) % 2 == 0;

       12 

       13     if (makeSwap)

       14     {

       15         tmp = bucketChoice;

       16         bucketChoice = oponentsPos;

       17         oponentsPos = tmp;

       18     }

       19 

       20     if (otherContestantMakesSwap)

       21     {

       22         tmp = bucketChoice;

       23         bucketChoice = oponentsPos;

       24         oponentsPos = tmp;

       25     }

       26 

       27     // See if we won

       28     if (bucketChoice == moneyPos)

       29     {

       30         if (makeSwap && otherContestantMakesSwap)

       31             numberOfWinningsWhenSwappingAndContestantSwaps++;

       32         else if (makeSwap && !otherContestantMakesSwap)

       33             numberOfWinningsWhenSwappingAndContestantStays++;

       34         else if (!makeSwap && otherContestantMakesSwap)

       35             numberOfWinningsWhenStayingAndContesstantSwaps++;

       36         else

       37             numberOfWinningsWhenStayingAndContesstantStays++;

       38     }

       39     else if (moneyPos == oponentsPos)

       40     {

       41         if (makeSwap && otherContestantMakesSwap)

       42             numberOfContestantWinsWhenYouSwapAndContestantSwaps++;

       43         else if (makeSwap && !otherContestantMakesSwap)

       44             numberOfContestantWinsWhenYouSwapAndContestantStays++;

       45         else if (!makeSwap && otherContestantMakesSwap)

       46             numberOfContestantWinsWhenYouStayAndContestantSwaps++;

       47         else

       48             numberOfContestantWinsWhenYouStayAndContestantStays++;

       49     }

       50 }

       51 

       52 // Summarize results

       53 Console.WriteLine("Percentage of Wins When Swapping and Contestant Swapping: {0}", Convert.ToDecimal(numberOfWinningsWhenSwappingAndContestantSwaps) / Convert.ToDecimal(ITERATIONS));

       54 Console.WriteLine("Percentage of Wins When Swapping and Contestant Stays: {0}", Convert.ToDecimal(numberOfWinningsWhenSwappingAndContestantStays) / Convert.ToDecimal(ITERATIONS));

       55 Console.WriteLine("Percentage of Wins When Staying and Contestant Swapping: {0}", Convert.ToDecimal(numberOfWinningsWhenStayingAndContesstantSwaps) / Convert.ToDecimal(ITERATIONS));

       56 Console.WriteLine("Percentage of Wins When Staying and Contestant Stays: {0}", Convert.ToDecimal(numberOfWinningsWhenStayingAndContesstantStays) / Convert.ToDecimal(ITERATIONS));

       57 Console.WriteLine("");

       58 Console.WriteLine("Percentage of Contestant Wins When Swapping and Contestant Swapping: {0}", Convert.ToDecimal(numberOfContestantWinsWhenYouSwapAndContestantSwaps) / Convert.ToDecimal(ITERATIONS));

       59 Console.WriteLine("Percentage of Contestant Wins When Swapping and Contestant Stays: {0}", Convert.ToDecimal(numberOfContestantWinsWhenYouSwapAndContestantStays) / Convert.ToDecimal(ITERATIONS));

       60 Console.WriteLine("Percentage of Contestant Wins When Staying and Contestant Swapping: {0}", Convert.ToDecimal(numberOfContestantWinsWhenYouStayAndContestantSwaps) / Convert.ToDecimal(ITERATIONS));

       61 Console.WriteLine("Percentage of Contestant Wins When Staying and Contestant Stays: {0}", Convert.ToDecimal(numberOfContestantWinsWhenYouStayAndContestantStays) / Convert.ToDecimal(ITERATIONS));

    I set ITERATIONS to 50,000 and let 'er rip. The following output confirmed my initial assumptions:

    Percentage of Wins When Swapping and Contestant Swapping: 0.08266
    Percentage of Wins When Swapping and Contestant Stays: 0.16618
    Percentage of Wins When Staying and Contestant Swapping: 0.16482
    Percentage of Wins When Staying and Contestant Stays: 0.08318

    Percentage of Contestant Wins When Swapping and Contestant Swapping: 0.16754
    Percentage of Contestant Wins When Swapping and Contestant Stays: 0.08386
    Percentage of Contestant Wins When Staying and Contestant Swapping: 0.08262
    Percentage of Contestant Wins When Staying and Contestant Stays: 0.16914

    As you can see, your chances of winning are best when you swap and your contestant stays or when you stay and your contestant swaps - namely, when you get your hands on your contestant's envelope. Your contestant, however, does best when he hangs onto his envelope, either by having you both stay or both change.

  • In Memoriam to My Nephew, Matthew O'Bryant

     My nephew, Matthew O'Bryant, was one of the two US military personnel that was killed in the terrorist bombing of the Pakistan Marriott last weekend.  He was a great kid (22 years old) and he will be missed very much by his wife, brother and sisters, parents, the rest of our family, and his many friends.

    http://www.wkrg.com/local/article/local_sailor_killed_in_pakistan_bombing/18566/
    http://www.myfoxgulfcoast.com/myfox/pages/News/Detail?contentId=7499322&version=2&locale=EN-US&layoutCode=TSTY&pageId=3.2.1
    http://www.al.com/news/press-register/metro.ssf?/base/news/1222247795109040.xml&coll=3

     

  • Silverlight 2 Release Candidate Now Available

    This evening we published the first public release candidate of Silverlight 2.

    There are still a small handful of bugs fixes that we plan to make before we finally ship.  We are releasing today's build, though, so that developers can start to update their existing Silverlight Beta2 applications so that they'll work the day the final release ships, as well as to enable developers to report any last minute showstopper issues that we haven't found internally (please report any of these on the www.silverlight.net forums).

    Important: We are releasing only the Silverlight Developer Runtime edition (as well as the VS and Blend tools to support it) today, and are not releasing the regular end-user edition of Silverlight.  This is because we want to give existing developers a short amount of time to update their applications to work with the final Silverlight 2 APIs before sites are allowed to go live with it.  There are some breaking changes between Beta2 and this RC, and we want to make sure that existing sites can update to the final release quickly once the final release is out.  As such, you can only use the RC for development right now - you can't go live with the new APIs until the final release is shipped (which will be soon though).

    You can download today's Silverlight Release Candidate and accompanying VS and Blend support for it here.  Note that Expression Blend support for Silverlight 2 is now provided using Blend 2.0 SP1.  You will need to install Blend 2.0 before applying the SP1 service pack that adds Silverlight 2 support.  If you don't already have Blend 2.0 installed you can download a free trial of it here.

    Beta2->RC API Updates

    Today's release candidate includes a ton of bug fix and some significant performance optimization work.

    Today's release candidate also includes a number of final API tweaks designed to fix differences between Silverlight and the full .NET Framework.  Most of these changes are relatively small (order of parameters, renames of methods/properties, movement of types across namespaces, etc) although there are a number of them.  You can read this blog post and download this document to get a listing of the known API breaking changes made from the Beta2 release. 

    We have updated the styles of the controls shipped with Silverlight, and have also modified some of the state groups and control template names they use.  When upgrading from Beta2 you might find it useful to temporarily remove any custom style templates you've defined, and get your application functionality working using the RC first - and then after that works add back in the styles one style definition at a time to catch any rename/behavior change issues with them.

    If you find yourself stuck with an question/issue moving from Beta2 to the RC, please report it on the www.silverlight.net forums (Silverlight team members will be on there helping folks).  If after a day or two you aren't getting an answer please send me email (scottgu@microsoft.com) and I can help or connect you with someone who knows the answer.

    New Controls

    Today's release candidate includes a bunch of feature additions and tweaks across Silverlight 2, as well as in the VS and Blend tools targeting it. In general you'll find a number of nice improvements across the controls, networking, data caching, layout, rendering, media stack, and other components and sub-systems.

    Over the next few months we will be releasing a lot of new Silverlight 2 controls (more details on these soon).  Today's release candidate includes three new core controls - ComboBox, ProgressBar, and PasswordBox - that we are adding directly to the core Silverlight runtime download (which is still only 4.6MB in size, and only takes a few seconds to install):

    At runtime these controls by default look like:

    The ComboBox in Silverlight 2 supports standard DropDownList semantics.  In addition to statically defining items like above, you can also use databinding with it.  For example, we could define a "Person" class like below:

    And the add a ComboBox to a page like so:

    And then write the below code to databind a collection of Person objects to the ComboBox (by setting its ItemSource property):

    At runtime our simple app will then display the data-bound Person names (note that we set the DisplayMemberPath property on the ComboBox above to display the "Name" value from our Person objects):

    We could then implement a SelectionChanged event handler like below to run code when a person is selected from the ComboBox:

    Notice above how we can retrieve a reference to the selected "Person" object from the databound ComboBox using the ComboBox's "SelectedItem" property. 

    We can then call the MessageBox.Show() helper method (new in the RC) to display a modal dialog box that displays some details about our selected person:

     

    New Control Skins

    The final release of Silverlight 2 will have a much more polished set of default control template skins than those that were in Beta1 and Beta2.  Our goal with the default control templates is to have a look that is professional and attractive, can be used in the majority of applications as-is (without requiring you to author custom style templates), and which is also easily tweakable using Expression Blend.

    Today's RC build has skins that are close to the final look we plan to ship (there are a few final tweaks we are doing post RC on the focus color of controls, as well as to tighten up and tweak a few issues in some of the control templates).  Below is the default look for the DataGrid, RadioButton, CheckBoxes, and the DatePicker controls with today's RC build:

    Note that the DatePicker control above allows users to type in a date (with a masked edit to ensure it is a valid date), or they can click the calendar icon to the right of the textbox and select the date using a popup Calendar control:

    One of the most powerful features of Silverlight and WPF, of course, is the ability for designers and developers to completely customize the look and feel of any control.  This goes beyond simple styling of colors and fonts - you can literally completely change the visual UI of a control, as well as customize its behavior (for example: add animation) without writing any code.

    Within Expression Blend, simply right-click on any Silverlight control and choose the "Edit Control Parts" sub-menu to open and edit its control template:

     

    When in control template editing mode, you can manipulate any sub-element of a control (for example: a checkbox's inner content), as well as customize each "state" its in (notice the states pane circled in red below).  This allows designers to customize what the control looks like in individual states (for example: checked, unchecked, mouseover, etc).  Silverlight will then automatically handle animating the control from state to state depending on the user action:

    You can learn more about how Silverlight's Visual State Model works from my previous blog post here

    Previous releases of Silverlight often rendered graphics on sub-pixel locations - which could cause lines and shapes to sometimes appear "fuzzy".  The RC of Silverlight has a new features called "layout rounding" that causes the layout system to round the final measure of a control to an integer ("pixel snapping"), which results in crisper lines and fewer rendering artifacts.  This feature is now on by default, and helps make applications look nicer.

    Summary

    The final release of Silverlight is not that far off now.  It has been a pretty amazing project that has come a long way in a pretty short amount of time.

    If you have existing Beta2 applications, please start getting them ready for the final release - as once we release Silverlight 2, users that have existing beta releases installed will automatically be upgraded to use the final version.  Testing your application out with the release candidate will ensure that you can easily update your applications and have them ready within hours of the final release.

    Let us know if you find issues with today's release candidate, and please make sure to post them on the forums on http://www.silverlight.net.

    Hope this helps,

    Scott

  • Raleigh Alt.Net Beer Users Group

    I am happy to announce that this Thursday (9/25) will be the first meeting of the new Raleigh Alt.Net Beer Users Group, or Alt.bug for short. The basic idea is just to go hang out and discuss whatever comes up. We might consider setting topics for future meetings, but for this first meeting we are just going to see what happens. I have already confirmed that Derik, Dug, and Jayme will be coming and hopefully many more will show up as well.

    The meeting will be at O'Malleys Tavern (Guinness and Red Oak on tap) and start around 6:00 PM and go till whenever, hope to see you there.

    -James

  • Transferring Large Files

    Every couple of weeks or so I seem to have a need to transfer (or receive) a relatively large file to another colleague or client. It might be a database zipped up into a 50 MB file, artwork totalling hundreds of megabytes, or various DLLs and source code. Over the course of my career I have used the following techniques for transferring data:

    • E-mail - the most straightforward technique for sharing data. The major con is that many email providers reject attachments over a certain threshold, usually in the 2-10 MB range. Also, e-mail is inherently insecure, making it a non-ideal medium for transferring sensitive data like passwords, credit card numbers, social security numbers, and so on.
    • Upload/Download from a Website - if you have write access to a website you can always upload the data to share and then send a URL to the file(s). The downside here is that you need a website. Security also requires a few extra steps - you need to have some mechanism to only allow the intended recepient from downloading the article.

      Side story: Back in 2002 Microsoft released Web Matrix, which later became Visual Web Developer Express Edition. Prior to announcing the release Microsoft and asp.netPRO Magazine asked me to write an overview of the tool to be included in the asp.netPRO edition that came out right when the product was released officially. There were a group of other ASP.NET folks at the time who also knew about this project and were invited to review my article. I distributed it by putting the article up on a website and provided this group a link to download the article. A couple days later I left for a short vacation during which I did not check e-mail or phone messages.

      The day I left town one of the reviewers leaked the article to Neowin.net, which linked directly to the URL on my website. Shortly after the link was published on Neowin.net I started getting e-mails and phone calls from Scott Guthrie asking me to take down the link. Of course, I did not get these messages until a few days later upon return from my vacation.

      I never did find out who leaked the link/article. I guess it didn't really matter in the end, and it wasn't the article on my server that caused the leak as someone else sent in the link to Neowin.net and they could have just as easily sent the Microsoft Word document. But still, I was rather red-faced when having to explain to the guys at Microsoft why the article was accessible to anyone on the Net and why it took so long to get the article down once it had been compromised.
    • Use a Third-Party Service - there are a plethora of web-based services that are built to allow large files to be transferred between two individuals. Most work by you uploading a file to a site, where it is stored temporarily. You then e-mail the recipient a link to download the attachment. I have used the free service TransferBigFiles.com many times and heartily recommend them. You could also use DropBox and place the file to share in the Public folder. The benefit of this approach is that you don't need a website; however, the security concerns remain. In fact, you might consider the security concerns even (slightly) greater because you are now adding an intermediary into the equation (the folks at TransferBigFiles.com or at DropBox).
    • Physically Deliver the Data - the most secure option is the physically deliver the data - put it on a thumb drive or burn it to a DVD and then mail or hand deliver the data. This approach takes the most work, but is the most secure, especially if you hand deliver the data. And if you are transferring extremely large amounts of data - terrabytes, let's say - this approach would likely be the fastest.

      I used this approach 8 years ago when transferring large amounts of information between myself and a publisher. At the time I only had dial-up access. When the publisher needed to send me several hundred megabytes worth of materials - mostly artwork from previous editions - it was more efficient to mail me a CD. I also had a student in one of my Web Services class, must have been four years ago or so, whose company was running computer simulations that would output several GBs worth of data per test run. To send the day's test data to another facility they would plug in an external hard drive, copy over the data, and then use a mail carrier to overnight deliver the drive. I wonder what techniques they use today.

    How do you share large files with others?

  • September Lounge Update

    August was a great month for The Lounge, with Scott joining the network the web publishers room hit an all-time high of 1.7 Million impressions in the month. To start out this month I have moved Sam Allen who runs the excellent site DotNetPerls from the Small Publishers Room to the Web Publishers Room. Sam has been writing some excellent articles and has been rewarded with great traffic.

    I am thrilled to announce that I will be handling sponsorship for the Herding Code Podcast. I am convinced that podcast sponsorships are a great opportunity for advertisers and I love supporting the great content and conversations that I hear on the podcasts I work with. Herding Code is also joining the Small Publishers Room to run display advertising on their site.

    I have also added a number of other new publishers to the Small Publishers Room, we are now up to 26 publishers in that room!

    Ben Hall has been on my radar for quite some time as he is a member of the team building my favorite unit testing tool (MbUnit) and also does a great job writing about unit testing, build tools, and much more.

    I have known Scott Reynolds virtually for awhile and finally met him at CodeStock. Right after joining The Lounge he ended up joining the great folks over at LosTechies so not sure how long this blog will be in The Lounge, but thankfully LosTechies is a member as well so we aren't really losing Scott.

    Alvin Ashcraft has been writing an excellent daily link post and has also started to write some quality articles, he will make another great addition to the room.

    September is going to be an exciting month for The Lounge, I hope to launch another new room in the next couple of weeks as well as launching a side-project that is related to The Lounge in some ways. Should be able to announce both very soon.

    -James

  • NHaml-Users Google Group

    Since NHaml has broken out of MvcContrib (which is a great decision I think, I hate contrib projects in general since they are very hard to maintain) there isn't a great place to ask questions about using NHaml so I went ahead and created a NHaml-Users Google group.

    -James

  • Search Your Site Using Google's Custom Search Engine

    Remember when people used to debate what search engine was the best among the bunnch? Do you remember back in the day when Yahoo! differentiated itself from other search engines by having actual human beings catalog, summarize, and rate large swaths of the World Wide Web? Today, search is a commodity. It's ubiquitous. Web surfers expect there to be a search box and expect accurate results returned in the blink of an eye. Because of these expectations, it's now more important than ever that your website offers search. I don't care if the site has only five static web pages that only get updated once in a blue moon, you still need search.

    The good news is that Google offers free tools for quickly and easily adding search to your site. With Google's Custom Search Engine (CSE) you can add search to your site within a matter of minutes. All it takes is configuring a few settings and then copying and pasting a snippet or two of HTML and JavaScript into your site. A couple of months ago I used CSE to add search to this blog. You can find the search interface in the upper right hand corner of every page or by visiting the Search page.

    To learn how to add a search engine to your site using CSE check out my latest article on DotNetSlackers: Implementing Search in ASP.NET with Google Custom Search.

    Site Note: Another technique for making your site more searchable is to create an OpenSearch provider. By adding a simple XML file to your website you can have your site searchable through the search box that's baked into the user interface of browsers like Internet Explorer and Firefox. Learn more about this technique by reading: Helping Visitors Search Your Site By Creating an OpenSearch Provider.

     

  • Three's Company, Too

    I try to keep this blog tightly focused on technical issues, but I wanted to share this happy bit of news: on September 1st my wife gave birth to our daughter, Alice Mitchell. Mom & baby are healthy and happy.

    If you'd care to read a bit more about this, I've blogged about it on my personal blog, ScottOnLife.com: Then There Were Three.

More Posts Next page »