C# Competency Test

One of the companies I used to work for has a huge problem in keeping competent developers on board. They basically have a revolving door for anyone that is a decent software engineer and most of the employees that stay there long term can't go anywhere else - which is why they stay.

In honor of their high standard of excellence, I wrote the following C# competency test. If your organization is similar, feel free to send the test around and see if your co-workers can pass it. :)


C# Competency Test

Which technology platform does C# belong to?
1) Microsoft .NET
2) Breakfast
3) Mainframe
4) Beach

What values can you assign to a bool (Boolean) in C#?
1) True, False
2) All of the above

How do you say C#?
(We will accept, "C Pound", "C Number Sign", "C Sharp", "Da Kine" or any form of mumbling)

What special character do you use to close a statement in C#?
1) Semi-Colon
2) Semi-Colon
3) Semi-Colon
4) Semi-Colon

Note: Due to HR policies employees cannot be evaluated based on their competence or technical proficiency. As such, all employees that attempt this test, or even think about taking this test, will automatically pass regardless of test results and will be considered proficient in C# and software engineering.

kick it on DotNetKicks.com

The Power (and complexity) Of .NET Unit Testing

I have recently begun a journey down the path of unit testing. I have been engineering software for many years now and up until this point my testing has been limited to integration testing my code using a front end. While this type of testing is effective in certain instances, it is not thorough and leaves a lot of unconvered bugs. I am part of a very strong engineering team at the moment and one of the software design techniques they swear by is unit testing. As I learn more and more about unit testing the more I am sold on this concept.

Where To Begin
I purchased "The Art of Unit Testing" by Roy Osherove to get my head wrapped around this new software design paradigm; this book is awesome. A great book that covers the basics, the concepts, the theory as well as the application in real world scenarios. I highly recommend you get this book: http://www.manning.com/osherove/

Unit Testing Aids
Unit testing takes time to engineer, no doubt about it. However there are a number of great software packages that will make your unit testing life a lot easier. Whatever unit testing framework you end up choosing needs a good mocking framework to co-incide with it. With that in mind let me suggest TypeMock.

TypeMock Isolator - Aiding your ASP.NET unit testing efforts

Unit Testing ASP.NET? ASP.NET unit testing has never been this easy.

Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle - and for the launch will be giving out FREE licenses to bloggers and their readers.

The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both Typemock Isolator, a unit test tool and Ivonna, the Isolator add-on for ASP.NET unit testing, for a bargain price.

Typemock Isolator is a leading .NET unit testing tool (C# and VB.NET) for many ‘hard to test’ technologies such as SharePoint, ASP.NET, MVC, WCF, WPF, Silverlight and more. Note that for unit testing Silverlight there is an open source Isolator add-on called SilverUnit.

The first 60 bloggers who will blog this text in their blog and tell us about it, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you'll get a license automatically (even if more than 60 submit) during the first week of this announcement.

Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends.

Go ahead, click the following link for more information on how to get your free license.

Aaron
http://www.churchofficeonline.com

How To Register An ASP.NET Custom Control

If you write a custom ASP.NET control you need to register the control either on the Page level or globally in your web.config so you can use the control on forms in your web application.

ASP.NET Page Level Custom Control Declaration

If you want to register your control per page that you use it on, you can use the following declaration at the top of your web form:
<%@ Register TagPrefix="myWebControls" Namespace="My.Website.Controls" Assembly="My.Website" %>

ASP.NET Site Level Custom Control Declaration

If you want to register your asp.net control(s) for you entire website you can use the following declaration in your web.config file:
<add tagPrefix="myWebControls" namespace="My.Website.Controls" assembly="My.Website"/>

Parser Error Message: Unknown server tag

If you pre-compile our web application and do not specify the assembly in your control declaration you will get the following error when trying to use your ASP.NET custom control:
Parser Error Message: Unknown server tag 'myWebControls:ViewEditTextControl'.

Aaron Schnieder
http://www.churchofficeonline.com

LINQ vs FOREACH vs FOR Loop Performance

I made a blatantly stupid logic mistake the other day in a business logic layer method that I was writing and got the following exception: “Collection was modified; enumeration operation may not execute.” The problem what I was trying to remove an object from a collection while I was enumerating through it using a FOREACH statement. After I realized what I had done I solved the problem by populating a new collection with a little bit of LINQ.

   1: // Get the original list of values


   2: List<bool> booleanValues = GetBooleanList(numberOfTests);


   3: // Now create a new list containing the objects we want to remove 


   4: // from the original list


   5: List<bool> valuesToDelete_Linq = new List<bool>(from b in booleanValues


   6:                                                 where !b


   7:                                                 select b);


   8: // Now remove the values safely from the original list


   9: foreach (bool b in valuesToDelete_Linq)


  10:     booleanValues.Remove(b);




There were several solutions I could have used to solve the problem; specifically I could have used a FOR loop, multiple FOREACH loops or the LINQ solution that I posted above (and there are probably more solutions that I haven’t thought of.) This led me to wonder about the performance for each solution, which in turn led me to this post: http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/



FOR – FOREACH – LINQ Performance Tests


My system specs:
- Intel Core 2 Duo 2.5 GHz
- 4 GB memory
- Windows Vista Enterprise 64bit
- .NET Framework 3.5 SP1

After reading through the post above and the comments that it was followed by, I decided to test all three solutions to see what the speed was like. The results surprised me by quite a lot. I knew LINQ was somewhat slow, but I didn’t realize exactly how slow until I ran these tests.




For Loop results: Bool Count = 2002, Time = 00:00:00.0060000


Double For Each results: Bool Count = 2002, Time = 00:00:00.0760000


LINQ results: Bool Count = 2002, Time = 00:00:00.0760000


 


 


For Loop results: Bool Count = 20002, Time = 00:00:00.1380000


Double For Each results: Bool Count = 20002, Time = 00:00:00.9780000


LINQ results: Bool Count = 20002, Time = 00:00:00.9820000


 


 


For Loop results: Bool Count = 200002, Time = 00:00:06.4100000


Double For Each results: Bool Count = 200002, Time = 00:01:39.0640000


LINQ results: Bool Count = 200002, Time = 00:01:39.4220000



So yeah, LINQ is considerably slower than using a FOR loop in this instance. However, I would personally still use LINQ in instances where I am working with a small amount of data and the performance difference is negligible. Why? Because in my opinion LINQ is much easier to read, understand & lends itself to fewer bugs.


Performance Test Application Download

You can download and run the performance test that includes implementations of each solution I mentioned above from the link below.



Aaron Schnieder

http://www.churchofficeonline.com

CollapsiblePanelExtender Height with data controls

I recently ran into a problem where I need to use the AjaxControlToolkit CollapsiblePanelExtender to contain a GridView. The CollapsiblePanelExtender is inside an UpdatePanel and items can be added to the table that drives the GridView within the CollapsiblePanelExtender. After a new item was added to the SQL table and then databound to the GridView, the CollapsiblePanelExtender height would not adjust to account for the new rows.

This problem was driving me nuts until I found the answer in the ASP.NET Forums. All you need to do is wrap your databound control(s) in an asp:Panel or Div with the following style attribute:

style="overflow: hidden;"

Adding that tag will take care of the problem and the CollapsiblePanelExtender will adjust to the proper height.

Example:

<asp:UpdatePanel ID="clpsUpdatePanel" runat="server" UpdateMode="Conditional">


    <ContentTemplate>


        <asp:Panel ID="pnlTitle" runat="server">


            <div style="padding: 1px; cursor: pointer; vertical-align: middle;">


                <h1>


                    This is my collapsible panel title


                    <asp:Image ID="collapseImage" runat="server" SkinID="TipGreen" />


                </h1>


            </div>


        </asp:Panel>                      


        <!--Panel with style="overflow: hidden;" will ensure the CollapsiblePanelExtender will display 


            the contents of a databound control after PostBack -->


        <asp:Panel ID="pnlDataBoundControl" runat="server" Height="100%" style="overflow: hidden;">


            <asp:GridView ID="grdvwExcludedAffiliates" 


                  runat="server" 


                  AutoGenerateColumns="True" 


                  DataSourceID="odsMyData" >


            </asp:GridView>


            <asp:ObjectDataSource ID="odsMyData" 


                runat="server" 


                OldValuesParameterFormatString="original_{0}" 


                SelectMethod="GetMyData" 


                TypeName="BusinessLogic.MyLogic">


                <SelectParameters>


                    <asp:Parameter DefaultValue="0" Name="Id" Type="Int32" />


                </SelectParameters>


            </asp:ObjectDataSource>


            <asp:UpdateProgress ID="updProgress" runat="server" AssociatedUpdatePanelID="clpsUpdatePanel">


                <ProgressTemplate>


                    <asp:Image ID="prgImage" SkinID="Spinner" runat="server" />


                </ProgressTemplate>


            </asp:UpdateProgress>


        </asp:Panel>


        <ajaxToolkit:CollapsiblePanelExtender ID="pnlExtender" 


            runat="Server"


            TargetControlID="pnlDataBoundControl" 


            ExpandControlID="pnlTitle" 


            CollapseControlID="pnlTitle"


            Collapsed="False" 


            ImageControlID="collapseImage"  


            ExpandedImage="~/Images/arrowdown.gif" 


            CollapsedImage="~/Images/arrow.gif"


            SuppressPostBack="false" />


    </ContentTemplate>


</asp:UpdatePanel>




- Aaron Schnieder

http://www.churchofficeonline.com

Creating a SQL Exclusion Table Using LEFT JOIN

For a current project I needed to create a table that contained a list of exclusions that should be filtered out from another table when a specific set of criteria were met. This is a pretty common scenario where you have a large table of data and you want to be able to provide the ability through a user interface for a user to filter the data using an exclusion list.

I have access to an amazing SQL guru, Peter Loos, who was able to help me brainstorm the best approach for altering existing stored procedures to include a filter for information included in the new exclusion table I added for this feature. During the course of speaking with Peter I learned the following:

!= operator is being deprecated, we need to use <> from here on out.

TSQL NOT IN performance sucks; a JOIN should be used in place of NOT IN if at all possible.

My initial solution was do modify the existing stored procedures which returned the list that I needed to filter with my exclusion table by adding a WHERE clause that included a NOT IN (SELECT…) statement. After speaking with Peter it became clear that NOT IN could cause performance problems down the road, so I went down the path of figuring out how to do this with a JOIN statement.

Example Exclusion Table

In this example of how to setup an exclusion table in SQL, we will use an address book example. Lets say I have an address book of all of the people I know and their contacts like the following:

AddressBook Table

SQL Table Definition

CREATE TABLE #AddressBook(AddressBookId INT PRIMARY KEY


                            ,FullName varchar(100) NULL


                            ,Phone varchar(20)    NULL


                            ,Location varchar(200) NULL)


INSERT INTO #AddressBook VALUES (1, 'Jack Bauer', '853-234-5968', 'Los Angeles, CA')


INSERT INTO #AddressBook VALUES (2, 'John Stewart', '451-856-4468', 'New York, NY')


INSERT INTO #AddressBook VALUES (3, 'Jim Cramer', '452-938-7228', 'New York, NY')


INSERT INTO #AddressBook VALUES (4, 'Stephen Colbert', '451-467-3333', 'New York, NY')


INSERT INTO #AddressBook VALUES (5, 'Bernie Madoff', '609-611-4468', 'Federal Prison, PA')


 


SELECT * FROM #AddressBook


Resultset

image

NotMyFriends Table

Now lets say I want to filter my address book down to a list of just people I consider my friends, so I create an exclusion table and enter those contacts that I DON’T consider to be friends.

SQL Table Definition



CREATE TABLE #NotMyFriends(NotMyFriendsId INT PRIMARY KEY


                            ,AddressBookId INT)


INSERT INTO #NotMyFriends VALUES (1,3)


INSERT INTO #NotMyFriends VALUES (2,5)


 


SELECT * FROM #NotMyFriends


Resultset

image

Filtered Resultset

So the end goal of creating the NotMyFriends exclusion table is to be able to filter the AddressBook table, removing anyone listed in the NotMyFriends table so my final resultset is a list of contact information for just my friends.

NOT IN Solution

The first way I thought of doing this was to use a NOT IN statement, which filtered the results just like I wanted. The only problem with this solution (as I learned from Peter Loos) was the NOT IN statement performance sucks.

SQL NOT IN Query



SELECT * 


FROM #AddressBook AS MyFriends


WHERE MyFriends.AddressBookId NOT IN (SELECT AddressBookId from #NotMyFriends)


Resultset

image

JOIN Solution

So now I wanted the exact same results as I got using the NOT IN statement, but I want to use a JOIN instead to improve the performance of my query.

SQL JOIN Query



SELECT * 


FROM #AddressBook AS MyFriends


LEFT JOIN #NotMyFriends


ON MyFriends.AddressBookId = #NotMyFriends.AddressBookId


Resultset

image

We are almost there, except the filter didn’t work. The LEFT JOIN combined our full table of addresses & our exclusion table list, but it didn’t filter the results. Looking at the results above we need to add a WHERE clause to our LEFT JOIN.

SQL JOIN Query with WHERE Clause



SELECT * 


FROM #AddressBook AS MyFriends


LEFT JOIN #NotMyFriends


ON MyFriends.AddressBookId = #NotMyFriends.AddressBookId


WHERE #NotMyFriends.AddressBookId IS NULL


Resultset

image

Query Script Example Download

As you can see, the final SQL JOIN query that included the WHERE clause gets us the filtered results we wanted from the exclusion table contents and performs well to.

I created a sample TSQL script that uses temp tables to demonstrate the queries I posted above. You can download it here:



Aaron Schnieder

http://www.churchofficeonline.com

The keys to successfully delivering software

I have been developing and delivering software for over 10 years now and I have learned a few things along the way. I am not the smartest developer, the most elegant engineer or a hardcore software design geek. I have however had a good deal of success delivering software that meets business requirements on time and learned what works and what doesn’t along the way. In this post I will share with you what I have learned that works well and what routinely causes failure.

Extremes lead to failure

Let me warn you in advance of a theme that you are going find throughout this post; avoid extremes. This is a general principle in life that applies heavily to successful software development. For example, there is nothing wrong with alcohol in moderation. However, if your goal in life it is get hammered at any possible opportunity you are going to watch your life go down the drain. Cheeseburgers are great, they are yummy and one of man’s greatest inventions. However, if you hit up In-N-Out every day for lunch and throw down a couple of double-doubles you aren’t going to be a happy camper after a while. The same basic principles apply to successfully delivering software, you need healthy moderation in all areas of the process.

Cut out the cancer

You need to have a good engineering team. Now the definition of a good team can very widely, but here is my take. You need a group of engineers that are good at what they do, have interpersonal skills, can communicate, check their egos at the door and all share the common goal of delivering quality software that meets the business needs on time. If you have engineers on the team that are a cancer to your success you need to figure out how to deal with them rather than avoid them or mitigate them and hope the problem goes away.

One of the common engineering cancers is the apathetic engineer. This is the engineer that comes in, does just enough not to get fired and looks for every opportunity to do anything other than work. The apathetic engineer will drag down those they work with and will make the rest of the team of engineers who are working hard angry.

The other common engineering cancer I have seen on teams is the ego engineer. This is the engineer that feels they are the apex of software development and no one before them or after them will ever be as good. The ego engineer is a poison to the team and most of the other engineers will avoid working with this person like the plague. They aren’t part of the team, they are an island and almost always are more talk than substance.

Your engineering team needs to gel well, with strengths and weakness complimenting each other. It is also very important that your team enjoy working together and respect each other. If your team hates to work with each other and there is disrespect within the group they will not perform well. Remove any cancer that exists and when you add new team members make sure that they are the right team fit.

Success Starts with good requirements

You can successfully deliver good software on time without good requirements, but it isn’t easy. Again, you don’t need an extreme here. You don’t need your marketing/product group to spend months and months and months analyzing requirements and debating over little nuances only to deliver the best set of requirements ever written two weeks before the year long project is due.

On the other end of the spectrum are back of the napkin requirements. I had a product guy once that would go to bars, get drunk and then write out all of our “requirements” on the back of a napkin while he was hammered. He insisted that we should be able to successfully build any complex application with no more input that what he was able to scribble on a napkin while inebriated. As you can imagine, this end of the extreme didn’t work out too well either.

You don’t need perfect requirements, but you need good requirements. Good requirements are well thought through, documented well enough that engineers can understand what the business needs are by reading them and have actionable requests. Wireframes, mockups and user experience scenarios are also very helpful to put into requirements.

don’t under or over engineer

Ah, now that you have a good team in place and you have some requirements you can work with here comes the fun part, actual engineering. Unfortunately it is still pretty easy to fail the actually software development part of the process even if you have a good team and solid requirements. Successfully delivering software from an engineering point of view requires that engineers be utilized effectively and over or under engineering is not allowed to be part of the process.

Under engineering is otherwise known as hacking or spaghetti code. This is where your engineer’s only concern is getting the feature “done” and they have no concern for how it is done. Common excuses here are that “the requirements are changing all the time”, “we just had to get something in there”, “lets just get it working and refactor it after the fact”, etc., etc., etc. Hacking crappy code together never works out well. Even if you can hack features in to a working state, this approach will lead to tons of bugs and doesn’t create a foundation that you can build upon for future features and enhancements.

Over engineering is a little bit trickier than hacking. Hacking is generally considered “bad” by most engineers and you won’t get much push back if you bring it up as a problem. Over engineering on the other hand is commonly accepted and viewed as good work because the theories and design being applied are all generally good software development. The problem here is that if you spend all of your time writing the most awesome, perfect, beautiful, slick, lovely, sweet, hotness widget you will probably never deliver anything at all, let alone on time. Software needs to be designed well, but if it is over engineered, overly complex and plans too far ahead in the future it is like spending the time to lay the foundation for a skyscraper when all you need to build is a single story house that you might want to add a room on to later on. Over engineering can be just as big of a problem as hacking because at the end of the day you still don’t end up with that you need to be successful.

Balance is once again the key to success. Don’t hack, but also don’t spend so much time trying to put together the ultimate software design for what you need that you don’t get anywhere. Come up with solid designs that are flexible and are forward thinking, but keep them within the scope of the requirements and apply KISS (keep it simple stupid) as much as humanely possible. Make things only as complex as they need to be, comment your code and keep on alert for engineering scope creep as well as project scope creep.

How does this apply to .NET development specifically?

These basic principles apply to successful software development in any language on any platform. However, from what I have seen .NET development is specifically prone to problems with under engineering. Because Visual Studio is such an awesome IDE and because Microsoft makes so many of the tasks so easy to complete in ASP.NET there is a tendency for developers to rely on the wizards and just brute force something that works into place. ASP.NET developers need to intimately understand the platform that they are developing on as well as understand best practices for general software design.

I hate to say it, but a lot of the problems come from software developers that worked in previous Microsoft frameworks such as Visual Basic who “update” their skills by opening Visual Studio 2008 and starting to code. The .NET framework is vast and if you want to write successful software in .NET you need to understand a) solid OOP software design principles and b) the framework itself. If you don’t understand solid OOP design principles, you need to take some classes or update your knowledge with some good books to get the basics. If you don’t understand .NET, I would recommend that you read some of the Intro to the Framework books and take one of the foundational Microsoft tests. The tests aren’t perfect, but if you study for them and pass them I guarantee you will come out understand the .NET framework better than most.

How Do I proceed?

If you read through this post and identified some area(s) that your company is currently struggling in, the fix probably won’t be quick or simple. You are going to have to put together a plan of what needs to change and how it should change, get buy in from the right individuals and then slowly implement process changes or team changes that will fix any issues that you identify. It is hard work to get past process or team problems, but well worth it when your software cycle becomes easier and is successful on a frequent basis.

Aaron
http://www.churchofficeonline.com


kick it on DotNetKicks.com