Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Which code is better?

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Question Which code is better?

    Hello all,

    Which of the following code is better? Please tell me on what your answer is based, convention, understand-ability or usability.

    First one:
    package Java24;
     
    public class HR9StudentGrades {
    	public static void main(String []args){
            //Hour nine, activity one.
            int twodim [][] = new int[6][6];
     
            twodim[0][0] = 30;
            twodim[0][1] = 26;
            twodim[0][2] = 88;
            twodim[0][3] = 96;
            twodim[0][4] = 72;
            twodim[0][5] = 81; 
     
            int SIX = 6;
            int issac = 0;
     
            for(int index = 0; index < 6; index++){
                issac = issac + twodim[0][index];
            }
            issac = issac / SIX;
            System.out.println("The average grade mark of Issac is: " + issac);        
        }
    }

    Second One:
    package Java24;
     
    public class TestFile {
    	public static void main(String []args){
            //Hour nine, activity one.
            int twodim [][] = new int[6][6];
     
            twodim[0][0] = 30;
            twodim[0][1] = 26;
            twodim[0][2] = 88;
            twodim[0][3] = 96;
            twodim[0][4] = 72;
            twodim[0][5] = 81;
     
            int SIX = 6;
            int issac = 0;
     
            for(int index = 0; index < 6; index++){
                issac = issac + twodim[0][index];
                if(index == 5){
                	issac = issac /SIX;
                	System.out.println("The average grade mark of Issac is: " + issac);
                }
            }
        }
    }

    I only pasted part of the, if you want the full code let me know.

    Thanks for taking time to answer!
    -Alem

    Edit:
    Only diffidence atm is the for loop.
    Last edited by Melawe; June 30th, 2011 at 10:47 PM. Reason: Only diffidence atm is the for loop.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,514
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Which code is better?

    Well I have several issues with both examples. I assume creating the 2D array was just a quick mock-up for this example so I will leave it alone. Do not hard code values. You have declared a variable with the value 6 so why not use it in your code? Use meaningful names. As a programmer how do you know the student's name will be Isaac? As for the if statement inside the loop, BLEECH! Totally pointless and if the program had to process 1000's of rows of data then it will be evaluating that if statement for every iteration and it is only needed once at the end.

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Which code is better?

    Actually the SIX variable does have a use, here is the full code.

    package Java24;
     
    public class HR9StudentGrades {
    	public static void main(String []args){
            //Hour nine, activity one.
            int twodim [][] = new int[6][6];
     
            twodim[0][0] = 30;
            twodim[0][1] = 26;
            twodim[0][2] = 88;
            twodim[0][3] = 96;
            twodim[0][4] = 72;
            twodim[0][5] = 81;
     
            twodim[1][0] = 48;
            twodim[1][1] = 36;
            twodim[1][2] = 95;
            twodim[1][3] = 63;
            twodim[1][4] = 100;
            twodim[1][5] = 54;
     
            twodim[2][0] = 68;
            twodim[2][1] = 49;
            twodim[2][2] = 36;
            twodim[2][3] = 65;
            twodim[2][4] = 65;
            twodim[2][5] = 32;
     
            twodim[3][0] = 46;
            twodim[3][1] = 56;
            twodim[3][2] = 65;
            twodim[3][3] = 65;
            twodim[3][4] = 46;
            twodim[3][5] = 95;
     
            twodim[4][0] = 66;
            twodim[4][1] = 46;
            twodim[4][2] = 84;
            twodim[4][3] = 98;
            twodim[4][4] = 46;
            twodim[4][5] = 35;
     
            twodim[5][0] = 78;
            twodim[5][1] = 69;
            twodim[5][2] = 98;
            twodim[5][3] = 98;
            twodim[5][4] = 82;
            twodim[5][5] = 64;
     
            int SIX = 6;
            int issac = 0;
            int dawiet = 0;
            int alem = 0;
            int ghebremariam = 0;
            int daniel = 0;
            int minus = 0;
     
            for(int index = 0; index < 6; index++){
                issac = issac + twodim[0][index];
            }
            issac = issac / SIX;
            System.out.println("The average grade mark of Issac is: " + issac);
     
            for(int index = 0; index < 6; index++){
                dawiet = dawiet + twodim[1][index];
            }
            dawiet = dawiet / SIX;
            System.out.println("The average grade mark of dawiet is: " + dawiet);
     
            for(int index = 0; index < 6; index++){
                alem = alem + twodim[2][index];
            }
            alem = alem / SIX;
            System.out.println("The average grade mark of alem is: " + alem);
     
            for(int index = 0; index < 6; index++){
                ghebremariam = ghebremariam + twodim[3][index];
            }
            ghebremariam = ghebremariam / SIX;
            System.out.println("The average grade mark of ghebremariam is: " + ghebremariam);
     
            for(int index = 0; index < 6; index++){
                daniel = daniel + twodim[4][index];
            }
            daniel = daniel / SIX;
            System.out.println("The average grade mark of daniel is: " + daniel);
     
            for(int index = 0; index < 6; index++){
                minus = minus + twodim[5][index];
            }
            minus = minus / SIX;
            System.out.println("The average grade mark of minus is: " + minus);
     
        }
    }


    Edit: Hmm.. I don't really need the variable.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Which code is better?

    Unless SIX can hold a value other than 6, I would just get rid of it and use 6 in your code.

    If it can hold another value, call it by what it's used for (for example, call it numGrades). Same thing goes for twodim: it look like it's used to store the grades of the students, call it something that reflects that.

    Also, I would recommend against hard-coding variable names for the people. It's much more difficult to add new names.

    The best way to accomplish all of these tasks is to create a Student class which contains the student's name and 6 grades (actually, use an array to get an arbitrary number of grades). It could have methods for computing the student's average grade, and getter methods for the student's name and individual grades if necessary. Then in your main method, just have an array of Student objects. Loop through the array and print out the necessary information for each student.

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Which code is better?

    Quote Originally Posted by helloworld922 View Post
    If it can hold another value, call it by what it's used for (for example, call it numGrades). Same thing goes for twodim: it look like it's used to store the grades of the students, call it something that reflects that.
    Excuse the name of the array, I used to name variables with, well not that describe them well. After I started using objects I noticed that it isn't really good. I'll be sure to use more suitable names.

    Quote Originally Posted by helloworld922 View Post
    Also, I would recommend against hard-coding variable names for the people. It's much more difficult to add new names.
    A bit con fuddled there, could you explain a lil more please?


    Quote Originally Posted by helloworld922 View Post
    The best way to accomplish all of these tasks is to create a Student class which contains the student's name and 6 grades (actually, use an array to get an arbitrary number of grades). It could have methods for computing the student's average grade, and getter methods for the student's name and individual grades if necessary. Then in your main method, just have an array of Student objects. Loop through the array and print out the necessary information for each student.
    I like your Idea. I wrote it for an assignment a little before I started making objects, so it was all thrown in one class.


    Thanks!
    -Alem

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Which code is better?

    Also, I would recommend against hard-coding variable names for the people. It's much more difficult to add new names.
    A bit con fuddled there, could you explain a lil more please?
    This:
            int issac = 0;
            int dawiet = 0;
            int alem = 0;
            int ghebremariam = 0;
            int daniel = 0;
            int minus = 0;
    because that causes this:

            for(int index = 0; index < 6; index++){
                issac = issac + twodim[0][index];
            }
            issac = issac / SIX;
            System.out.println("The average grade mark of Issac is: " + issac);
     
            for(int index = 0; index < 6; index++){
                dawiet = dawiet + twodim[1][index];
            }
            dawiet = dawiet / SIX;
            System.out.println("The average grade mark of dawiet is: " + dawiet);
     
            for(int index = 0; index < 6; index++){
                alem = alem + twodim[2][index];
            }
            alem = alem / SIX;
            System.out.println("The average grade mark of alem is: " + alem);
     
            for(int index = 0; index < 6; index++){
                ghebremariam = ghebremariam + twodim[3][index];
            }
            ghebremariam = ghebremariam / SIX;
            System.out.println("The average grade mark of ghebremariam is: " + ghebremariam);
     
            for(int index = 0; index < 6; index++){
                daniel = daniel + twodim[4][index];
            }
            daniel = daniel / SIX;
            System.out.println("The average grade mark of daniel is: " + daniel);
     
            for(int index = 0; index < 6; index++){
                minus = minus + twodim[5][index];
            }
            minus = minus / SIX;
            System.out.println("The average grade mark of minus is: " + minus);
    You have a ton of loops which basically do the same thing for different variables, but exponentially increases the amount of code you need to maintain.

    In conjunction with creating a Student object (or if you really don't want to go down that path for some obscure reason), use arrays and just refer to them as "students" or something like that. Arrays are nice because you can easily loop through them.

    To further reinforce this, use the length field of the arrays whenever possible. It's a very big pain to have to hard-code the size of the array.

    // solution if you didn't create a student object
    String[] studentNames = new String[]{"issac", "dawiet", "alem", "ghebremariam", "daniel", "minus"};
    int[] studentAverages = new int[studentNames.length];
    int[][] studentScores = new int[studentNames.length][6];
    // ...populate studentScores with the respective students' scores
     
    // ... later on, when computing averages:
    // I know I said use descriptive names, but I got a little lazy and it's fairly well understood that i and j are indices
    for(int i = 0; i < studentNames.length; ++i)
    {
        for(int j = 0; index < studentScores[i].length; ++index)
        {
            studentAverages[i] += studentScores[j][index];
        }
        studentAverages[i] /= studentScores[i].length;
        System.out.println(studentNames[i] + " average score: " + studentAverages[i]);
    }

    Further improvements (other than the obvious one of using a Student object) would be to abstract away the average calculation to a "helper method".

    for(int i = 0; i < studentNames.length; ++i)
    {
        studentAverages[i] = computeAverage(studentScores[i]); // computeAverage is a method which takes in an array of scores and computes their average
        System.out.println(studentNames[i] + " average score: " + studentAverages[i]);
    }