How do you write code that is easily read by other people who did not have a hand in writing any part of it?

How do you write code that is easy to read by other people and who does not have the right to write any part of it?

+5
source share
13 answers

The best way to make sure others read your code is to make sure that it is clear and concise. Namely,

  • Use self-identifying names for variables, functions, and classes.
  • Comment on complex algorithms so that the reader does not have to analyze for a long time what he is doing.
  • Make sure tabs and line breaks are constant throughout the code.

, , , .

+9

, StackOverflow, FAQ

?

, , . , !

:

  • :

    // add one to the count:
    i++;
    
  • :

    int x = i + j;
    int runSum = prevSum += newValue;
    
  • , :

    if (x == y)
    {
      z = a;
    }
    else
    {
      z = b;
    }
    z = (x == y) ? a : b;
    
+6

Clean Code . . .

, , , , , . , , Test Driven Development, , , , , , , "".

, , , . - , .

+6

, . , , (, , foreach, , ).

( ), , .

+4
+4

, , ! . , . , , :

  • : , , , , , , , . .

  • : , , . , .

: , . , .

. . @rmbarnes, Code Complete 2. " " Kent Beck - .

+2
  • , , .
  • , ..
  • , , .
  • / .. .
  • ( ). , , ( , , , ).
+1

, , , , , . , , . Code Complete, , , .

+1

:

  • , . "x" "y" .
  • . , , , . . Cmp ? ? ? ? , .
  • . .
  • , . ntHungarian?
  • (if, for) . .
+1

, - . , .

0

, . , , , . . -.

  • , , . -, , - .

  • - . , , .

  • : . , , . , " , , " . , , .

0

, , , . . , HD , .

, :

        int total = 10;
        int sum = 0;

        for (int i = 0; i < total; i++)
        {
            sum += i;
        }

        // Next coding statement is a space below the bracket
        return sum;

:

        int total = 10;int sum = 0;
        for (int i = 0; i < total; i++)
        {
            sum += i;
        }
        return sum;

, , . if. , .

if(true)
    // some action

if(true)
{
   // Some action
}

- , . , , , , .

0

, - , . , (/ ..), , 10 , . Doxygen/Javadocs 10-15 , - .

100% , , / .

, , , , .

0

All Articles