tags:

views:

837

answers:

5

What are the downsides to using static methods in a web site business layer versus instantiating a class and then calling a method on the class? What are the performance hits either way?

+10  A: 

The performance differences will be negligible.

The downside of using a static method is that it becomes less testable. When dependencies are expressed in static method calls, you can't replace those dependencies with mocks/stubs. If all dependencies are expressed as interfaces, where the implementation is passed into the component, then you can use a mock/stub version of the component for unit tests, and then the real implementation (possibly hooked up with an IoC container) for the real deployment.

Jon Skeet
It depends. The System.Math static class doesn't seem less testable than it would have been if it were non-static. Am I wrong, Jon?
Andrei Rinea
It's not that the static method itself isn't testable - it's that things *using* static methods aren't as testable, because you can't replace the static method's implementation with a simple one. For instance, if the method hits the database, your test will too.
Jon Skeet
A: 

If the method uses static data, this will actually be shared amongst all users of your web application.

Code-only, no real problems beyond the usual issues with static methods in all systems.

Cade Roux
+3  A: 

Jon Skeet is right--the performance difference would be insignificant...

Having said that, if you are building an enterprise application, I would suggest using the traditional tiered approach espoused by Microsoft and a number of other software companies. Let me briefly explain:

I'm going to use ASP.NET because I'm most familiar with it, but this should easily translate into any other technology you may be using.

The presentation layer of your application would be comprised of ASP.NET aspx pages for display and ASP.NET code-behinds for "process control." This is a fancy way of talking about what happens when I click submit. Do I go to another page? Is there validation? Do I need to save information to the database? Where do I go after that?

The process control is the liaison between the presentation layer and the business layer. This layer is broken up into two pieces (and this is where your question comes in). The most flexible way of building this layer is to have a set of business logic classes (e.g., PaymentProcessing, CustomerManagement, etc.) that have methods like ProcessPayment, DeleteCustomer, CreateAccount, etc. These would be static methods.

When the above methods get called from the process control layer, they would handle all the instantiation of business objects (e.g., Customer, Invoice, Payment, etc.) and apply the appropriate business rules.

Your business objects are what would handle all the database interaction with your data layer. That is, they know how to save the data they contain...this is similar to the MVC pattern.

So--what's the benefit of this? Well, you still get testability at multiple levels. You can test your UI, you can test the business process (by calling the business logic classes with the appropriate data), and you can test the business objects (by manually instantiating them and testing their methods. You also know that if your data model or objects change, your UI won't be impacted, and only your business logic classes will have to change. Also, if the business logic changes, you can change those classes without impacting the objects.

Hope this helps a bit.

Ed Altorfer
+3  A: 

Performance wise, using static methods avoids the overhead of object creation/destruction. This is usually non significant.

They should be used only where the action the method takes is not related to state, for instance, for factory methods. It'd make no sense to create an object instance just to instantiate another object instance :-)

String.Format(), the TryParse() and Parse() methods are all good examples of when a static method makes sense. They perform always the same thing, do not need state and are fairly common so instancing makes less sense.

On the other hand, using them when it does not make sense (for example, having to pass all the state into the method, say, with 10 arguments), makes everything more complicated, less maintainable, less readable and less testable as Jon says. I think it's not relevant if this is about business layer or anywhere else in the code, only use them sparingly and when the situation justifies them.

Vinko Vrsalovic
"It'd make no sense to create an object instance just to instantiate another object instance :-)" - it does if you need to tell someone how to create instances of a certain type in a mockable/pluggable way. That's the Factory pattern.
Christian Vest Hansen
I'm talking about the Factory method pattern there...
Vinko Vrsalovic
A: 
  1. Testability: static dependencies are less testable
  2. Threading: you can have concurrency problems
  3. Design: static variables are like global variables
ema