C 如何避免超车;“多余的”;函数中的参数?

C 如何避免超车;“多余的”;函数中的参数?,c,function,parameters,arguments,C,Function,Parameters,Arguments,我正在编写一些代码,我意识到我一直在向函数传递参数,而函数没有使用参数。只有带有函数的子函数(或子函数中的子函数等…)使用它 实际函数根本没有使用参数,它的唯一目的是将参数“中继”到子函数(或子函数等等) 例如: 有没有一种优雅的方式来避免重复的争论?或者这只是C语言固有的一部分?将my转换为答案 “显而易见”的替代方法是通过全局变量“传递”这些变量。这是一个好主意-编写的代码更好 如果你有两个或多个从一个函数传递到另一个函数的参数(如这里),你可以考虑将它们组合成一个由指针传递的结构,因此

我正在编写一些代码,我意识到我一直在向函数传递参数,而函数没有使用参数。只有带有函数的子函数(或子函数中的子函数等…)使用它

实际函数根本没有使用参数,它的唯一目的是将参数“中继”到子函数(或子函数等等)

例如:



有没有一种优雅的方式来避免重复的争论?或者这只是C语言固有的一部分?

将my转换为答案


“显而易见”的替代方法是通过全局变量“传递”这些变量。这是一个好主意-编写的代码更好

如果你有两个或多个从一个函数传递到另一个函数的参数(如这里),你可以考虑将它们组合成一个由指针传递的结构,因此每个调用函数只有一个直接的参数。 请注意,如果从子函数调用的函数需要用户传递给调用函数的信息,则参数不是“冗余的”;它们是必要的,即使是冗长的

概述:

typedef struct BestInfo
{
    int row;
    int col;
} BestInfo;

int search(int (*board)[DIM], int search_digit, BestInfo *best)
{ 
    // some code; doesn't use argument best

    longest=seq_length(board, longest, search_digit, best,  r, c); //sub-function

    // some more code; doesn't use argument best
}

int seq_length(int board[][DIM], int longest, int search_digit, BestInfo *best, int row, int col)
{
    // code that doesn't use argument best

    longest = updateLongest_best(best, longest, seqLength, row, col); //sub-sub-function

    // more code that doesn't use argument best
    return …;
}

int updateLongest_best(BestInfo *best, int longest, int seqLength, int row, int col)
{ 
    // Finally use argument best: best->row, best->col
    // You can split the structure when appropriate
    int r1 = one_more_function(&best->row);
    int r2 = another_function(&best->col);
    return computation_using(r1, r2);
}

我想说的是,你需要重构代码来扁平化调用树,但是如果没有它,就很难说了。。。这不是多余的!现在,
bestRow
bestCol
search()
返回的指针。真正的问题是:
search()
的调用方是否使用或设置了这些参数?如果是,那么就完成了。“显而易见”的选择是通过全局变量“传递”这些变量。这不是一个好主意——编写的代码更好。如果你有两个或多个这样的参数(如这里),你可以考虑将它们组合成一个由指针传递的结构,因此只有一个直接的参数。@ RoBotoBabOni,你是说只要Salk()的调用方使用这些参数,它就不是。redundant@JonathanLeffler我可以在这里使用structs,谢谢!
int seq_length(int board[][DIM],int longest,int search_digit,int * bestRow,int *bestCol,int row,int col)
{
  //some code,haven't use arguments bestRow,bestCol

  longest=updateLongest_best(bestRow,bestCol,longest,seqLength,row,col); //sub-sub-function

  //some code,haven't use arguments bestRow,bestCol

}
int updateLongest_best(int* bestRow,int *bestCol,int longest,int seqLength,int row,int col)
{ 
 //finally used arguments bestRow,bestCol
}
typedef struct BestInfo
{
    int row;
    int col;
} BestInfo;

int search(int (*board)[DIM], int search_digit, BestInfo *best)
{ 
    // some code; doesn't use argument best

    longest=seq_length(board, longest, search_digit, best,  r, c); //sub-function

    // some more code; doesn't use argument best
}

int seq_length(int board[][DIM], int longest, int search_digit, BestInfo *best, int row, int col)
{
    // code that doesn't use argument best

    longest = updateLongest_best(best, longest, seqLength, row, col); //sub-sub-function

    // more code that doesn't use argument best
    return …;
}

int updateLongest_best(BestInfo *best, int longest, int seqLength, int row, int col)
{ 
    // Finally use argument best: best->row, best->col
    // You can split the structure when appropriate
    int r1 = one_more_function(&best->row);
    int r2 = another_function(&best->col);
    return computation_using(r1, r2);
}