Good coding style

Writing code is like writing poetry. Everyone has their own style, but the bottom line is that people should enjoy reading it.

Some general advice on good coding style

Some more specific advice on good coding style

Code formatting:

Bad example:

        for(int i=0; ... )             
        for(int j=0; ... )
        if(i<j)
        ...

Good example:

        for(int i=0; ... )             
            for(int j=0; ... )
                if(i<j)
                     ...

Bad example:

        public static int MethodX()
        {   int pointX; int pointY; int pointZ;       
            ...                     
            return pointX+pointY+pointZ; }
        public static int MethodY()
        {  ... }

Good example:

        public static int MethodX(){
            int pointX; // comment ...
            int pointY; // comment ...
            int pointZ; // comment ....

            ...                     

            return pointX+pointY+pointZ; 
        }


        public static void MethodY(){
            ...
        }

Variables:

Bad example:

        int cMX;

Good example:

        int currentMax;     

Good example:

       int  k;        // counter that ranges from i to j

Bad example:

        public static void MethodX(Matrix X, Matrix Y){
            // doing something 1

            Matrix tmpMatrix = operationMatrix(X,Y);

            // doing something 2

            Matrix productMatrix = operationMatrix(X,tmpMatrix);

            ...
        }

Good example:

       public static void MethodX(Matrix X){
            Matrix tmpMatrix;        // obtained by operationMatrix on X and Y
            Matrix productMatrix;    // obtained by operationMatrix on X and tmpMatrix

            // doing something 1

           tmpMatrix = operationMatrix(X,Y);

            // doing something 2

            Matrix productMatrix = operationMatrix(X,tmpMatrix);

            ...
        }

Good example:

    int m=1;      // minimal m when sorting takes >5 sec for array of size 10^m 

    for(; m<MAX_M; m++){
        int A[];                // random array
        long startTime;         // start time
        long estimatedTime;     // current time - start time

        ...
    }

Bad example:

        public static void MethodX(){
            ...
             for (... ;i<15; ...)
                for (... ;j<7; ...)
            ...
        }

Good example:

        public static int FIRST_ITERATION = 15;
        public static int SECOND_ITERATION = 7;

        public static void MethodX(){
            ...
             for (... ;i<FIRST_ITERATION; ...)
                for (... ;j<SECOND_ITERATION; ...)
            ...
        }

Methods/Functions:

Bad example:

        public static void Task21(){

            // minimal m when sorting takes >5 sec for array of size 10^m 

            // compute matrix of running times

            // compute average
        }

Good example:

        public static int findMinM(){ ... }

        public static long [][] generateMatrixOfRunnintTimes(int m){ ... }

        public static long [] computeAverage(long [][] runningTimes){ ... }

        public static long [] Task21(){

            int m = findMinM();

            long [][]  runningTimes =  generateMatrixOfRunnintTimes(int m);

            long [] avgTimes = computeAverage(runningTimes);

            // do the rest ...
        }

Testing:

Good example:

        public static void testFindInSortedArray(){ 
            int [] array = { -11, -3, -1, 2, 8, 11, 15, 23};

            // Here we expected that method finds the element with index 6
            System.out.println(findInSortedArray(array,15));

            // Here the method should return not found (-1)
            System.out.println(findInSortedArray(array,3));
        }

Commenting Code:

Bad example:

        /* this is the main method where our program starts*/
        public static void main(String[] args) {
            ...
        }

Bad example:

        // methodTryThis(3);
        // methodTryThat(4);
        // methodTryThis(5);
        // methodTryThat(6);

References


author: Ognjen Savkovic (Ognjen [DOT] Savkovic [AT] unibz [DOT] it)

Last edited: March 2015