Coding
The coding is the process of transforming the design of a system into a
computerlanguage format. This coding phase ofsoftware development is
concerned withsoftware translating design specificationinto the source code. It is
necessary towrite source code & internal documentationso that conformance of the
code to its specification can be easily verified.
Goals of Coding
1. To translate the design of system into a computer language format:
2. To reduce the cost of later phases:
3. Making the program more readable:
Structured Coding Techniques
Structured coding techniques aids inlinearizing the control flow through
acomputer program so that the executionsequence follows the sequence in
which thecode is written.
The following are some of the golden rules to beconsidered for
structured coding.
1.Single entry, Single exit constructs
Single entry, single exit constructs are control structuresthat enables us to create
sequencing, selection anditeration constructs.
The following are the common single entry, single exitconstructs –
Sequencing Construct
Selection Construct
Iteration Construct
Type 1: while-do looping
Type 2: do-while looping
Type 3: for loop
In cases when we require multiple loop exits, we mayapply go to as some
language allow it or shouldredesign the algorithm to avoid multiple loop
exits,which is the desired practice. Alternatively in suchcases, we may use
the exit or ‘break’ statement asfollows –
while (cond 1){
Code 1;
Code 2;
if (Cond 2){
break;
2. Data encapsulation
Data encapsulation is the process of encapsulating thedata operations of an object
from outside interferenceor misuse. In object oriented languages such as C++, C#
or Java, we implement data encapsulation by classabstractions. In such a case the
class itself doesn’t havephysical existence and is only a logical abstraction.
3. Recursion
A recursive programming style may be applied when theiterative processing may
be indirectly performed byrecursively calling the same program.
.Coding Style
The following are some of the major guidelines thatdictates a perfect codig
style
-DO –
.Use a few standard control constructs –
. Use Gotos in a disciplined manner – G
.Introduce user-defined datatypes to model entities inthe problem domain –
Hide data structures behind access functions. Isolatemachine dependencies
in a few routines
Provide standard documentation prologues for eachsubprogram and/or
compilation unit
.Use indentation, parenthesis, blank spaces, blank linesand borders around
blocks of comments to enhancecode readability
– DONT –
Do not use programming tricks –
. Avoid null-then statement –
Avoid then – if statements
.Avoid routines/methods that causes obscure sideeffects
.Don’t sub-optimize –
. Carefully examine methods/routines having than 5formal parameters
–
. Don’t use an identifier for multiple purposes –.Don’t nest the
loops too deeply –
Documentation
Structured programming constructs, good coding style,meaningful identifier names
to denote user-defined datatypes, variable, enumerators, literals, parameters
canvastly improve code readability and reduce the need forinternal documentation.
Some of the general guidelinesfor internal documentation is as follows –
● Minimize the need for embedded comments byusing
○ Standard prologues
○ Structured programming constructs
○ Good Coding style
○ Descriptive names from the problem domainfor user-defined data
types, variables, formalparameters, enumeration literals,
methods,files etc.
○ Self documenting features of the
implementation language such as
user-defined exceptions, user defined datatypes, data
encapsulation etc.
● Attach comments to blocks of code that
○ perform major data manipulations
○ simulate structured control constructs○ perform
exception handling
● Use problem domain terminology in the comments● Use blank lines,
borders, and indentation tohighlight comments
● Place comments to the far right to documentchanges and revisions
● Don’t use long, involved comments to documentobscure, complex
code, instead rewrite the code● Be sure that the comments and code
agree with
one another and with the requirements and designspecifications.
Coding Standards
General coding standards refers to how thedeveloper writes code, so here
we will discusssome essential standards regardless of theprogramming
language being used.
The following are some representativecoding standards:
1. Indentation: Proper and consistentindentation is essential in producing
easy toread and maintainable programs.
Indentation should be used to:
○ Emphasize the body of a control structuresuch as a loop or a select
statement.
○ Emphasize the body of a conditionalstatement
○ Emphasize a new scope block
2. Inline comments: Inline commentsanalyze the functioning of the
subroutine, orkey aspects of the algorithm shall befrequently used.
3. Rules for limiting the use of global:These rules file what types of data
can bedeclared global and what cannot.
4. Structured Programming: Structured (orModular) Programming methods
shall beused. "GOTO" statements shall not be used as
they lead to "spaghetti" code, which is hard toread and maintain, except as
outlined line inthe FORTRAN Standards and Guidelines.
5. Naming conventions for globalvariables, local variables, and
constantidentifiers: A possible naming conventioncan be that global
variable names alwaysbegin with a capital letter, local variablenames are
made of small letters, andconstant names are always capital letters.
6. Error return conventions and exceptionhandling system: Different
functions in aprogram report the way error conditions arehandled should be
standard within an
organization. For example, different taskswhile encountering an error
condition shouldeither return a 0 or 1 consistently.
Coding Guidelines
General coding guidelines provide theprogrammer with a set of the best
methodswhich can be used to make programs morecomfortable to read and
maintain. Most of theexamples use the C language syntax, but theguidelines
can be tested to all languages.
The following are some representative codingguidelines recommended by
many softwaredevelopment organizations.
1. Line Length: It is considered a good practiceto keep the length of source code
lines at orbelow 80 characters. Lines longer than this maynot be visible properly
on some terminals andtools. Some printers will truncate lines longerthan 80
columns.
2. Spacing: The appropriate use of spaces withina line of code can improve
readability.
Example:
Bad: cost=price+(price*sales_tax)
fprintf(stdout ,"The total cost is
%5.2f\n",cost);
Better: cost = price + ( price * sales_tax )
fprintf (stdout,"The total cost is
%5.2f\n",cost);
3. The code should be well-documented: Asa rule of thumb, there must be at
least one
comment line on the average for everythree-source line.
4. The length of any function should notexceed 10 source lines: A very
lengthyfunction is generally very difficult to understandas it possibly carries out
many various functions.For the same reason, lengthy functions arepossible to
have a disproportionately largernumber of bugs.
5. Do not use goto statements: Use of gotostatements makes a program
unstructured andvery tough to understand.
6. Inline Comments: Inline comments promotereadability.
7. Error Messages: Error handling is anessential aspect of computer
programming. Thisdoes not only include adding the necessary logicto test for
and handle errors but also involvesmaking error messages meaningful.
Data Abstraction
Data abstraction is the programming process of creating a data type, usually a
class, that hides the details of the data representation in order to make the data
type easier to work with. Data abstraction involves creating a representation for
data that separates the interface from the implementation so a programmer or
user only has to understand the interface, the commands to use, and not how the
internal structure of the data is represented and/or implemented.
User defined data types
User-Defined DataTypes:
The data types that are defined by the user are called the derived datatype or user-
defined derived data type.
These types include:
● Class
● Structure
● Union
● Enumeration
● Typedef defined DataType
Below is the detailed description of the following types:
Class: The building block that leads to Object-Oriented programming is a Class. It
is a user-defined data type, which holds its own data members and member
functions, which can be accessed and used by creating an instance of that class. A
class is like a blueprint for an object.
Syntax:
Structure: A structure is a user defined data type in C/C++. A structure creates a
data type that can be used to group items of possibly different types into a single
type.
Syntax:
struct address {
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
Union: Like Structures, union is a user defined data type. In union, all members
share the same memory location. For example in the following C program, both x
and y share the same location. If we change x, we can see the changes being
reflected in y.
Enumeration: Enumeration (or enum) is a user defined data type in C. It is mainly
used to a constants, the names make a program easy to read and maintain.
Syntax:
enum State {Working = 1, Failed = 0};
Typedef: C++ allows you to define explicitly new data type names by using the
keyword typedef. Using typedef does not actually create a new data class, rather it
defines a name fo an existing type. This can increase the portability(the ability of a
program to be used across different types of machines; i.e., mini, mainframe,
micro, etc; without much changes into the code)of a program as only the typedef
statements would have to be changed. Using typedef one can also aid in self-
documenting code by allowing descriptive names for the standard data types.
Syntax:
Typedef type name;
wheretype is any C++ data type and name is the new name for this data type.
This defines another name for the standard type of C++.
Concurrency control
Concurrency in software engineering means the collection of techniques and
mechanisms that enable a computer program to perform several different tasks
simultaneously, or apparently simultaneously. This allows for parallel execution of
the concurrent units, which can significantly improve overall speed of the
execution. The base goals of concurrent programming include correctness,
performance and robustness.