具有相同类型的多个对象的名称
假设我们有一辆等级车。
您如何命名需要两辆不同汽车的函数参数?
void Race(Car first, Car second);
或者也许
void Race(Car car1, Car car2);
与以汽车和汽车列表作为参数的函数有相同的情况。
我习惯于将汽车列表命名为“汽车”,因此使用以下名称很不方便:
void Race(Car car, List<Car> cars);
关于名称有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我个人选择代表汽车用途的名称。
在您的示例中,我可能只有
void Race(IListcars)
,因为每辆车都是等效的。如果总是有两个等效的选项,那么使用 firstCar 和 secondaryCar 似乎是合理的。
但是,如果您正在做类似将零件从一辆车移动到另一辆车的事情,我会非常具有描述性,即:
您的参数命名越具有描述性,您的 API 就越容易使用。
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:
The more descriptive you can be in your argument naming, the easier your API becomes for usage.
将数字添加到函数参数通常是一种不好的做法,因此我会使用
void Race(Car first, Car secondary);
关于你的第二个函数,我没有看到名为的汽车列表有任何问题
cars
但我无法理解将一辆汽车作为单独的参数传递的想法,因此我建议将其重命名以显示它与列表中的汽车的差异,或者如果可以将其作为单独的参数传递,则将其删除汽车
列表的成员。PS 一旦可以通过智能感知检索到
cars
列表,您就可以将cars
列表重命名为participants
或competitors
你的IDE。更新: 基本上,参数的名称应首先取决于函数的名称,然后取决于参数的类型(如果需要)。因此,我会选择
Car GetSimilar(Car targetCar, IListcars)
。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 ofcars
list.P.S. You can surely rename
cars
list intoparticipants
orcompetitors
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)
.