Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Chapter 6

d i ng
C o
Introduction
• The goal of the coding or programming activity is
to implement the design in the best possible
manner.
• Coding affects testing and maintenance
profoundly
• The time spent in coding is a small percentage of
the total software cost, while testing &
maintenance consume major percentage.
• As testing and maintenance costs are high, the aim
 Hence, goal should not be to reduce coding cost, but
testing and maintenance cost, i.e. make the job of tester
and maintainer easier
Programming Principles & Guidelines

The main goal of the programmer is to write simple and


easy to read programs with few bugs in it.
Of course, the programmer has to develop it quickly to
keep productivity high
There are various programming principles that can help
write code that is easier to understand (and test…).
Some of these are
Be aware of common programming errors
Use structured programming
Apply information hiding
Follow some coding practices
Practice coding standards
Common Coding Errors
• Much of effort in developing software goes in
identifying and removing bugs.
• There are various practices that can reduce the
occurrence of bugs, but regardless of the tools or
methods we use, bugs are always going to occur in
programs.
• Common bugs which occur during coding directly or
indirectly manifest themselves to a larger damage to the
running program.
• List of common coding errors can help a programmer
avoid them.
• Examples
• Array index out of bounds
• Off by one errors
• Freeing an already freed resource
• etc.
Structured Programming
• It has been argued by many that structured
programming practice helps develop programs that are
easier to understand.
• The goal of structured programming is to simplify
program structure so it is easier to argue about
programs.
• A program has a static structure which is the ordering
of statements in the code – and this is a linear ordering.
• A program also has dynamic structure –order in which
statements are executed.
• Correctness of a program must talk about the dynamic
structure.
• Hence, to show a program as correct, we must show
that its dynamic behavior is as expected.
• The aim of structured programming is to write
programs whose dynamic structure is same as static
structure.
Information Hiding
• Software solutions always contain data structures that
hold information
• Programs work on these data structures to perform the
functions they want
• In general only some operations are performed on the
information, i.e. the data is manipulated in a few ways
only
• The information should be hidden; only operations on it
should be exposed (information hiding )
• I.e. data structures are hidden behind the access
functions, which can be used by programs
• Information hiding reduces coupling
• This practice is a key foundation of OO and components,
and is also widely used today

Coding Standards
Programmers spend more time reading code than
writing code
Readability is enhanced if some coding conventions are
followed by all
Coding standards provide these guidelines for
programmers
Some Java coding conventions are
Naming conventions e.g. Const name, package name,
Type name, method name variable name
Commenting and layout e.g. single line and multiline
comments
Layout guidelines e.g. Indentation guidelines
Coding Process
• Coding starts when specifications for modules from
design is available
• Usually modules are assigned to programmers for
coding
• In top-down development, top level modules are
developed first; in bottom-up lower levels modules are
developed first.
• For coding, developers use different processes; Some
of these are.
• An Incremental Coding Process : most common
• Test Driven Development: relatively new approach
• Pair programming: key technique in extreme
programming (XP) methodology
An Incremental Coding Process
• In this process
• A code is written for the currently assigned module,
and when done, unit testing is performed on it and
the bugs found are fixed
• Then the code is checked in the project repository to
make it available to others in the project.
• Look at the figure on next page
• A better process for coding, that is often followed by
experienced developers – most common one
• Advantage
• with testing being done after every round of coding is
to facilitate debugging—an error found in some
testing can be safely attributed to code that was added
since last successful testing.
Test Driven Development (TDD)
• In TDD, a programmer first writes the test scripts, and then
writes the code to pass the tests.
• The whole process is done incrementally, with tests being
written based on the specifications and code being written to
pass the tests.
• Is a relatively new approach, and is a part of the extreme
programming (XP)
• Ensures that all functionality is there is on test case design,
not coding
• Help ensure that all code is testable (advantages)
• Has possibility that special cases for which test cases are not
possible get left out (Disadvantage)
Pair programming
• Also a coding process that has been proposed as key
practice in XP
• Code is written by pair of programmers rather than
individuals
• The pair together design algorithms, data structures,
strategies, etc.
• One person types the code, the other actively reviews
what is being typed
• Errors are pointed out and together solutions are
formulated
• Roles are reversed periodically
• Advantage
• PP has continuous code review, and reviews are
known to be effective
• likely to result in better decisions being taken about
the data structures, algorithms, interfaces, logic, etc.
• Disadvantage
• It may, however, result in loss of productivity
• Ownership and accountability issues are also there
Verification
• Once a programmer has written the code for a module,
it has to be verified before it is used by others.
• Here we discuss only verification of code written by a
programmer (system verification is discussed in next
chapter-Testing)
• There are many different techniques; Some of these are
• Code Inspections
• Unit Testing
• Static Analysis
• Formal verification
Verification techniques
• Code Inspection:
• Involves reviewing source code with a group who ask
questions analyzing the program logic, analyzing the
code with respect to a checklist of historically
common programming errors, and analyzing its
compliance with coding standards.
• Unit Testing
• A software testing technique that focuses on
exercising the features of individual functions or
modules in isolation.
• Static Analysis
• Analysis of a program carried out without executing
the program.
• There numerous tool supports
• Formal Verification
• Use of various types of logic and mathematical
methods to verify the correctness of code - Used
mostly in very critical situations

You might also like