Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Which is best practice and why

Posted on 2006-05-04
5
Medium Priority
?
244 Views
Last Modified: 2010-04-16
Which is best practice

namespace test
{

Class xmlProcessor
{
   BusinessLogicLayer  objClass=new BusinessLogicLayer
   objClass.hello()
 }

Class BusinessLogicLayer
{
     private void hello{}
}
}

OR

namespace test
{

Class xmlProcessor
{
   BusinessLogicLayer.hello()
 }

Class BusinessLogicLayer
{
     internal static void hello{}
}
}

0
Comment
Question by:PradeepYadhav
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Fahad Mukhtar
ID: 16604695
IMO it depends heavily on the functionality of hello() method!
0
 
LVL 19

Accepted Solution

by:
Fahad Mukhtar earned 750 total points
ID: 16604711
the best practice (regardless of the functionality of hello() method) is the first one as it more follow the Object oriented design paradiagm more than the 2nd one.

static methods should be avoided unless there is some requirement to use them
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16604843
Yeah it really truly depends on what that method does and even how that class works.  Most often you need an instance of a class to make it work correctly.  Keep in mind that if you make static method it can't access instance data.  Also if that static method can access static members then you need to make the method thread safe.

There is no right or wrong it just depends on what characteristics your class needs and that is most often determined by functionality (for example I would not make a connection pool an instance class) but sometimes to simply make it convenient like,

double result = Math.SqureRoot(423);
is easier to use than
Math math = new Math();
double result = math.SqureRoot(423);
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 16604860
Hi...

I usually don't use static objects on the BusinessObjects (BO) Layer just because It usually doesn't make cense.
On the other hand, my DataLayer is usually structured by static classes obviouslly with static members.

Why... lets see...

A BO object represents something that has members that are directly related with that object instance.
For example, I can have an object called Customer that represents a... CUSTOMER!!! :)
with some properties... lets say... Name, Address, BirthDate...
and some methods like... LOAD, SAVE, DELETE
None are static... they are relate to that instance of the object Customer.

On the other hand, on the DataLayer (DL), I'll have another object, also called Customer, thet will have 3 static members:
LOAD, SAVE and DELETE.
All of them receive an BO instance of the Customer object and do their work.
They are static because they're not attached to a specific instance of any customer object, they are generic, behave just the same with every customer instance.


This is how I use static members on a simple n-tier aplications... there are more complicated scenarios but this one is a good sample.
I'm not saying that you can't use static member on any other layer... even on the UI, you just have to see if it makes cense.

Alex :p
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16606156
um thanks for accepting that rather worthless answer.

it has NOTHING to do with object oriented design.  a cite avoiding them but no reason as to why
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Loops Section Overview
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question