C#.NET code reuse best practice

I have a class called Address which contains a method called IsPoBoxAddress.

public class Address
   public bool IsPoBoxAddress()
      string[] aPOBox = {"po box", "p.o. box", ..., ..., etc. };
      foreach ( string address in aPOBox)
           if ( Address1.ToLower() )   // Address1 is a class member
                return true;
       return false;


This class and method is usually accessed through an instance member like so:

CustomerAccount acct = FindAcctById(12345);

foreach(Address addr in acct.Addresses)
   if (addr.IsPoBoxAddress())
      // do something

I have a string that contains an address. The string is not part of any CustomerAccount or Address object -- just a stand alone string variable. How do I reuse the logic in IsPoBoxAddress to test if my new string contains a PO Box address?

I thought about making the IsPoBoxAddress() method static and passing an optional string parameter like below. Is there any problems with this?

static bool IsPoBoxAddress( string addr = null )
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Making the function static would certainly work presuming that the function logic is fully encapsulated and does not require other non-static functions to be available.

..but is "static" the right choice?

does it matter?  I mean, what are the chances that another developer is going to use this code?  Heck what are the chances that another developer (or team of developers) will even see this code, let alone use it?

I used to spend a *lot* of time making sure my applications were well architected with interfaces and reusable code, etc, etc.  98% of the time I was the only person who ever saw the code and half the time it was never reused.

After 15 years of programming there are only two pieces of code that use over and over (and are very well written) - reading data from a Datasource, and writing log information out to a log file or event viewer.  That's it.

I still write fairly well structured code (because, hey I have pride in my work), but for all intents 90% of what I do could be spaghetti code and as long as it worked and was efficient no one would care - nor would they even see it.
pzozulkaAuthor Commented:
In this case, I'm that other programmer that needs to reuse that piece of logic contained within the IsPOBoxAddress() method.

The programmer that created that logic, placed it inside a member method of an Address class.

So as things stand right now, the only way to use that logic is if you have an Address instance.

Since I don't (I just have a standalone string), I somehow need to modify his method so that it is available for reuse, and am wondering on what would be the best approach.

Here's why I'm hesitant on making it static. Inside the logic, it uses "Address1" which is not a static member of that same class. It's a regular member. So I don't think this approach will work -- now that I'm thinking about it.
If that's the case, then you will need to create an Address instance, perform your test and then destroy the address object.

Kinda sucks that you have to eat up those resources like that, but it's not always clean.

Your other alternative is to create a stand alone static override  - create it completely from scratch for your own personal use and copy/paste the original code in the static method but allow the address1 to be passed in instead of using the instance var

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.