Professional Documents
Culture Documents
RTS Lecture 8 1646473580573
RTS Lecture 8 1646473580573
presentation
BITS Pilani Pawan Gupta
Pilani Campus
BITS Pilani
Pilani Campus
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
– For each use, compiler substitutes the actual type of the arguments for the ‘type’
template parameters
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
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: …
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?
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
… 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!
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
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
slide 16
BITS Pilani, Pilani Campus
Pointers
C, C++, Ada, Pascal
Value is a memory address
– Remember r-values and l-values?
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.