示例
void
doEverything(
int
argc,
char
* argv[])
{
/*
*
* Comments contribute to the physical function length.
*
*
*
For statement counting, this method would have 13 statements.
*
* */
if
(argc>1){
cout <<
"Reading all files"
;
for
(
int
i=1; i<argc; i++){
cout <<
"reading file "
<< argv[i] << endl;
char
temp[1000];
fstream file(argv[i], ios::in);
cout <<
"file contents are "
<< endl;
while
(!file.eof()){
file.getline(temp, 1000);
cout << temp;
}
file.close();
}
}
else
{
cout <<
"No files specified"
;
}
}
解决方案
使函数更简短且更有针对性。应该有足够的头和内嵌注释用于记录。
void
readOneFile(
char
* fileName);
/*
*
* Header for function readEachFile(int argc, char* argv[])
* Reads a given list of files.
*
* */
void
readEachFile(
int
argc,
char
* argv[])
{
if
(argc>1){
//the first parameter is the executable
cout <<
"Reading all files"
;
for
(
int
i=1; i<argc; i++){
cout <<
"reading file "
<< argv[i] << endl;
readOneFile(
argv[i]
);
}
}
else
{
cout <<
"No files specified"
;
}
}
void
readOneFile(
char
* fileName){
char
temp[1000];
fstream file(fileName, ios::in);
cout <<
"file contents are "
<< endl;
while
(!file.eof()){
file.getline(temp, 1000);
cout << temp;
}
file.close();
}