Thursday, 11 December 2014

F452 - Maintainable code

Programs should be written so that they are easy for any programmer to understand, debug and modify.


Why write maintainable code?


  • Make errors less likely.
  • Make errors easier to spot.
  • Programmers working in a team can understand each other's code.
  • If the code needs to be revisited later, the time needed to familiarise yourself with the program is reduced.

Variable & constants: definitions?

  • Variable: 
The name used to refer to a particular memory location used to store data of a particular type which may change as the program is running.
  • Constant: 
The name used to store a fixed value. The value is set when the code is written and cannot be changed while the program is running.
  • Identifier: 
The name given to a variable, a constant or a subroutine.

Choosing Identifiers?

  • Must begin with a letter.
  • May contain letter, digits and underscores.
  • Should be meaningful... They should describe the purpose of the variable or constant.
  • Use underscores_ or CamelCase to divide words.
  • Use standard conventions to indicate data types.
  • Avoid reserve words (keywords).
  • Variables should be initialised before being used in an expression.
  • Constants should be declared at the beginning of the program or subroutine.

Scope or Variables & Constants?

Usually LOCAL or GLOBAL.
Refers to the part of the program in which a variable or constant can be used.

Local:
Declared within a subroutine and only available for use within that subroutine.
Pros: Saves space; able to reuse names.

Global:
Declared at the beginning of the program and available throughout the code.
Pros: Eases data passing between parts of the code; more efficient when used as constants.

[Encapsulation: the function is self contained.]

Code layout [Modularisation]:

  • Complex operations should be broken up into subroutines that are defined separately and called from the main routine.
  • Modularisation is easier if the program has been designed using top-down techniques.

Code layout [Indentation]:

  • Code within structures should be inherited.
  • White spaced should run from start to end of statement.
  • Makes it clear where structures are nested.
//ALL THE COOL KIDS USE COMMENTS


No comments:

Post a Comment