4444
4444

Reputation: 3680

Using an entire Class as a Parameter - Good practice?

Suppose a class called "Place" stores locations and relevant details:

class Place
{
    string Name;
    string Location;
    // etc...
}

Later on we populate the name and details of up to 80 different towns. So we store these Towns numerically in an array:

class Setup
{
    public static GetTowns
    {
        for (int i = 1; i <= numberOfTowns; i++)
        {
            Town[i] = new Place(name, location);
        }
        // more code...
    }
    // more methods...
}

To access a specific Town and its data, we pass the Town itself as a parameter and receive it:

public static void DescribeTown(Place Town)
{
    // example method from some other class
    Console.WriteLine("Shipping to {0}.", Town.Name);
}

Other methods need access multiple towns, or all of them. Then we can pass in the entire Town array as a parameter:

public static void ListAllTowns(Place[] Town)
{
    Console.WriteLine("Our Beloved Clients:");
    foreach (Place here in Town)
    {
        Console.WriteLine("{0} in {1}", here.Name, here.Location);
        // Various other operations requiring info about the Towns
    }
}

The Complete Reference, C# 2.0, states the following

Parameters are variables that receive the value of the arguments passed into the method when it is called.

This seems to say every instance of a class, and all data involved, is being passed into ListAllTowns each time it is called. That sounds wasteful to me, but is it?

(This example is heavily simplified for proof-of-concept reasons, but methods require read/write permissions and comparison between multiple towns.)

Upvotes: 5

Views: 3372

Answers (6)

Konstantin Chernov
Konstantin Chernov

Reputation: 1936

It's always a dilemma.

On one hand - Occam's Razor saying that you dont need anything excessive, on onther - changing DescribeTown(Place Town) to DescribeTown(string TownName) means doing it not OOP-way, and that, in turn, means that's the code would be harder to change and to maintain in future.

But sometimes when system is matured and stable and when it turns out that the whole Town was still excessive, you can refactor it back to DescribeTown(string TownName).

One more aspect - unit-testing. Easier to test DescribeTown(string TownName) method than DescribeTown(Place Town), the latter requires a Town object to be mocked, and it's not obvious what properties you should mock (you have to know it before you test).

Upvotes: 1

Dallas Caley
Dallas Caley

Reputation: 5818

My opinion is that it is perfectly fine to pass a class as an argument. This is backed up by various authors including Robert Martins Clean Code. I also believe thought that you should avoid passing parameters at all if possible. What if, instead of passing an array of Town objects to the ListAllTowns method, you made "ListAll" a method of the Place class, then you would call the method like this:

Towns.ListAll()

In this sense you would not have to pass the list to the function everytime it is used because it would simply be operating on it's owning object.

Upvotes: 0

corrego
corrego

Reputation: 327

In .NET, objects are kept around as references. In this case, you are not passing around or copying the whole data, but references to an already created instance of that data.

In this context, passing whole instances around is encouraged, because it allows you to keep consistency (it makes sense that a method that deal with Towns get Towns as a parameter)

Upvotes: 1

Witold Sosnowski
Witold Sosnowski

Reputation: 506

All classes in C# are references by default, so only a pointer is being send to function.

Answering to your question: it depends. Sometimes it actually does make sense to send a whole object as a parameter (i.e. we are trying to something with an object). On the other hand, creating classes only for function parameters is probably not nice. In your particular example it does make sense to create a class.

One exception from what I've just told: structs are being sent as a copy (all members will be copied). You must be careful while sending these as function parameters.

Upvotes: 2

Guffa
Guffa

Reputation: 700212

Parameters are always passed by value (unless the ref or out keywords are used), but for a reference type the value is not the object itself, but the reference to the object.

So, the entire object is not passed as a parameter, only a reference to the object.

Upvotes: 4

Justin Niessner
Justin Niessner

Reputation: 245399

There's nothing wasteful about it. You're not passing a copy of all of the items, you're simply creating an array and then passing a reference to that array to the ListAllTowns method (arrays are reference types).

You're doing things (one of) the right way(s).

Upvotes: 7

Related Questions