See http://jeffreypalermo.com/blog/use-guid-comb-in-your-database-if-you-need-guid-keys-but-don-t-want-to-take-a-big-performance-hit/ for this article.
Tag Archives: Tips & Tricks
If it takes forever to start your app with the debugger, check for thrown exceptions – level 300
Overview of Exceptions
There are quite of a few things that are just laws of Object-Oriented development, and one of those is that exceptions should be avoided. If you can prevent an exception from being thrown, do it. In the world of managed runtimes, particularly Java’s JRE and .Net’s CLR, objects are “thrown” to communicate errors. In a try/catch block, the language limits objects that can be thrown to ones that derives from System.Exception, or java.lang.Throwable in Java. When an object is “thrown”, the runtime stops and assembles the callstack and some other information and gives code at all levels of the callstack an opportunity to catch the thrown object (exception) and do something with it. If the exception is never caught, the runtime with catch it and terminate the program.
Clearly, exceptions being thrown in code is a bad thing, and it signals and unstable state in the program. It may be a huge bug, or the network may have gone down. Either way, and exception is thrown. Proper error handling with catch the exception at a point high enough in the callstack where the program can actually make a decision to do something about it.
Swallowing exceptions (wrapping code in a try/catch where the catch block is empty) leads to less feedback. An exception will happen, but it will be swallowed, and you won’t know about it. As soon as you start swallowing exceptions, they will start happening without being noticed. Debuggers pay special attention to exceptions, so swallowed exceptions (thrown, immediately caught, and ignored) will slow down the debugger with each occurrance.
Solution
Go to the Debug menu in Visual Studio and select Exceptions. CTRL+ALT+E is the shortcut. Check the checkbox for “Common Language Runtime Exceptions”. Now when you start your debugger, it will break when a managed exception is thrown. It will break on the line from which the exception originates. You can use this technique to find all the exceptions that are happing in your software right under your nose. If you refactor to keep those exceptions from happening, you’ll see a marked improvement on debugger load time (provided there were a large number of exceptions happening previously).
Alternatives
The alternative to debugging often is automated tests. When each small piece of code is verified independently, you don’t have much occassion for running the full application in debug mode. If you have unit tests as part of your automated test suite, a failure will point to the exact place where you have the problem.
Rule to live by
Fail fast. Fail fast. If your software is going to fail, make it fail quickly so that you can get the feedback earlier and fix it earlier. Don’t hide the problem by ignoring it or burying it in a log file that’s already verbose. If you are unfamiliar with this concept, read this article by James Shore.
Don’t use an exception as a return value. In an ideal situation, your application should run with 0 exceptions. You may have a library that swallows an exception, and that would be unfortunate, but keep your application code clean. If you can anticipate an exception happening, perform some checks to avoid it being thrown.
How to keep an eye on exceptions
Use Perfmon. Watch the counter “.Net CLR Exceptions# of Exceps Thrown”. The number should be zero in an ideal situation. If you have an app that can’t avoid some exceptions, you can watch “# of Exceps Thrown / sec”. This number should be close to zero. If your application is constantly throwing exceptions under ideal circumstances, you have some work to do.
[tags: exceptions, programming, c#, java, failfast, objectoriented, development, .net, clr]
Build and publish .Net 2.0 projects with NAnt and the MSBuild task – level 200
When
.Net 2.0 first came out, I was left using the <exec
/> task to call msbuild.exe to build my solution. The NAnt <solution
/> task is specific to .Net 1.1 because Microsoft change the structure
of project files to be MSBuild scripts.
I may be a little behind on this, but NAntContrib now contains a
<msbuild /> task that can either just build your solution or execute an
entire msbuild script. Here is an excerpt from my NAnt build script:
<target name=“compile“>
<echo message=“Build Directory is ${build.dir}“ />
<msbuild project=“src/xxx.sln“>
<arg value=“/property:Configuration=release“ />
<arg value=“/t:Rebuild“ />
</msbuild>
</target>
The task brings the full power of MSBuild to NAnt. At first, I thought about converting the
entire build to MSBuild, but I have so much invested in NAnt that I can’t
justify the effort of conversion yet since we won’t gain anything. For now, we’ll have a mixed build process.
Later when we need to package with ClickOnce, we can use the
<msbuild /> task with the publish target:
<msbuild project=“src/xxx.sln“>
<arg value=“/p:Configuration=${project.config};ApplicationVersion=${project.version}.${CCNetLabel};MinimumRequiredVersion=${project.version}.${CCNetLabel};UpdateRequired=true“ />
<arg value=“/t:publish“ />
</msbuild>
This node builds the ClickOnce deployment package for our
application. CruiseControl.Net executes this NAnt
script, so every time we commit code to Subversion,
we build out the ClickOnce install package and commit it back to SVN and tag
it. No matter what version we need, it’s
right at our fingertips.
By the way, I now always edit my build scripts in VS 2005
since Resharper 2.0 helps
with NAnt scripts. I can rename targets,
find usages, jump from target to target just as I can in code. In short, it has refactoring and navigation
support for the build scripts.
Another Winforms testing framework from Thoughtworks! – level 200
Testing WinForms UIs is tough. Manual testing is slow and difficult to repeat. Vivek Singh has just released a new version of SharpRobo, a WinForms testing framework. Vivek recommends running these tests through NUnit, and from practive, I’ve found that whatever I can test with NUnit, I can test with FIT, so I’m excited to try it out. Thoughtworks has previously produced NUnitForms.
Selenium and FIT are good for testing Web apps, but I am dealing with a windows application at this point. Kudos to Vivek for this library.
Configuring Subversion (SVN) on Windows – level 200
I just finished configuring my team’s Subversion server, and
I’ve learned quite a few things in the process.
On past teams, someone else gravitated to svn admin duties, so I didn’t
complain.
My subversion server is running Windows 2003 server. Setting up subversion initially is a piece of
cake, and the
distribution comes with an installer for Windows. At that point, you can expose the repository
to the network via a URN share. You can
use file://server/repository to
access it. This doesn’t require a daemon
running to keep track of the source code.
You can give folks NTFS permission to the share. I prefer, however, to manage users through
subversion by using svnserve.exe. This
is an executable, so I chose to use svsservice.exe to install it
as a Windows service that would automatically start up with the server.
I have everything set up with the svn:// protocol, and each
person with access to the repository gets commit emails now. Commit emails are very important in a team
environment because each team member is responsible for the well-being of the
code, so everyone needs to know what is being changed.
I want to publicly
thank some people who have saved me some time.
Steve Donie and James Higgs have both posted about setting up subversion
on Windows, and reading those posts has saved me some time.
Steve
Donie’s tutorial: http://donie.homeip.net:8080/pebble/Steve/2006/02/27/1141079943879.html
James Higgs’
tutorials: http://staff.interesource.com/james/search/?keyword=subversion
[tags: svn, subversion, scc, windows, programming]
Bread and butter Resharper (demo) – level 300
with it before then, but I didn’t learn enough to get the full benefit. I want
to thank Steve Donie and Jeremy Miller for helping me to learn the full
featureset of the product. I can’t imagine coding without it now. I’m using
Resharper 2.0 (with VS 2003 as well as VS 2005), and I’m not going to talk
about what features are different from 1.5 because v1.5 has all the features I
will talk about here.
Resharper is a refactoring tool, yes, but that seems to be a
small portion of it. It is an overall productivity tool. It does some
code-generation, some fill-in-the-blank, some extract method/variable, some
move method, some create method/property/constructor, and a whole lot of
helping you with the job of programming.
You can certainly use the mouse with
this tool if you wish, but the shortcut keys make me so productive on the
keyboard that I don’t have to slow down and reach for the mouse.
I’m going to go over the features that I use. I’ve created a test project using the Movie application that comes as an example in VS 2005. This is not
an exhaustive list, and it isn’t every dialog, but I use these all the time.
Every screenshot is inside Visual Studio in the code window. Here is the menu
for Re#er. It has the standard things.
Optimize usings
You’ll notice in the screenshot that two using statements
are grayed out. These are unnecessary. There is no code that requires these.
They are visual noise. Let’s get rid of them. CTRL+ALT+O
Reformat code
Resharper has so many formatting options that (if it’s
important to you) you can share settings with the team so that all code looks
uniform.
But that not all there is. Re#er will include optimize usings,
shorten System.DateTime to DateTime, remove unnecessary “this”
keywords and put your “virtual public void” back to “public
virtual void” (if you have a boneheaded programmer that left it like
that). CTRL+ALT+F
Find code file
Why go for the solution explorer when you know the class you
want.
Just start typing, and Re#er will narrow down the list as you type. You
can take a shortcut and type “*Tester” to find all your NUnit
classes.
CTRL+N
Find file
Now you need to open one of your NHibernate mappings, but
it’s not a code file.No problem. Just narrow down from all files. CTRL+SHIFT+N
Find usages
How many times have you wondered how many classes in your
solution are calling a particular method on IConfiguration. Is it used at
all?
How much? Do you have any hope to change it without major regression?
Find out who is using it easily and jump right to the usage, all from the code
window.
CTRL+ALT+F7
Context-sensitive refactoring
You will typically know what you want to do, and Re#er will
be there waiting for you. Stick the cursor on a member name and choose from
the refactorings available there. One of my favorites is “Change
Signature”.
It makes it easy to add an extra parameter to a class
constructor, and Re#er will add it in all the places where that constructor was
used. CTRL+SHIFT+R
Error analysis
Ever wondered if you needed to clean up the class a bit?
Re#er keeps a running tab on errors and warnings in the upper-right hand corner
of the code window. When things are good, it will stay green. It’s always
there.
Find subclass
You know you need to go to your default implementation of
IConfiguration.
You know, the one that actually reads from app.config? Your
entire application uses IConfiguration since you don’t want to tightly-couple
to the app.config file.
Sometimes you just want to pop over to whoever
implements the interface or inherits from a class.No problem. Place the
cursor on the interface (or class) name and find who inherits. CTRL+ALT+B
Context help
Wherever you are, if Re#er can help, it will show you a
yellow lightbulb or pop up a suggestion. If you like what it shows you, give
Re#er the go ahead and continue without missing a beat. in this case, I need
to add a using statement.
Isn’t it annoying to stop your flow just to go up to
the top to adding a using?
Now you don’t have to. With all the
context-sensitive helps, and whenever there is a lightbulb, just press: ALT+ENTER
Line-level refactoring
The red lightbulb is a context-sensitive help that is also a
warning.
Go ahead and fix the error quickly (or just let Re#er do it for
you). In this case, I really wanted to match up types, but I made a mistake. ALT+ENTER
Live templates
These are like code snippets but much simpler, and they have
been a Re#er feature for a long time, so just because VS 2005 has a similar
feature that is harder to use doesn’t mean that I should stop using my
templates. I have created templates for common things like creating a new
NUnit test fixture and a test method. In this example, I have typed
“foreach”, and Re#er awaits to take it from there. Here is a before
and after. CTRL+J
Name suggestions
I always call my IDomainRepository “repository”
when using it in a method. I like descriptive variable names, but it takes
more typing. . . but not with Re#er. After the type name, just ask Re#er for a
name.
Pick from the list. CTRL+SPACE
Show Class Members
When you are in a class you want to remember what is there
and jump to it quickly. Use Re#er to move around quickly. CTRL+F12
Recent files
If you want to jump between files that you have recently
opened, just pull up the list and arrow down to the one you want. CTRL+E
Dead code detection
If code is dead, delete it. It’s never a good idea to save
code for a rainy day. It just pollutes the codebase. Re#er will color it gray
for you. Let it help you keep the codebase clean.
Generate Code
I saved this one for last because it’s my favorite one.
This is a micro-code-generator. It will create a property/constructor, etc
exactly the way I would have typed it. In that case, why do I want to type
it.
Imagine this: You have a new domain class with 8 fields. Do you really
want to type those 8 boring properties and a constructor? I don’t. A few
shortcut keys, and the class is done. ALT+INSERT
[tags: programmer, programming, resharper, jetbrains, productivity, c#, .net, vs2005, visualstudio]
Hiring technical people, take 2 – level 100
This is a planned follow-on to my first post on hiring. In my first post, I talked about the general process I like to go through when hiring a new programmer. Of course, you can bet that this is continually evolving based on what works and what doesn’t. So far, this is working very well.
When hiring, you obviously want to ask all kinds of questions, but I have to admit, I agree with a popular mantra that many speak about, including Joel Spolsky: “Smart people who get things done.” Smarts isn’t everything. Take me, for example. I know so many people who are smarter than me, hands down, but I make up for it with drive and persistence. 🙂 “Getting things done” isn’t everything either, because I’ve known my fair share of those people as well. Sure, the system works (now), but with the “smart” in there, it could be a big jumbled mess that’s a maintenance nightmare. I believe the two go together.
How the heck do I determine if the candidate is a good mix of the two? Ha ha. No easy answer there either. I have to use my own experience to weight everything the candidate has to offer. Some reference checks can help to determine the mix. Some phone screen questions can contribute as well. For me, I think the coding assignment can help determining the “smart” part. I’m not fond of assignments like “create a Roman number converter” because that mostly tests procedural programming skills. While there is value in that as well, I’ve preferred to ask them to do a little something with a technology that they have no experience in. I leave it open-ended. Using every available resource, this is downright easy for a smart person, and it should be because I need to respect their time. It has the potential, though, of saving an interview for a candidate we wouldn’t hire.
Getting things done: I think questions can bring this out. It seems like it almost a mentality of getting things done as opposed to talking about getting things done. Stories of past projects can help with this factor.
What else is pertinent in a candidate I’m looking for? Ignorance of something! What? Did I read that correctly? Yes. If there is something the candidate doesn’t know, he is human. If the candidate is so well-versed in everything, then he’s kidding himself. Very few people are like that, and they certainly don’t have to interview. Real people don’t know things. Real people are constantly learning. Someone who is slow to admit ignorance might be hard to work with. From personal experience, the more I learn the more I realize how much I have to learn. Does that scare anyone else but me? I hope so, but it’s real. Remember the teenager syndrome? Teenagers know everything! In fact, they know little, but are foolish enough to think they know everything. Wise folks reject this notion as time progresses. I’m impressed with someone who can talk about the things he doesn’t know. This shows me that he can recognize gaps in knowledge that need to be filled.
Continual learning is another biggie for me. Someone who is constantly learning impresses me. I ask what technical things the candidate is reading/studying. Answers that include books, magazines, etc are all good. Nowadays, podcasts are in that boat too. If a candidate struggles and finally comes up with an article they ran across “last week”, I get suspicious and follow up on the topic.
There are so many different techniques to find good people, and there is no way I can know them all, but I continue to learn. 🙂
[tags: hiring, technical, candidates, recruiting, interview, interviewing, managing, programmer]
Hiring technical people, take 1 – level 100
With unemployment at essentially ZERO these days, it’s hard finding the best people, especially skilled knowledge workers like programmers. Every company needs programmers, so the good ones who happen to be looking are few and far between. By contrast, the mediocre to poor programmers are abundant and available. Wading through the duds to find the right guy can be a daunting task.
In-person interviewing is the most expensive part of searching for new employees. It requires real time from the people in the department. Each time a candidate is turned away because of an interview, the company has lost time and money. The trick is to keep the bad candidate from making it to the in-person interview. I’ve recently employed a few techniques to minimize time spent by my team on interviews. My goal was to bring a candidate to interview only if I felt we would be making an offer. Here are the things to do to weed out the folks who are unlikely to make it to the offer stage:
- Do a technical phone screen on every candidate. Ask basic questions about levels of experience on required technologies and 3rd party libraries. For instance, if your team uses 3rd party tools like CruiseControl.Net, Resharper, NAnt, NUnit, SVN, NHibernate, Rhino Mocks, etc, then it’s a plus if a candidate has used some of them. This phone call should also ensure that you aren’t wasting each others’ time. Make sure the candidate is interested in the kind of team you are running.
- Given them a “take home test”. Make the questions appropriate for the technology (no insulting trivia). Good questions are ones that a good programmer could answer easily. The duds will fail the test, and you will have saved some time.
- Ask for a sample of code including code and SQL. What they give you is a good indication of the types of things they have worked on and are confident in.
- Give a coding assignment. Call it an audition if you wish. This can be any small coding task, but make it a prerequisite to the interview. The interview is the most expensive part of the recruiting process. I like to find a technology the candidate is not familiar with and ask them to produce something with it. This ensures that the candidate can pick up new things and produce quickly.
I fully recognize that recruiting is not a science. The worse thing to do, however, is to just talk in the recruiting process. I’m going to ask my programmer to write complex software, and talking about it is little indication of that aptitude. I prefer to see some example of the professed ability. When a candidate easily progresses through all the above steps, it’s pretty certain that the in-person interview will be a “getting to know you” gate that opens easily to an employment offer right away.
[tags: hiring, technical, candidates, recruiting, interview, interviewing, managing, programmer]
No Fluff Just Stuff: Testing with Selenium with Neal Ford – level 200
On the 3rd day of NFJS, Neal gave a talk about
testing with Selenium. Selenium is a
end-to-end testing solution for web applications. This is testing on the customer acceptance
level and not the unit-testing level.
Selenium was developed by ThoughtWorks to test an
application they were working on. It
grew out of an actual need. The testing
framework was good enough to open-source it.
They named it Selenium because it is the key mineral that protects the
body from mercury poisoning (if you recall Mercury testing tools).
With Selenium, tests run directly in the browser. It supports most common browsers that are in
existence today. The test engine itself
is written in JavaScript and runs directly in the browser. It creates a compatibility layer across
browsers. Calling an API in Selenium
will map to the right API in the current browser.
It deploys a browser bot that runs alongside your
application. This bot can accept
commands. The bot is embedded in an
IFrame. Consequently, it will work with
any javascript-enabled browser. To use
selenium, you must take the selenium folder and deploy it alongside the
application. This will be in a
non-production environment, of course.
There are two modes for Selenium: TestRunner mode and Driven mode. In TestRunner mode, the browser is driven
inside the process. In Driven mode, the
Selenium can be driven from another process remotely.
With TestRunner mode, Selenium is alongside the
application. The user can launch it and
click the “Run Tests” button to run tests.
Selenium test case is a simple html table. The first row is a title and can be
ignored. The next rows are the commands. By default, it looks for
tests/TestSuite.html.
You can run Selenium tests directly in a browser and watch
it exercise your application at full speed or “walk” speed. You can also step through. If you need to run it from another process, you
can use Driven mode to run it from Ant, Nant, Maven, CruiseControl or anywhere. Once you have a big suite of tests, you’ll
want to have it integrated with your normal build process.
There is a Selenium IDE that can be used to record
tests. The IDE is an extension for
FireFox at http://www.openqa.org/selenium/. Selenium can test regular web applications as
well as AJAX web applications.
Blogging from Tech Ed 2006 – Brian Button on how to write frameworks – level 300
Brian Button from the patterns and practices team gave a session bright and early Friday morning that was great. He covered how to write frameworks. What’s interesting is that he focused on writing frameworks as a non-technical problem since he’s seen too many frameworks that solve the wrong problem very well. J
- Rules for Framework development
- Clients come before frameworks.
- Be sure that you are solving the right problem.
- Quality, quality, quality.
- Be an enabler: You can’t solve every problem, so solve many problems but allow for extensibility points.
- Creating frameworks is a people problem.
Brian stress that it’s impossible to write a good framework without a real application using it. It will end up missing the mark. He suggests, instead, writing several applications and harvest a framework from the common parts of the application. This resonates with me because I believe it’s very difficult to write code that’s intended for reuse. I write code for use, and if it needs to be used elsewhere, I’ll harvest it into a shared library.
Brian was very bold at this Microsoft conference, and I applaud him for this. He stressed automated testing, and he said that here and now there is NO EXCUSE for not writing automated tests. Bravo! Brian contends that functional testing is a waste of a tester’s time. The thought is that testers are too valuable for functional testing that could be covered with automated testing. Testers should be testing the harder things.
Brian shares my frustration about sealed classes in the .Net Framework. He has encountered parts of the framework that are sealed, and when he needs to extend them, he can’t. Sealed classes are hard to write testable code against. He made a good point that I hadn’t thought of before: If you seal a class, you are saying “I can predict the future, and this is all that this class will ever need to do.”
Finally, Brian advocates these in creating a framework.
- Do open planning.
- Practice open prioritization.
- Show visible progress.
- Don’t allow surprises.
Brian recommends
- Resist the urge to create something new. Harvest frameworks from existing applications.
- Encourage quality throughout the project. Lead by example and testers rule!