大象传媒

Maintainability

The purpose of is to ensure that, over time, a can be easily maintained.

A programmer may decide to return to a program they wrote some time before in order to add an extra feature. Additionally, another programmer may wish to modify the program in order to improve it or an error.

In both situations, the understanding of the program, how it works and the purpose of the code will be made easier if the program is written in a maintainable style. This includes using:

  • comments
  • naming conventions
  • indentation

Comments

Comments are lines in programs that provide information about what the different parts of the program do. They serve no other purpose, and are not executed when the program is run - the program simply ignores them and moves on to the next line of code.

Different languages implement comments in different ways. In Python, for example, a comment begins with a hash symbol, and is coloured red:

#ensures the input is restricted to a y or an n

Naming conventions

Many programmers choose meaningless names such as x or y. While a program will run with such names, it makes understanding the purpose of the variable much more difficult. Programmers should choose a variable name that reflects the purpose of the variable and the it is intended to hold:

H - poor choice. What does it mean?

height - better choice. The value to be inputted will be a number.

Indentation

Code within selections or should be indented. This allows the programmer to easily see which code falls within the selection or iteration, and where it ends.

Some programming languages, such as Python, indent such code automatically.

do
    for count = 1 to 10
         print("Hey!")
    next count
    again = input("Enter y to go again")
    again.lower
until again !="y"