Tomek Tarczynski
Tomek Tarczynski

Reputation: 2785

Name of several objects that have the same type

Lets assume we have a class car.
How would You name parameters of function that takes two different cars?

void Race(Car first, Car second);

or maybe

void Race(Car car1, Car car2);

The same situation with function that takes car and list of cars as a parameters.
I'm used to name 'cars' for list of cars, so it is inconvenient to use names like:

void Race(Car car, List<Car> cars);

Any suggestions about names?

Upvotes: 2

Views: 70

Answers (2)

Li0liQ
Li0liQ

Reputation: 11264

Adding numbers to function arguments is commonly a bad practice therefore I'd go with void Race(Car first, Car second);

Concerning your second function I don't see any problems with the list of cars named cars but I cannot understand the idea of passing a single car as a separate argument so I'd advice to rename it to show it's difference from the cars in the list or remove if it can be passed as a member of cars list.

P.S. You can surely rename cars list into participants or competitors as soon as their type can be retrieved by intellisense of your IDE.

Upd.: Basically, the names of the arguments should depend on the name of the function first and on the type of the argument second, if needed. So, I'd go with Car GetSimilar(Car targetCar, IList<Car> cars).

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564413

I personally choose names that represent how the cars will be used.

In your example, I'd probably just have void Race(IList<Car> cars), since each car is equivalent.

If there are always two, equivalent options, using firstCar and secondCar seems reasonable.

However, if you were doing something like moving parts from one car to another, I'd be very descriptive, ie:

static void StealParts(Car targetCar, Car sourceCar);

The more descriptive you can be in your argument naming, the easier your API becomes for usage.

Upvotes: 4

Related Questions