Page 1 of 1

Patterns & Practices : Static Methods

Posted: 25 May 2009, 09:44
by GrimStoner
I've just installed DevXpress CodeRush/Refactor Pro, which analyzes your code as you go along, and gives suggestions as to improving it.

In one of my existing projects, one that pops up quite often is "Member can be static". Why is it advisable to make all my members static?

Re: Patterns & Practices : Static Methods

Posted: 25 May 2009, 10:14
by rustypup
it most certainly isn't... static instances are really light on RAM, but are commonly open to abuse and are a healthy environment for growing bugs...

Re: Patterns & Practices : Static Methods

Posted: 26 May 2009, 13:50
by -Prometheus-
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................

Re: Patterns & Practices : Static Methods

Posted: 26 May 2009, 13:53
by Ron2K
-Prometheus- wrote:Don't know what language you are using but it certainly should NOT be static unless you know the reason it should be static. Since you're asking this I'm assuming you don't. As you are referring to members I'm also assuming you are talking about classes.

Static member variables exist as instances of a class and not instances of the objects themselves. That is when you declare the class it makes one variable for that class no matter how many objects or if you even have an object of the class. Think of them as global variables that are accessible only by the class and in terms of the class. They are actually global variables with different access rights and are usually used to keep track of how many objects you create and destroy.

Member functions or methods can access static members. Static methods can be accessed without making an instance of the class. Even with non-static methods there's never more than one instance of a method so static methods only change the access rights.
Quoted for emphasis. There are perfectly good reasons to use the static modifier, but if you don't know what they are, you should not be using the modifier.

The language is likely to be either C# or Java, by the way.

Re: Patterns & Practices : Static Methods

Posted: 26 May 2009, 14:04
by rustypup
Ron2K wrote:There are perfectly good reasons to use the static modifier, but if you don't know what they are, you should not be using the modifier.
static final is good for the right reasons... public static is mostly bad outside of event-driven/component code...

yet i see it often... :cry:

Re: Patterns & Practices : Static Methods

Posted: 26 May 2009, 14:06
by RuadRauFlessa
rustypup wrote:
Ron2K wrote:There are perfectly good reasons to use the static modifier, but if you don't know what they are, you should not be using the modifier.
static final is good for the right reasons... public static is mostly bad outside of event-driven/component code...

yet i see it often... :cry:
public static works fine with my Singleton patterned Database connectors I write. Although they are more for class factoring than anything else. I don't use them for much more in any case.

Re: Patterns & Practices : Static Methods

Posted: 27 May 2009, 09:00
by GrimStoner
I wrote a couple of classes that model database rows, and then have a dbrow manager class, that is used to retrieve the data from the database, i.e. an Event class, and then an EventManager class, with GetAllEvents, GetFilteredEvents, AddEvent etc. methods. I don't think that I'll be calling these methods other than with my WCF service. So, is it advisable to have to create an instance of each manager class when I want to get data over the service?

Currently I do this... Silverlight app instantiates a service client, and calls the function to retrieve data. The service then instantiates one of the manager classes, and then calls the GetData method on it. The manager class then gets the data from the db, models each row in the datareader with a private function in the manager class (i.e. ModelDataReaderAsEvent), and returns a list of the class retrieved to the service (i.e. List<Event>).

And it's C#.

Re: Patterns & Practices : Static Methods

Posted: 30 May 2009, 15:57
by -Prometheus-
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................