Professional Documents
Culture Documents
Programming Systems
CS 217
Goals
• Master the art of programming
Learn how to be “good” programmers
Introduction to software engineering
1
Outline
• First four weeks
C programming
Coursework
• Six programming assignments (60%)
String library
Hash table ADT
Circuit simulator
Sparc assembly
Assembler
Shell
• Exams (30%)
Midterm
Final
• Class participation (10%)
2
Materials
• Required textbooks
C Programming: A Modern Approach, King
SPARC Architecture, etc. Paul
• Recommended textbooks
The Practice of Programming, Kernighan & Pike
Programming with GNU Software. Loukides & Oram
• Other textbooks
The C Programming Language, Kernighan & Ritchie
C: A Reference Manual. Harbison & Steele
C Interfaces and Implementations. Hanson
The UNIX Programming Environment. Kernighan & Pike
• Web pages
www.cs.princeton.edu/courses/cs217/
Facilities
• Unix machines
CIT’s arizona cluster
SPARC lab in Friend 016
3
Logistics
• Lectures
Introduce concepts
Work through programming examples
– M,W 10AM CS105
• Precepts
Review concepts
Demonstrate tools (gdb, makefiles, emacs, …)
Work through programming examples
– T,Th 1:30 Friend 110
– M,W 1:30 Friend 111
– TBA
Software is Hard
“What were the lessons I learned from so many years of intensive work
on the practical problem of setting type by computer? One of the most
important lessons, perhaps, is the fact that SOFTWARE IS HARD.
From now on I shall have significantly greater respect for every
successful software tool that I encounter. During the past decade I was
surprised to learn that the writing of programs for TeX and Metafont
proved to be much more difficult than all the other things I had done
(like proving theorems or writing books). The creation of good software
demands a significantly higher standard of accuracy than those other
things do, and it requires a longer attention span than other intellectual
tasks.”
4
Software in COS126
Specification 1 Person
1022 Lines of Code
1 Type of Machine
Design
0 Modifications
1 Week
Programming
Debugging
Testing
Debugging
Testing
5
Good Software in the Real World
• Understandable
Write
Write code
code inin modules
modules
Well-designed with
with well-defined
well-defined interfaces
interfaces
Consistent
Documented
• Robust Write
Write code
code in
in modules
modules
Works for any input and
and test
test them
them separately
separately
Tested
• Reusable Write
Write code
code in
in modules
modules
Components that
that can
can be
be used
used elsewhere
elsewhere
• Efficient
Only matters for 1% Write
Write code
code in
in modules
modules
and
and optimize the slow
optimize the slow ones
ones
• Robust Write
Write code
code in
in modules
modules
Works for any input and
and test
test them
them separately
separately
Tested
• Reusable Write
Write code
code in
in modules
modules
Components that
that can
can be
be used
used elsewhere
elsewhere
• Efficient
Only matters for 1% Write
Write code
code in
in modules
modules
and
and optimize the slow
optimize the slow ones
ones
6
Modules
• Programs are made up of many modules
• Each module is small and does one thing
string manipulation
mathematical functions
set, stack, queue, list, etc.
String List
Interfaces
• An interface defines what the module does
decouple clients from implementation
hide implementation details
• An interface specifies…
data types and variables
functions that may be invoked
StringList
StringList *StringList_create(void);
*StringList_create(void);
void
void StringList_delete(StringList
StringList_delete(StringList *list);
*list);
void
void StringList_insert(StringList
StringList_insert(StringList *list,
*list, char
char *string);
*string);
void
void StringList_remove(StringList
StringList_remove(StringList *list,
*list, char
char *string);
*string);
int
int StringList_write(StringList
StringList_write(StringList *list);
*list);
7
Implementations
• An implementation defines how the module does it
• Can have many implementations for one interface
different algorithms for different situations
machine dependencies, efficiency, etc.
StringList
StringList *StringList_create(void)
*StringList_create(void)
{{
StringList
StringList *list
*list == malloc(sizeof(StringList));
malloc(sizeof(StringList));
list->entries
list->entries == NULL;
NULL;
list->size
list->size == 0;
0;
}}
void
void StringList_delete(StringList
StringList_delete(StringList *list)
*list)
{{
free(list);
free(list);
}}
etc.
etc.
Clients
• A client uses a module via its interface
• Clients see only the interface
can use module without knowing its implementation
int
int main()
main()
{{
StringList
StringList *list
*list == StringList_create();
StringList_create();
StringList_insert(list,
StringList_insert(list, “CS217”);
“CS217”);
StringList_insert(list,
StringList_insert(list, “is”);
“is”);
StringList_insert(list,
StringList_insert(list, “fun”);
“fun”);
StringList_write(list);
StringList_write(list);
StringList_delete(list);
StringList_delete(list);
}}
8
Clients, Interfaces, Implementations
• Interfaces are contracts between
clients and implementations
Clients must use interface correctly
Implementations must do what they advertise
Client
Interface
Implementation
9
C Programming Conventions
• Interfaces are defined in header files (.h)
stringlist.h
StringList
StringList *StringList_create(void);
*StringList_create(void);
void
void StringList_delete(StringList
StringList_delete(StringList *list);
*list);
void
void StringList_insert(StringList
StringList_insert(StringList *list,
*list, char
char *string);
*string);
void
void StringList_remove(StringList
StringList_remove(StringList *list,
*list, char
char *string);
*string);
void
void StringList_write(StringList
StringList_write(StringList *list);
*list);
etc.
etc.
C Programming Conventions
• Implementations are described in source files (.c)
stringlist.c
#include
#include “stringlist.h”
“stringlist.h”
StringList
StringList *StringList_create(void)
*StringList_create(void)
{{
StringList
StringList *list
*list == malloc(sizeof(StringList));
malloc(sizeof(StringList));
list->entries
list->entries == NULL;
NULL;
list->size
list->size == 0;
0;
}}
void
void StringList_delete(StringList
StringList_delete(StringList *list)
*list)
{{
free(list);
free(list);
}}
etc.
etc.
10
C Programming Conventions
• Clients “include” header files
main.c
#include
#include “stringlist.h”
“stringlist.h”
int
int main()
main()
{{
StringList
StringList *list
*list == StringList_create();
StringList_create();
StringList_insert(list,
StringList_insert(list, “CS217”);
“CS217”);
StringList_insert(list,
StringList_insert(list, “is”);
“is”);
StringList_insert(list,
StringList_insert(list, “fun”);
“fun”);
StringList_write(list);
StringList_write(list);
StringList_delete(list);
StringList_delete(list);
}}
Standard C Libraries
assert.h assertions
ctype.h character mappings
errno.h
math.h error numbers
limits.h math functions
signal.h metrics for ints
stdarg.h signal handling
stddef.h variable length arg lists
stdio.h
stdlib.h standard definitions
string.h standard I/O
time.h standard library functions
string functions
date/type functions
11
Standard C Libraries (cont)
• Utility functions stdlib.h
atof, atoi, rand, qsort, getenv,
calloc, malloc, free, abort, exit
12
Summary
• We will learn good programming in first third of this class
• A key to good programming is modularity
A program is broken up into meaningful modules
An interface defines what a module does
An implementation defines how the module does it
A client sees only the interfaces, not the implementations
13