C++ 这个款式好吗?
我的函数中有一些参数接收和传递相同的值。我是否应该将所有参数变量命名为相同的名称 例如:C++ 这个款式好吗?,c++,coding-style,parameter-passing,C++,Coding Style,Parameter Passing,我的函数中有一些参数接收和传递相同的值。我是否应该将所有参数变量命名为相同的名称 例如: // assume numMonth = 12 // assume numYear = 2000 int promptMonth(); // returns numMonth int promptYear(); // returns numYear int daysInMonth(int numMonth, int numYear); // numMonth and numYear will alwa
// assume numMonth = 12
// assume numYear = 2000
int promptMonth(); // returns numMonth
int promptYear(); // returns numYear
int daysInMonth(int numMonth, int numYear); // numMonth and numYear will always
int daysInYear(int numYear); // be sent to these.
bool isLeapYear(int numYear); // daysInYear() and daysInMonth() call
// this function and send it numYear
// (which would be 2000 in this case).
既然传递给每个参数的值相同,那么所有这些参数是否应该命名相同?相同,我假定您指的是函数参数名称
numYear
是的,以一种有意义的方式命名变量始终是一种良好的做法,它表明了所传递值的意义/目的
这些变量是不同函数的一部分,因此其作用域仅限于这些函数,因此如果您考虑到这一点,就不存在多个定义的问题。同样,我假设您指的是函数参数名称
numYear
是的,以一种有意义的方式命名变量始终是一种良好的做法,它表明了所传递值的意义/目的
这些变量是不同函数的一部分,因此范围仅限于这些函数,因此,如果您考虑到这一点,就不存在多个定义的问题。通常,这种样式是个好主意。你当然应该遵循它,但仍然有滥用和误解的空间 让我解释一下
- 错误解释:即使您对变量的命名是一致的,读者也可能不相信您的意思
-
误用:可能没有任何
适合作为年份值int
cout << "the current month is " << promptMonth();
一般来说,这种风格是个好主意。你当然应该遵循它,但仍然有滥用和误解的空间 让我解释一下
- 错误解释:即使您对变量的命名是一致的,读者也可能不相信您的意思
-
误用:可能没有任何
适合作为年份值int
cout << "the current month is " << promptMonth();
“相同的值”是什么意思?我的意思是,如果在
promptmount()
中为numMonth
赋值12,那么函数daysInMonth()
中的参数变量numMonth
也是12,因为promptmount()
传递给daysInMonth()
。因此变量numMonth
是并且应该始终是相同的值,在本例中为12。您所说的“相同的值”是什么意思?我的意思是如果numMonth
在promptmount()
函数daysInMonth()中被赋值为12
也是12,因为promptmount()
中的numMonth
被传递到daysInMonth()
。因此变量numMonth
是并且应该始终是相同的值,在本例中为12。
the current month is 'October'