开发者

Is it good practice to name variables differently when defining more than one function?

For example, in this simple function where fun1 takes as input two numbers, adds them together and passes them to function 2 for printing the output. var1_in is local to each function, so is it OK to use the n开发者_运维技巧ame var1_in in both functions, or is it better practice to call them different things?

fun1 <- function (var1_in, var2_in) {
 var3 = var1_in + var2_in
 fun2(var3)
 }

fun2 <- function (var1_in) {
 var4 = var1_in
 print(var4)
 }


As long as the functions are short enough to easily understand, then identifying the scope of local variables and parameters will be easy as well. But there isn't a hard and fast rule for this. What's important is that the code is easy to understand and that the names of variables are relevant and meaningful regardless if this means name duplication. Modern IDE's will also help here by highlighting the instances of such variables making it easy to see their declaration and various usage points. Point being, I would focus more on quality and meaningful naming rather than duplication of variable names.

EDIT - Of course, one situation to avoid would be naming a local variable or parameter the same as a global variable. This can confuse things greatly and lead to many a subtle bug.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜