473,698 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Design Decision - Static vs. Non Static Classes

In consideration of a "Utilities" class that contains methods intended to be
used amongst multiple projects...

Is there any general consensus on whether such a class should be static? I
have traditionally and somewhat thoughtlessly created these sorts of classes
as static... but now that I'm refactoring a number of apps written over the
past few years I'd like to make a good decision on this particular issue.

And yes - I know there is no "right vs. wrong" way to go here - so I'd
appreciate some considerations both for and against static classes (in the
case of utilities or DAL).

Thanks
Aug 21 '07 #1
6 1579
Hi,

"Smithers" <A@B.comwrote in message
news:eg******** *********@TK2MS FTNGP05.phx.gbl ...
In consideration of a "Utilities" class that contains methods intended to
be used amongst multiple projects...

Is there any general consensus on whether such a class should be static? I
have traditionally and somewhat thoughtlessly created these sorts of
classes as static... but now that I'm refactoring a number of apps written
over the past few years I'd like to make a good decision on this
particular issue.
Well, take a look at how those "utilities" classes are defined in the
framework, take a look at for example Math, File, Directory, Environment,
etc


Aug 21 '07 #2
It depends. If the class functions can be used as standalone with no
internal data that has to hang around then static works well. On the other
hand, if the class will have data members that will hang on to information
between method calls then static is probably not such a good idea.
--
Richard Lewis Haggard
General: www.Haggard-And-Associates.com

Please come visit here for a couple thousand good giggles!:
www.haggard-and-associates.com/Humor/humor.htm
"Smithers" <A@B.comwrote in message
news:eg******** *********@TK2MS FTNGP05.phx.gbl ...
In consideration of a "Utilities" class that contains methods intended to
be used amongst multiple projects...

Is there any general consensus on whether such a class should be static? I
have traditionally and somewhat thoughtlessly created these sorts of
classes as static... but now that I'm refactoring a number of apps written
over the past few years I'd like to make a good decision on this
particular issue.

And yes - I know there is no "right vs. wrong" way to go here - so I'd
appreciate some considerations both for and against static classes (in the
case of utilities or DAL).

Thanks

Aug 21 '07 #3
And how would the use of delegates affect the use of the utility class
members?

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee. com
URL http://clintongallagher.metromilwaukee.com/
"Richard Lewis Haggard" <HaggardAtWorld DotStdDotComwro te in message
news:O$******** ******@TK2MSFTN GP03.phx.gbl...
It depends. If the class functions can be used as standalone with no
internal data that has to hang around then static works well. On the other
hand, if the class will have data members that will hang on to information
between method calls then static is probably not such a good idea.
--
Richard Lewis Haggard
General: www.Haggard-And-Associates.com

Please come visit here for a couple thousand good giggles!:
www.haggard-and-associates.com/Humor/humor.htm
"Smithers" <A@B.comwrote in message
news:eg******** *********@TK2MS FTNGP05.phx.gbl ...
>In consideration of a "Utilities" class that contains methods intended to
be used amongst multiple projects...

Is there any general consensus on whether such a class should be static?
I have traditionally and somewhat thoughtlessly created these sorts of
classes as static... but now that I'm refactoring a number of apps
written over the past few years I'd like to make a good decision on this
particular issue.

And yes - I know there is no "right vs. wrong" way to go here - so I'd
appreciate some considerations both for and against static classes (in
the case of utilities or DAL).

Thanks


Aug 22 '07 #4
clintonG wrote:
And how would the use of delegates affect the use of the utility class
members?
From the perspective of keeping everything involved in the process
inside the method, I don't really see any issues doing it this way -- as
long as the delegate itself follows the same rule and only tries to act
on things it has been provided. You might get into threading issues if
you try and do anything beyond that.

Chris.
Aug 22 '07 #5
On 2007-08-21, Smithers <A@B.comwrote :
Is there any general consensus on whether such a class should be static? I
have traditionally and somewhat thoughtlessly created these sorts of classes
as static... but now that I'm refactoring a number of apps written over the
How do you want to test the class? And how would you test stuff that
uses these static methods? (Mocking seems hard to achieve)
--
Kind regards,
Tim Van Wassenhove <url:http://www.timvw.be/>
Aug 22 '07 #6
Tim Van Wassenhove <ti***@newsgrou p.nospamwrote:
Testing the static method itself isn't that hard. Testing methods that
use this static method is what can become difficult.
Indeed, if it's doing something complicated or using an external
resource. In that case it's often worth encapsulating the behaviour in
an interface and using dependency injection or a replacable factory
etc.

Most static classes I've written haven't included such behaviour,
however - they've been simple utility methods.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 23 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1740
by: Danimal | last post by:
I have been using PHP for a long time... since it was called PHP/FI. I have a programming design question: Let say I have this class: class attrib { var $lenght; var $type; ... }
36
6383
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but something I'll need in this case is some experience-based set of rules about how to use python in this context. For example... is defining readonly attributes in classes worth the hassle ? Does duck-typing scale well in complex
3
3143
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
11
3180
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to the Data Access Layer method -or- 2) Pass some kind of a unique id to the Data Access Layer method
3
1423
by: Amadelle | last post by:
Hi All and thanks in advance, I wanted to know when is a good idea to use a static class (with static constructor) and when to use instance classes? I have read couple of articles on line and the general example is when you want to write a log file use static class ...but they don't say why? My specific question is that I have a validation class with some data validation methods which will be called through out the program and I am...
4
1332
by: news.microsoft.com | last post by:
Hello, I've got a design problem that I can't figure out. I need to override a static method which happens to be referenced by methods inside nested classes. In the sample below, in the call tst.runTest() I need the result to display "child", however it always displays "parent". I can understand all the reasons why it should display "parent" and not "child" but that doesn't help me solve it. In my real-world problem the parent...
20
1517
by: Brad Pears | last post by:
I am completely new to vb .net. I am using visual Studio 2005 to redo an Access 2000 application into a .net OO application using SQL Server 2000 - so a complete rewrite and re-thinking of how this app will work. I have NEVER done any OO programming at all although I have used OO techniques in programs of course - just never actually designed the classes etc... So I am just a tad nervous in re-writing this Access application as it is...
9
2819
by: Grizlyk | last post by:
Somebody have offered std colors to C++ in the msg here: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/2e5bb3d36ece543b/1acf6cd7e3ebdbcd#1acf6cd7e3ebdbcd The main objection to the including "colors markup" is useless of the marks, the marks is unneccessary extra information. The most used way to make colored view of C++ programs is usage of classes of C++ language words (already included in C++): reserved words, user...
4
1179
by: =?Utf-8?B?Um9i?= | last post by:
I've a number of objects (classes) defined in an application which I'm using to model packets on a network. I have a class hierarchy of Packet (inherited by) EthernetPacket (inherited by) IPPacket (inherited by) TCPPacket. I've some control logic which takes in a data stream and determines which kind of packet to create. Should this logic be within the constructors of the packet classes - so I create a Packet and within the contructor of...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.