Ad Code

Identifier names also play a significant role

Identifier names also play a significant role


Identifier names also play a significant role in enhancing the readability of a program. The names should be chosen in order to make them meaningful to the reader. In order to understand the concept, let us look at the following statement.

if (x==0) // this is the case when we are allocating a new number

In this particular case, the meanings of the condition in the if-statement are not clear and we had to write a comment to explain it. This can be improved if, instead of using x, we use a more meaningful name. Our new code becomes:

if (AllocFlag == 0)

The situation has improved a little bit but the semantics of the condition are still not very clear as the meaning of 0 is not very clear. Now consider the following statement:

If (AllocFlag == NEW_NUMBER)

We have improved the quality of the code by replacing the number 0 with a named constant NEW_NUMBER. Now, the semantics are clear and do not need any extra comments, hence this piece of code is self-documenting.


Reactions

Post a Comment

1 Comments

  1. CS504 Software Engineering Quizzes
    http://vukwl.blogspot.com/2018/07/cs504-software-engineering-quizzes-file_15.html

    ReplyDelete

Please add nice comments or answer ....