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

BITS Pilani

presentation
BITS Pilani Pawan Gupta
Pilani Campus
BITS Pilani
Pilani Campus

SS ZG553, Real Time Systems


Lecture No 8
Coding standards
Is a set of stylistic conventions.
Complying with coding standards will not foster portability,
but rather in many cases, readability and maintainability.
May even increase reliability.
May also be used to foster improved performance by
encouraging or mandating the use of language
constructs that are known to generate more efficient
code.
Many agile methodologies embrace coding standards, for
example, Extreme Programming.

3
BITS Pilani, Pilani Campus
Coding standards
Standard or boilerplate header format.
Frequency, length, and style of comments.
Naming of classes, methods, procedures, variable names, data,
file names and so forth.
Formatting of program source code including use of white space
and indentation.
Size limitations on code units including maximum and minimum
lines of code, number of methods and so forth.
Rules about the choice of language construct to be used; for
example, when to use case statements instead of nested if-
then-else statements (previously discussed).
Example: Hungarian notation -- a set of rules about naming
variables, it has been used with C++, Ada, Java and even C.

4
BITS Pilani, Pilani Campus
Coding standards
They can promote very mangled variable names.
Their very strength can be its own undoing. E.g. what if the
type information embedded in the object name is, in fact,
wrong? There is no way for a compiler to check this.
Adoption of coding standards is not recommended mid-
project. It is much easier to start conforming than to be
required to change existing code to comply.
A “bad” coding standard is better than none at all.

5
BITS Pilani, Pilani Campus
Overview of Overloading

Overloading occurs when the same operator or


function name is used with different signatures
Both operators and functions can be overloaded
Different definitions must be distinguished by their
signatures (otherwise which to call is ambiguous)
– Reminder: signature is the operator/function name and
the ordered list of its argument types
– E.g., add(int,long) and add(long,int) have
different signatures
– E.g., add(const Base &) and add(const
Derived &) have different signatures, even if
Derived is-a Base
– Most specific match is used to select which one to call

BITS Pilani, Pilani Campus


Overloading vs. Overriding

Overriding a base class member function is similar to


overloading a function or operator
– But for overriding, definitions are distinguished by their scopes rather than by
their signatures

C++ can distinguish method definitions according to either


static or dynamic type
– Depends on whether a method is virtual or not
– Depends on whether called via a reference or pointer vs. directly on an object
– Depends on whether the call states the scope explicitly (e.g., Foo::baz();)

BITS Pilani, Pilani Campus


Polymorphism and Generics
An operator or function is polymorphic if it can be applied to
any one of several related types
– Enables code re-use!

Example: generic functions in C++


– Function operates in exactly the same way regardless of the type of its
arguments

– For each use, compiler substitutes the actual type of the arguments for the ‘type’
template parameters

– This is an example of parametric polymorphism

slide 8
BITS Pilani, Pilani Campus
Type
A type is a collection of computable values that share
some structural property

 Examples  “Non-examples”
Integers 3, true, x.x
Strings
int  bool
(int  int) bool

Distinction between sets that are types and sets that are
not types is language-dependent slide 9
BITS Pilani, Pilani Campus
Uses for Types
Program organization and documentation
– Separate types for separate concepts

• Represent concepts from problem domain


– Indicate intended use of declared identifiers

• Types can be checked, unlike program comments


Identify and prevent errors
– Compile-time or run-time checking can prevent meaningless computations such
as 3 + true - “Bill”

Support optimization
– Example: short integers require fewer bits
– Access record component by known offset

slide 10
BITS Pilani, Pilani Campus
Operations on Typed Values
Often a type has operations defined on values of this type
– Integers: + - / * < > … Booleans:    …

Set of values is usually finite due to internal binary


representation inside computer
– 32-bit integers in C: –2147483648 to 2147483647
– Addition and subtraction may overflow the finite range, so sometimes a + (b +
c)  (a + b) + c
– Exceptions: unbounded fractions in Smalltalk, unbounded Integer type in Haskell
– Floating point problems

slide 11
BITS Pilani, Pilani Campus
Type Errors
Machine data carries no type information
– 01000000010110000000000000000000 means…
– Floating point value 3.375? 32-bit integer 1,079,508,992? Two 16-bit integers
16472 and 0? Four ASCII characters @ X NUL NUL?

A type error is any error that arises because an operation is


attempted on a value of a data type for which this
operation is undefined
– Historical note: in Fortran and Algol, all of the types were built in. If needed a type
“color,” could use integers, but what does it mean to multiply two colors?

slide 12
BITS Pilani, Pilani Campus
Static vs. Dynamic Typing
Type system imposes constraints on use of values
– Example: only numeric values can be used in addition
– Cannot be expressed syntactically in EBNF

Language can use static typing


– Types of all variables are fixed at compile time
– Example?

… or dynamic typing
– Type of variable can vary at run time depending on value assigned to this
variable
– Example?

slide 13
BITS Pilani, Pilani Campus
Strong vs. Weak Typing
A language is strongly typed if its type system allows all
type errors in a program to be detected either at compile
time or at run time
– A strongly typed language can be either statically or dynamically typed!

Most dynamically typed languages associate a type with


each value

slide 14
BITS Pilani, Pilani Campus
Compile- vs. Run-Time Checking
Type-checking can be done at compile time
– Examples: C, ML f(x) must have f : A  B and x : A
… or run time
– Examples: Perl, JavaScript

Java does both


Basic tradeoffs Which gives better
– Both prevent type errors programmer diagnostics?
– Run-time checking slows down execution
– Compile-time checking restricts program flexibility

• JavaScript array: elements can have different


types

slide 15
BITS Pilani, Pilani Campus
Enumeration Types
User-defined set of values
– enum day {Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday, Sunday};
enum day myDay = Wednesday;
– In C/C++, values of enumeration types are represented as integers: 0, ..., 6

More powerful in Java:


– for (day d : day.values())
System.out.println(d);

slide 16
BITS Pilani, Pilani Campus
Pointers
C, C++, Ada, Pascal
Value is a memory address
– Remember r-values and l-values?

Allows indirect referencing


Pointers in C/C++
– If T is a type and ref T is a pointer:
& : T → ref T * : ref T → T *(&x) = x
Explicit access to memory via pointers can result in
erroneous code and security vulnerabilities

slide 17
BITS Pilani, Pilani Campus
Arrays
Example: float x[3][5];
Indexing []
– Type signature: T[ ] x int → T
– In the above example, type of x: float[ ][ ],
type of x[1]: float[ ], type of x[1][2]: float
Equivalence between arrays and pointers
– a = &a[0]
– If either e1 or e2 is type: ref T,
then e1[e2] = *((e1) + (e2))
– Example: a is float[ ] and i int, so a[i] = *(a + i)

slide 18
BITS Pilani, Pilani Campus
Strings
Now so fundamental, directly supported by languages
C: a string is a one-dimensional character array terminated
by a NULL character (value = 0)
Java, Perl, Python: a string variable can hold an
unbounded number of characters
Libraries of string operations and functions
– Standard C string libraries are unsafe!

slide 19
BITS Pilani, Pilani Campus
Structures
Collection of elements of different types
– Not in Fortran, Algol 60, used first in Cobol, PL/I
– Common to Pascal-like, C-like languages
– Omitted from Java as redundant

struct employeeType {
char name[25];
int age;
float salary;
};
struct employeeType employee;
...
employee.age = 45;

slide 20
BITS Pilani, Pilani Campus
Case Study : Vending
Machine
A Real Time Application solution for a Chocolate Vending Machine. A
Chocolate is dispensed only after inserting a proper coin. There is
provision for rejecting and accepting the coin.

If the coin was rejected, the system returns to its initial state;
Otherwise the system first Dispenses an item and then returns to
its initial state.

BITS Pilani, Pilani Campus

You might also like