20 Tips to Improve ASP.net Application Performance

Not a .net Developer?

Are you an asp.net developer?  If you aren’t don’t worry, we have similar posts in the works for Ruby, PHP, and other developers out there.  If you are an ASP.net developer, listen up!

Get Ready for Massive Gains

There are certain things you should take into account when you are developing your applications.  Over the last 12 years or so of working with asp and asp.net, I have learned to avoid and do certain things that increase your application performance by a massive amount!  Below are my top 20 tips to improving ASP.net application Performance.

  1. Disable Session State
    Disable Session State if you’re not going to use it.  By default it’s on. You can actually turn this off for specific pages, instead of for every page:
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    AutoEventWireup="false" Inherits="WebApplication1.WebForm1"
    EnableSessionState="false" %>

    You can also disable it across the application in the web.config by setting the <sessionState> mode value to Off.

  2. Output Buffering
    Take advantage of this great feature.  Basically batch all of your work on the server, and then run a Response.Flush method to output the data.  This avoids chatty back and forth with the server.
    <%response.buffer=true%> 

    Then use:

    <%response.flush=true%> 
  3. Avoid Server-Side Validation
    Try to avoid server-side validation, use client-side instead. Server-Side will just consume valuable resources on your servers, and cause more chat back and forth.
  4. Repeater Control Good,  DataList, DataGrid, and DataView controls Bad
    Asp.net is a great platform, unfortunately a lot of the controls that were developed are heavy in html, and create not the greatest scaleable html from a performance standpoint.  ASP.net  repeater control is awesome!  Use it!  You might write more code, but you will thank me in the long run!
  5. Take advantage of HttpResponse.IsClientConnected before performing a large operation:
    if (Response.IsClientConnected)
    {
    // If still connected, redirect
    // to another page. 
    Response.Redirect("Page2CS.aspx", false);
    }

    What is wrong with Response.Redirect? Read on…

  6. Use HTTPServerUtility.Transfer instead of Response.Redirect
    Redirect’s are also very chatty.  They should only be used when you are transferring people to another physical web server.  For any transfers within your server, use .transfer!  You will save a lot of needless HTTP requests.
  7. Always check Page.IsValid when using Validator Controls
    So you’ve dropped on some validator controls, and you think your good to go because ASP.net does everything for you!  Right? Wrong!  All that happens if bad data is received is the IsValid flag is set to false. So make sure you check Page.IsValid before processing your forms!
  8. Deploy with Release Build
    Make sure you use Release Build mode and not Debug Build when you deploy your site to production. If you think this doesn’t matter, think again.  By running in debug mode, you are creating PDB’s and cranking up the timeout.  Deploy Release mode and you will see the speed improvements.
  9. Turn off Tracing
    Tracing is awesome, however have you remembered to turn it off? If not, make sure you edit your web.config and turn it off!  It will add a lot of overhead to your application that is not needed in a production environment.
    <configuration>
    <system.web>
    <trace enabled="false" pageOutput="false" />
    <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/>
    <compilation debug="false" />
    </system.web>
    </configuration>
  10. Page.IsPostBack is your friend
    Make sure you don’t execute code needlessly. I don’t know how many web developers forget about checking IsPostBack!  It seems like such a basic thing to me!  Needless processing!
  11. Avoid Exceptions
    Avoid throwing exceptions, and handling useless exceptions. Exceptions are probably one of the heaviest resource hogs and causes of slowdowns you will ever see in web applications, as well as windows applications.  Write your code so they don’t happen!  Don’t code by exception!
  12. Caching is Possibly the number one tip!
    Use Quick Page Caching and the ASP.net Cache API!  Lots to learn, its not as simple as you might think.  There is a lot of strategy involved here.  When do you cache?  what do you cache?
  13. Create Per-Request Cache
    Use HTTPContect.Items to add single page load to create a per-request cache.
  14. StringBuilder
    StringBuilder.Append is faster than String + String.  However in order to use StringBuilder, you must
    new StringBuilder()

    Therefore it is not something you want to use if you don’t have large strings.  If you are concatenating less than 3 times, then stick with String + String. You can also try String.Concat

  15. Turn Off ViewState
    If you are not using form postback, turn off viewsate, by default, controls will turn on viewsate and slow your site.
    public ShowOrdersTablePage()
    {
    this.Init += new EventHandler(Page_Init);
    }
    
    private void Page_Init(object sender, System.EventArgs e)
    {
    this.EnableViewState = false;
    }
  16. Use Paging
    Take advantage of paging’s simplicity in .net. Only show small subsets of data at a time, allowing the page to load faster.  Just be careful when you mix in caching.  How many times do you hit the page 2, or page 3 button?  Hardly ever right!  So don’t cache all the data in the grid! Think of it this way: How big would the first search result page be for “music” on Google if they cached all the pages from 1 to goggle ;)
  17. Use the AppOffline.htm when updating binaries
    I hate the generic asp.net error messages!  If I never had to see them again I would be so happy.  Make sure your users never see them!  Use the AppOffline.htm file!
  18. Use ControlState and not ViewState for Controls
    If you followed the last tip, you are probably freaking out at the though of your controls not working.  Simply use Control State.  Microsoft has an excellent example of using ControlState here, as I will not be able to get into all the detail in this short article.
  19. Use the Finally Method
    If you have opened any connections to the database, or files, etc, make sure that you close them at the end!  The Finally block is really the best place to do so, as it is the only block of code that will surely execute.
  20. Option Strict and Option Explicit
    This is an oldy, and not so much a strictly ASP.net tip, but a .net tip in general.  Make sure you turn BOTH on.  you should never trust .net or any compiler to perform conversions for you.  That’s just shady programming, and low quality code anyway.  If you have never turned both on, go turn them on right now and try and compile.  Fix all your errors.

There are hundreds more where these came from, however I really feel that these are the most critical of the speed improvements you can make in ASP.net that will have a dramatic impact on the user experience of your application.  As always if you have any suggestions or tips to add, please let us know!  We would love to hear them!

Have web development!

 

Software Development in the Real World Podcast!

Miguel and Erin Carrasco
First a Big Thank You!

A big welcome to all the new readers via Digg, Technorati, DZone, and del.icio.us!  Thanks for all your support, and great comments over the last few weeks.  For those that are not aware, Software Development in the Real World has grown nearly 1,000 percent in traffic over the last two weeks! 

I’m humbled, and excited to have all of these new readers, and look forward to feeding your thirst for knowledge over the next few years!  Although we are very far in software development, we still have even further to go!  It’s exciting times to be in this industry!  Lets share our knowledge together, grow together, and live with undeniable passion for software development and our quality of lives!

Help Spread the word!

If you have not done so yet, help spread software development by adding this blog as your Technorati favorite! We started a contest to give away a free iPod once we are number 1 on Technorati!  We are actually nearly at the top 100!  we only need about 200 more favorite additions to make it!  Spreading the word will help us get better speakers, guest blogger’s, and help make this blog a really awesome place to visit daily!

Software Development Podcast Coming Soon!

That’s right!  Software Development in the Real World will be introducing a brand new podcast in the next few weeks!  We are really looking forward to starting the Software Development in the Real World podcast! We are planning on interviewing some of the most amazing software developers, managers, CEO’s, and founders across the software industry.

Another great feature is you will see in the coming weeks is audio enabled RSS feeds!  This will allow you to easily subscribe to our audio RSS feed, and listen to our blog on your iPod or other MP3 player!  My lovely wife Erin will be voicing the new audio enabled RSS feeds!

If any of you have specific software developers, architects, CEO’s, or blogger’s you would like to see featured, please let us know via comments below!  All podcasts will also be converted to text for your downloading pleasure.

 

Speed Up Your Software Development in 10 Steps

Speed Up Software Development

While many of you are great software developers, you can always use a boost in speed!  Here are some tips that I try and instill in my software development team.

Learn the Keyboard Shortcuts

The mouse was one of the greatest inventions to come out of Palo Alto, however, it has made some of us a little too dependent on this great little invention.  How many times are you moving it around to find the file menu, or the tools menu, then moving along to find something else?  All this time can be saved by using the keyboard shortcuts.  Depending on your development environment, you should really make an effort to learn to do everything with your keyboard.

One way to do this, is set aside an hour when you will only use the keyboard to do your development.  This means no mouse movements or help from your little friend at all.  If you can train yourself to use your keyboard for most things and your mouse less, you will become extremely fast at development, and will be labeled the keyboard cowboy at your office.

Remove Unnecessary Dependencies

As your software expands and grows, the build process will start taking longer and longer. This will start affecting your productivity by making you sit there and wait for things to finish.  A large part of this process is caused by dependencies that are not needed.  Make sure you clean them up.  There are a lot of great tools to help you with this process, suggesting and guiding you along the way. 

Make sure you research these tools, but at the end of the day, just make sure you keep things clean on your own.  By removing these unwanted and unused dependencies, you will notice a huge improvement in speed of build, and even the overall software application.

Setup a Continuous Integration Build Environment

This tip will speed up your development time astonishingly!  One of the areas that always takes the longest is the code integration between different pieces of code, and different developers.  Software developers go and develop some great pieces of the software, time comes to put out a new version of the software, or a patch, and everyone scrambles to give the build master their new code files. 

Poor build master tries the build, and everything fails.  The software development team then spends the rest of their time for a week making sure everything works well together, and compiles.  Then come all the bugs, and it’s just a bad situation all around.  Setup a continuous integration build environment and enjoy the process of builds!

Put Source Control in Place

Source control is extremely important to any project, without it, you are going to develop slowly.  Period.  I’ve talked about this before, because it is extremely important.  It’s the foundation to your project.  Even if you are coding a small project, make sure it’s in source control.  Source Control will enable you to have a history, a journal, of you project. 

Let’s say at some point someone wanted to roll back a certain screen to a previous design, or worse yet, a huge bug was found with the current version.  What do you do?  Well if you don’t have source control, you are in big trouble.

Write Unit Tests

By adding test cases to your software development, you will speed yourself up by not allowing things to be changed.  In a future article I will explain the best way to create these unit tests, but here are some of the basics for those of you that have heard about unit tests, but haven’t had time to implement them into your software development lifecycle. 

There are a few common types of unit tests you will want to start with: Validation, Bad Input, and Code Path unit tests.  The main idea here is you will be writing these automates tests, that will run every time you check in new code, thanks to our source control and automated build environment combination we setup in the previous steps.

Remember the golden rules: If it’s broken, fix it!  If it passed, and it still broke, create the unit test.  If it’s hard to write the unit test, make your code easier to work with. If you need to do setup before executing the test, do not copy paste from the application.

Use Code Coverage Tools

Code coverage tools are fantastic and go hand in hand with unit tests.  If you want to know how much code your tests really exercise, or want to ensure that the outsourced code is tested, code coverage tools are for you.  Code coverage tools will allow you to understand what parts of your application are well tested and what is not tested.  Some of the best tools out there not only measure line coverage, but also measure branch & statement, and methods.

Become One with the Debugger

Becoming a debugger master will really speed things up for you.  I’m not going to spend a lot of time on this as its very basic, but it is far to important to not mention.  Learn all the basic and advanced features that are available to you in your debugger. Breakpoints, Watches, and Stepping will save you countless amounts of time.

Write Reusable Code

When you are writing code, don’t just write code for the problem at hand.  Write code for thinking about the future! Create classes, and put things together into reusable custom frameworks and libraries.  How to write reusable code is another article, however just make sure that each of your classes or methods does one thing.  Try and reduce the coupling in your code.  Lastly, make sure you are always thinking “modular”.

Take Advantage of Frameworks

There are hundreds of thousands of frameworks out there for you to use.  Pick one that suites your needs, and use it.  The main reason for the creation of software development frameworks is to allow software developers to focus on the software requirements, and less time on the low level details.  There are a slew of excellent frameworks out there, pick the area you need, select a framework, and you are off to the races.

Ensure Proper Testing and Quality Control

Do you have bug tracking software?  Do you have a process that works for finding bugs, reporting user issues, logging all feature enhancements?  If you don’t, chances are you will be spending a lot of your time trying to keep track of all of these times.  Set yourself up for success.  Invest in some great bug tracking software.  Some of my favorites are Joel Spolsky’s FogBugz, and Axosoft OnTime.

I love tools, and over the next few weeks I will start sharing with you some of the tools that we use to make our developers fly through the software development lifecycle.  Stay tuned.

 

Ultimate Server-Side Web Development Cheat Sheets

Even before the other Ultimate Web Development Cheat Sheet Guide became popular on Digg and del.icio.us, I was working on another version focused around Server-Side technologies.  I was going to add them into the other list, but removed them.  I wanted to have a list that was more focused, and only had the best Server-Side Cheat Sheets.  Also please note while there are hundreds of cheat sheets for each area, I try and only list at most the top 10 in each area, otherwise it makes the guide useless with repeated data. This makes these guides much more useful than the old guides out there.

PHP

Jack Daniel’s PHP Cheat Sheet (HTML)

Interactive PHP Cheat Sheet (HTML)

Blue Shoes Developer – PHP Cheat Sheet (HTML)

PHP Cheat Sheet (HTML)

PHP Cheat Sheet (HTML)

Regular Expressions Reference Sheet (HTML)

Tiger PHP Cheat Sheet (PDF)

PHP 4 Reference Card (PDF)

PHP Templates Cheat Sheet (PDF)

Ruby

Ruby Cheat Sheet (PDF)

Jack Daniel’s Ruby on Rails Cheat Sheet (HTML)

Textmate Rails Cheat Sheet (PDF)

Zen Spider Ruby Quick Reference (HTML)

Rails Reference 1.1 (PDF)

Rails Active Resource Cheat Sheet (PDF)

ActiveRecord Relationships (PDF)

Rails Strings Cheat Sheet (PDF)

What Goes Where Cheat Sheet (PDF)

Ruby on Rails Cheat Sheet (PDF)

Ruby on Rails Form Helpers (PDF)

Perl

Perl Quick Reference (PDF)

Perl Cheat Sheet (HTML)

Perl 5 Cheat Sheet (HTML)

Perl Testing Reference Card (PDF)

Perl Quick Reference Card (PDF)

ASP.net

ASP.net Page Lifecycle Diagram (PNG)

.NET Format String Quick Reference (PDF)

ASP.NET 2.0 Page Life Cycle & Common Events (PDF)

Visual Studio 2005 Built-in Code Snippets (C#) (PDF)

Visual Studio 2005 Default Keybindings C# (PDF)

Visual Studio 2005 Default Keybindings VB.net (PDF)

ASP.NET AJAX Client Life Cycle & Events (PDF)

VB.net and C# Comparison (PDF) (Word)

Casting in VB.net and C# (HTML)

ASP.net Basics (PDF)

Python

Python 2.4 Cheat Sheet (HTML)

Python 2.2 Quick Reference (HTML)

Python Cheat Sheet (HTML)

Python 101 Cheat Sheet (HTML)

Python PHP Cheat Sheet (PDF)

Python Quick Reference (PDF)

ColdFusion

ColdFusion Reference Sheet (PDF)

ColdFusion Cheat Sheet (HTML)

ColdFusion Quick Reference (HTML)

Java / JavaServer Pages

JavaServer Pages Syntax (PDF)

JSP 2.0 XML Cheat Sheet (HTML)

Java Cheat Sheet : Java Glossary (HTML)

Java Reference Sheet (PDF)

MySQL

Jack Daniel’s MySQL Cheat Sheet (PDF) (PNG)

MySQL Reference Card (PDF)

Neal Parikh MySQL Cheat Sheet (HTML)

MySQL Reference Sheet (PDF)

Handy Cheat-Sheet of MySQL Commands (HTML)

MySQL Commands (HTML)

SQL Injection Cheat Sheet (HTML)

SQL Server

Jack Daniel’s SQL Server Cheat Sheet (PDF) (PNG)

A to Z SQL Server 2005 (HTML)

SQL Injection Cheat Sheet (HTML)

 

 

I’m on a Mission, and Need Your Help

Software Development Should be Number 1

I woke up this morning, and I was thinking, wouldn’t it be cool if a Software Development Blog became number 1 on the Technorati Top 100?  After all, without software developers and software development, we would not have the Internet at all.  Right now, the top sites are “Boing Boing” and a site about making money!  Let’s change that!

If you have spent some time on my blog, you will see the purpose of this blog is to improve software development, and more importantly, try and help everyone improve their lives.  Help me spread the word, and let’s put Software Development on the top of Technorati!

Win an iPod

To motivate everyone and put everyone into action, I will be giving away a free Apple iPod (80GB Brand New!) when Software Development in the Real World makes it to the top spot!  With the amount of readers we have and daily visitors, this should take no time at all!  All it requires is 2,700 or so people to add the site, and it takes less than one minute to do it!

What to do.

  1. Click here to add this site as your Technorati favorite.
  2. Once you have added the site, just leave a comment behind with your email address.
  3. When Software Development in the Real World is number one, I will add all the valid email address’s in the comments into a random draw application, and pick the winner.
  4. The winner will be emailed, and I will send them a shiny free iPod!

Pretty sweet eh!

I will be checking hourly over the next few days, and the second we are number one, I will post the winner on my blog!

Add Software Development as your favorite here.