'Which type of function fits more [closed]

Say, I need to sort some scores from classOne, classTwo, and classThree. Each class has their own file, named as stated previously. I need to print them on the screen, as following:

Class One:
John 23
Timmy 34
Drake 35

Class Two:
Tina 25
Bob 31

Class Three:
Anthony 18
Rose 20

In the files themselves, each data is formatted in name#score, therefore I used fscanf to read them properly and print them as name score.

Now, coming to the sorting part. I propose two methods:

  1. Sort every file respectively in 1 function
  2. Sort each file in their own function

According to Robert C. Martin in his book, Clean Code, one of the main rules about functions is that they should do only ONE thing. This means that Number Two obviously stands, however, I think that it is WET and Number One could do the same with a much DRYer approach.

Below are some code snippets.

Number 1:

void sortFile(){
    FILE *classScore;
    for (int i = 0; i < 3; i++){
        char name [50][20]; //assuming there are 50 students
        int score [50];
        if (i == 0){
            classScore = fopen ("score1.txt", "r");
        }
        else if (i == 1){
            classScore = fopen ("score2.txt", "r");
        }
        else if (i == 2){
            classScore = fopen ("score3.txt", "r");
        }
        //stores file content into array
        fclose (classScore);
        //sort mechanism (bubble sort)
        //print sorted array content
    }
}

Number 2:

void sortClassOne(){
    FILE *classScore;
    char name [50][20]; //assuming there are 50 students
    int score [50];
    classScore = fopen ("score1.txt", "r");
    //stores file content into array
    fclose (classScore);
    //sort mechanism (bubble sort)
    //print sorted array content
    }

void sortClassTwo(){
    FILE *classScore;
    char name [50][20]; //assuming there are 50 students
    int score [50];
    classScore = fopen ("score2.txt", "r");
    //stores file content into array
    fclose (classScore);
    //sort mechanism (bubble sort)
    //print sorted array content
    }

void sortClassThree(){
    FILE *classScore;
    char name [50][20]; //assuming there are 50 students
    int score [50];
    classScore = fopen ("score3.txt", "r");
    //stores file content into array
    fclose (classScore);
    //sort mechanism (bubble sort)
    //print sorted array content
    }

With this, which between both methods would fit (at least in my case)? I understand that I am not working with a really big project or somekind, but I would like to get myself used to good habits in writing codes further on.

Thank you! ;)



Solution 1:[1]

Posting my working code here! I followed some suggestions from the comments.

void readFile(FILE *fileOpenedToRead){
char buffer[20];
int temp;
j = 0;
while (fscanf (fileOpenedToRead, "%[^#]#%d\n", buffer, &temp) != EOF){
    strcpy (namePlayer[j], buffer);
    score[j] = temp;
    j++;
}
fclose (fileOpenedToRead);}

void sortFile(){
int store;
char storage[20];
for (int i = 0; i < j; i++){
    for (int k = 0; k < j - i - 1; k++){
        if (score[k] > score[k+1]){
            store = score[k]; //swap score
            score[k] = score[k+1];
            score[k+1] = store;
            strcpy (storage, namePlayer[k]); //swap name
            strcpy (namePlayer[k], namePlayer[k+1]);
            strcpy (namePlayer[k+1], storage);
        }
    }
}}

void displayFile(){
for (int i = 0; i < j; i++){
    printf ("| %d", i+1);
    if (i < 9) printf ("   ");
    else printf ("   ");
    printf ("| %s", namePlayer[i]);
    for (int k = 0; k < 51 - strlen(namePlayer[i]); k++) printf (" ");
    printf ("|    %d |\n", score[i]);
}}

Solution 2:[2]

For myself, I would personally prefer the first option as it would be as you mentioned "DRYer". But I would have some slight modifications to the codes. Firstly, I would add a numOfClass parameter to dynamically determine if there is how many classes. Secondly, assume all of the file have the same format of name scoreX.txt, I would create a string for the file name and I dont need the if else block.

void sortFile(int numOfClass){
    FILE *classScore;
    for (int i = 0; i < numOfClass; i++){
        char name [50][20]; //assuming there are 50 students
        int score [50];
        string className = "score" + (i + 1) + ".txt";
        classScore = fopen(className, "r");
        //stores file content into array
        fclose (classScore);
        //sort mechanism (bubble sort)
        //print sorted array content
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bolakecil
Solution 2 Loong