Quality and readability of the code - Let`s E
Download
Report
Transcript Quality and readability of the code - Let`s E
Quality and Readability of the
code
Criteria covered in this presentation
M1** Explain the importance of the quality of code
Discuss the factors that can improve the
D1**
readability of code
The importance of the quality of the
code
High quality code is important to ensure having
an efficient, a reliable, a robust, a usable, a
portable and an easily maintainable system
–
–
–
–
–
–
Efficiency
Reliability
Robustness:
Usability
Portability
Maintainability
Efficiency
• The goal of code efficiency is to reduce
resource consumption and completion time as
much as possible with minimum risk
• Code efficiency is directly linked with the
speed of runtime execution for software.
• Can be achieved through using appropriate
data types, functions, procedures and try
catch statements
Reliability
• Will your code function every time it is run?
• Will it break down, will it throw out errors?
• Will it produce different results even though
you're running the same data through it?
Robustness
• Provide predictable documented behaviour in
response to errors and exceptions.
• Do not hide errors and do not force clients to
detect errors.
• E.g.; data entry validation and displaying error
messages using if and using try catch
statements
Usability
• How easy is the code to use
• Can an untrained user use the code
• How easy is the code to edit
Portability
• Can your code be used on different
computers, different operating systems
• Can your code be used in different companies,
or different countries
Maintainability
•
•
•
•
How easy is it to edit the code
How often do people have to update the code
How well commented is the code
The most important factor that affects the
maintainability of a program is its readability
which is discussed in the coming slides
Readability of the code
• Readability describes the ease of which
programs can be read and understood.
• It is so important because it significantly
effects the maintainability of code which has
in turn been a major cost for programs.
Factors that improve the readability of
code
•
•
•
•
•
Comments
Appropriate naming scheme
Indentation
Code grouping
DRY Principle
Comments
• Comments are usually added with the
purpose of making the source code easier to
understand
• Comments are used to explain the intent of
the programmer
• It’s a good practice to include the name of the
programmer and last date of modifying the
program at the top of the program in form of
comments
Appropriate names for variables
• Meaningful names that match the purpose it’s used
for, like: name and age instead of n and a for to save
values of name and age respectively
• Consistent Naming Scheme: The names should have
word boundaries. There are two popular options:
– camelCase: First letter of each word is capitalized,
except the first word, like: netPrice,
discountAmount
– underscores: Underscores between words, like:
net_price, discount_amount
Indentation
• It is a good idea to keep your indentation style
consistent.
• Automatically done through visual studio
Code Grouping
• Most often, certain tasks require a few lines of
code. It is a good idea to keep these tasks
within separate blocks of code, with some
spaces between them.
DRY Principle
• DRY stands for Don’t Repeat Yourself. Also
known as DIE: Duplication is Evil.
• The principle states:“Every piece of knowledge
must
have
a
single,
unambiguous,
authoritative representation within a system.”
• This principle should be maintained in all
code. The same piece of code should not be
repeated over and over again.
• Like using functions and procedures