Introduction
This is really just a list of things (in no particular order) that I regard as good or useful practices, all of which in some way contribute to your efficiency. Efficiency is a pointy-haired boss' word that they like to throw around whenever they feel more is required from you. Here, anyway, in no particular order, are some things then I regard as being conducive to personal efficiency, particularly for developers, and even more so for .NET. If you find something on the list that you don't do, then think about doing it. A lot of them are intertwined, and really, the whole drive is self-learning and self-improvement. On the technical side the move is from engineering (building everything from scratch everytime) to production (attached together pre-engineered pieces). This is really the way things are moving from the developers perspective, and more functionality has to be built in less time. For that to happen, we need to be smarter developers doing less actual coding and more re-using of existing code. You need to leverage as much as you can. All these things do tend to tesselate into each other, reinforcing each other, which is a good thing. I could make some funky flow diagram to illustrate, but I will restrain myself, I think you get the idea. Also, research time becomes self-supporting, as the savings made further free time to do more research. If you are doing it right, you should be spending a significant proportion of your time doing research.
Background
I was recently employed to improve developer productivity at a company developing solutions with ASP.NET. This company had its very own pointy-haired boss, who had no idea about what software development is actually about. I was constantly pressed to find faster ways for the company to develop software. So a lot of these recommendations come out of practices I discovered along the way. My pointy-haired ex-boss will be the subject of another article, where I get to do him full justice.
1. Application Blocks
These are the free, open-source collections of best-practice code written to cover specific areas. There are ones to cover Enterprise applications, Data Access, Application Updates, etc. The list goes on, and it is possible to write your own. I spent a lot of time building my own Data Access class to speed things along, then took a look at the Data Accessor block to find it amazingly similar. I now kind of wish I had invested that time in learning how to use the existing Data Application block, then I would have been able to take that knowledge with me anywhere. As it stands, I can't take the proprietary code with me, so I must settle for finally getting round to do what I should have done in the first place. So my advice is learn how to use them, and leverage what is out there, and the long term benefits will be yours. I recently watched a webcast on using the updater block, and it is really cool if you are writing Windows apps (really blurring the lines between the web and Windows worlds). Check out the links in the reference section. There is a webcast on creating your own too (another reason to get into the webcasts).
2. MSDN Webcasts
Educate yourself for free! In my opinion, these are an absolutely brilliant resource. Go and have a look, great for learning about a new area, free online seminars for learning whenever suits you. I try and watch a couple a week. It's got me really excited about Whidbey. MS also has free assessments for ASP.NET, to help you on the road to certification.
3. Use the tools available
You are not alone. There is a huge community of developers out there, producing great tools to help. I am a huge fan of codesmith and it has made generating Web Forms and Business objects a breeze. A good rule of thumb is anything that changes only depending on the database can be automated with codesmith. I made a whole bunch of templates and they've paid the company back 100 times over in time savings. This is the bridge you can use to go from coding solutions from scratch, to generating solutions against databases automatically. Try it. There will be no going back. This is a big step forward from engineering to production.
There are other great tools too, I use mainly Nunit, The Regulator and NDoc. For a full list, see the reference list at the end.
4. Custom Errors
If it breaks, don't show a server error. Please. CodeProject itself has a good page for when something breaks.
Placing the settings below in the web.config will specify a page to redirect to if an unhandled exception occurs. The remoteonly
setting means you will only be redirected if you are not running on the local server (i.e. debuggers will see the real error). This is the minimum you should do if you do not want your users to see server errors when something breaks (which may or may not be the fault of your application - so expect things to break!). You can also specify pages to use for each HTTP error status code, and I put some example there too. By the way, in the code below I took out all the angle brackets, otherwise it gets interpreted as invalid html.
customErrors mode="RemoteOnly" defaultRedirect="ApplicationError.aspx"
error statusCode="500" redirect="/error/callsupport.htm"
error statusCode="404" redirect="PageNotFound.aspx"
error statusCode="403" redirect="/error/noaccess.aspx"
/customErrors
Additionally, it is important to be notified when errors occur, and have a log to refer to when debugging difficult problems. My applications send me an email when an error occurs, and I can diagnose in seconds, then contact the person who saw the error whilst they are wondering what went wrong, then explain what happens. Now that is good customer service. Often it is "Request detected a dangerous value" when they type HTML into text boxes where they shouldn't. The code below goes in global.asax, and logs the error to the event log, and fires off an email when an unhandled application error occurs. Here I use the standard "Application" event log, but you can create your own, it just means you have to create a new category on the server. Whilst it is possible to code that, your code should not have sufficient permissions to make such a change. It is a straightforward implementation of the concept, which works. There is an Application Block for logging as well if you need some heavy duty logging.
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim ErrorMessage As String = WriteError()
SendEmail(ErrorMessage)
Application("AppName") = ConfigurationSettings.AppSettings("AppName")
End Sub
Function WriteError()
Dim HeaderInfo As String = "MyApp ERROR, "
Dim Message As String = HeaderInfo
Try
Try
Dim ex As Exception = Server.GetLastError().GetBaseException()
Message = Message & "MESSAGE: " & ex.Message &
ControlChars.Lf & "SOURCE: " & ex.Source & ControlChars.Lf
& "FORM: " & Request.Form.ToString() & ControlChars.Lf
& "QUERYSTRING: " & Request.QueryString.ToString() &
ControlChars.Lf & "TARGETSITE: " & ex.TargetSite.ToString &
ControlChars.Lf & "STACKTRACE: " & ex.StackTrace &
ControlChars.Lf & EventLogEntryType.Error
Message = Message & ControlChars.Lf & "Server : " & _
Request.ServerVariables("SERVER_NAME")
WriteToErrorLog(Message)
Catch
End Try
Finally
WriteError = Message
End Try
End Function
Sub WriteToErrorLog(ByVal Message As String)
Try
Dim anEventLog As New System.Diagnostics.EventLog
anEventLog.Source = "Application"
If anEventLog.SourceExists("Application") Then
anEventLog.WriteEntry(Message, _
System.Diagnostics.EventLogEntryType.Error)
End If
Catch ex As Exception
End Try
End Sub
Sub SendEmail(ByVal ErrorMessage As String)
Dim aMM As New MailMessage
aMM.Body = ErrorMessage
aMM.To = _
ConfigurationSettings.AppSettings("ErrorReceiverEmailName").ToString
aMM.Subject = "Error in MyApp at " & Now.ToString
aMM.From = _
ConfigurationSettings.AppSettings("ApplicationEmailName").ToString
SmtpMail.SmtpServer = _
ConfigurationSettings.AppSettings("MailServerName").ToString
Try
SmtpMail.Send(aMM)
Catch ex As Exception
End Try
End Sub
Sub WriteToInfoLog(ByVal Message As String)
Dim anEventLog As New System.Diagnostics.EventLog
anEventLog.Source = "Application"
If anEventLog.SourceExists("Application") Then
anEventLog.WriteEntry(Message, _
System.Diagnostics.EventLogEntryType.Information)
End If
End Sub
5. Get an RSS Feed Reader
RSS readers are great. It is a quick way of browsing lots of sites very quickly and finding out if anything useful has been posted. Get an RSS Reader, set up MSDN and CodeProject for a start. Make it part of your daily routine. Also, there is a Webcast on building your own (this is one of those cross-reference synergistic things).
6. Use Skype
Talk to your clients and other developers, as often as you like, for free! You can have conference too, which is good for brainstorming lots with other developers, or having meetings. I don't get any commission for this, but I am an avid user. I have saved a fortune on the company's and my own phone bill. You will need broadband, but it will probably more than pay for the broadband connection itself. This also really opens up the option of working from home as you can have conference meetings and brainstorming sessions through the day.
7. Automate deployment
I use batch files for deployment, to strip code out, have an update package for clients, and have a log of changes. The batch file below copies everything from your designated directory to another location, with a date and time stamp. It also removes any unnecessary files. A complete setup project is great first time, but when you maintain your own web applications and are updating frequently without overwriting settings, becomes a headache. The approach below works very well for me. Include it with the source for your project so that it becomes part of source control. This is because, every time you use it, you should set the date to today, so next time you run it, it picks up the correct files.
@echo ............STARTING DEPLOYMENT .................
@echo note this batch file is primarily intended for redeployment
during development
@echo it creates a local folder with the current date and time,
which contains all the files updated to a given date
rem get the date and build a variable
set newdate1=%DATE:~0,2%
set newdate2=%DATE:~3,2%
set newdate3=%DATE:~6,4%
set time1=%TIME:~0,2%
set time2=%TIME:~3,2%
set DatePart=%newdate2%-%newdate1%-%newdate3%at%time1%%time2%Hr
SET deploypath=D:\deployments\MyApp\%DatePart%
@echo lets clean the deployment path - ONLY FOR LOCAL DEPLOYMENTS
rem del %deploypath%\*.* /s /q /f
SET sourcepath=C:\Inetpub\wwwroot\MyApp
@echo my deployment path is : %deploypath%
@echo copying all files from %sourcepath% to %deploypath% modified
after the date specified. You should update this date to be the current
date after you run the batch file. The command below pulls out every file
modified on or after xmas day 2004.
xcopy %sourcepath%\*.* %deploypath%\*.* /S /R /Y /D:12-25-2004
>>deployresults%DatePart%.txt
@echo deleting files we dont want
del %deploypath%\batch /s /q /f
del %deploypath%\class /s /q /f
del %deploypath%\*.vb /s /q /f
del %deploypath%\*.aspx.resx /s /q /f
del %deploypath%\*.aspx.vb /s /q /f
del %deploypath%\*.scc /s /q /f
del %deploypath%\*.vbproj /s /q /f
del %deploypath%\*.vbproj.vspscc /s /q /f
del %deploypath%\*.ascx.resx /s /q /f
del %deploypath%\*.sln /s /q /f
del %deploypath%\*.vspcc /s /q /f
del %deploypath%\*.vssscc /s /q /f
del %deploypath%\styles.css /s /q /f
del %deploypath%\*.vsdisco /s /q /f
del %deploypath%\*.resx /s /q /f
del %deploypath%\*.pdb /s /q /f
del %deploypath%\*.webinfo /s /q /f
rem delete project specific files/dirs
rmdir %deploypath%\batch /s /q
rmdir %deploypath%\class /s /q
@echo Do not deploy the web.config file please for an update -
update manually, renamed below
rename %deploypath%\web.config web.config.new
@echo ............FINISHED.................
8. Use WebControls
Lots of great free web controls are out there. Use them! I don't want to get into web control development, but here are some more useful online resources to leverage.
- Need a better calendar control than the standard ASP.NET control? Try eworld.
- Need a text editor? Use freetextbox.
- Need a spell checker? Go no further than NetSpell.
Many many more resources out there, these are just ones that I've come across recently. Really just wanted to emphasize the point that, it is good to check whether some already in existence can be leveraged before you start coding.
References
History
First draft created on 21st April 2005 by Paul Heap.
Spent my whole life developing, having worked in C++, Delphi, ASP, then finally settling down to working solely in ASP.Net. Also a forex day trader.