COP 3402 Lecture -*- Outline -*- * Information Hiding in C idea is to localize information in a program in order to make maintenance easier. (C doesn't support a complete solution to for this) another benefit is to reduce the size of the global namespace ** Modularity *** Examples in Real Life ------------------------------------------ EXAMPLES OF MODULARITY IN REAL LIFE Electric plugs, can plug in anything: - toaster, - TV - computer - phone charger - coffee maker Cooking ingredients: - meat (chicken, etc.) - butter - fruits - spices What makes a system modular? ------------------------------------------ ... - an agreed upon interface (plug, heating element, pots, ...) - allows one side to provide a service or utility (e.g., power) and the other to use it, perhaps to provide another service) Q: What other examples of modular systems are there? Gas stations (provide gasoline) Hardware (nails, screws, lumber, ...) Q: Does a modular system allow for competition? Yes, it's one of the main ways companies can compete Q: If you are selling gasoline, do you need customers who understand all of its properties and how it was made? (No, it's not a need, but it would be great if they only bought yours...) Q: How much do you want and need to know about the gasoline you buy? (probably not much, just the "kind" e.g., regular) Customers want to be able to use any gas of the right kind... *** Examples in software ------------------------------------------ EXAMPLES OF MODULARITY IN SOFTWARE SQL queries work on: - many different databases - on many different computers - give same results TCP/IP network protocol: - connects many different systems (different hardware, different OS) - clients include: browsers, email, ... Mathematical libraries: - correct computation of trig. functions - on many different computers - approximately same results ------------------------------------------ ... each is a language that defines syntax and semantics that allow different implementations Q: What other examples are there of modularity in software? HTML and JSON are standard for communicating information LinkedIn and other social networks have interface defined by their web service (and also APIs) Twitter had an API for finding data from users/tweets *** software terminology **** roles: client and implementation ------------------------------------------ MODULARITY IN SOFTWARE 2 roles: - client: - implementation: Agreed upon interface: - names and how they can be used ------------------------------------------ ... uses the service ... provides the service ... e.g., functions, types called an API Q: Does an API allow for different implementations? Yes, as long as the implementation of the interface follows the specification Q: If there is a standard API, what can change? The implementation - performance and reliability (if not specified) - implementation algorithms and data structures **** modules ------------------------------------------ MODULES IN PROGRAMMING A module: In programming want to hide decisions about: ------------------------------------------ ... - provides a service - hides some decision (to protect IP or to ease maintenance) ... - algorithms - data structures - how algorithms and data structures work together (to provide a service) *** information hiding ------------------------------------------ INFORMATION HIDING Some information Ideally, information about decisions that are likely to change e.g.: ------------------------------------------ ... is kept from clients and is only known in the implementation ... - information that needs to be stored/remembered - data structures - algorithms Note: conventional wisdom is that the most frequent changes require changes to data structures (often to track more data or more kinds of data, to enable some new computation) Q: What are some examples of changing decisions? Y2K: programs needed to keep 4 digits for year Enhancements to (office suite) programs.... *** Programming Language Support **** kinds of language support ------------------------------------------ PROG. LANG. SUPPORT FOR INFORMATION HIDING Limit clients: - discovery of secrets - manipulation of secrets - creation of secrets Mechanisms: - function mechanisms - scope limitations - export/import of names - type limitations ------------------------------------------ ... hide algorithms ... names private to area of program ... public/private hiding of names (names known only inside) ... implementation can use new type(s) to make data as its own (enforce properties) **** information hiding in C ------------------------------------------ INFO. HIDING IN C Modules are Names private to module: Names available to clients: ------------------------------------------ ... files ... declared as static ... are not static (declared as extern) Note: name hiding only protects data if clients can never directly manipulate it ------------------------------------------ EXAMPLE IN C: SET OF STRINGS // FILE string_set.h #ifndef _STRING_SET_H #define _STRING_SET #include // Initialize the string set extern void string_set_init(); // Put s into the set extern void string_set_add(const char *s); // Is s in the set? extern bool string_set_has(const char *s); // ... #endif ------------------------------------------ Q: How could we implement this API? As an array, a list, a binary search tree, hash table, ... ------------------------------------------ ONE IMPLEMENTATION // FILE string_set.c #include #include #include #include #include "string_set.h" typedef struct element_s { const char *val; struct element_s *next; } element; static element *elements; // invariant: elements is // a singly-linked list with no cycles // invariant: no duplicates // in the list of elements // Initialize the string set void string_set_init() { elements = NULL; } // Put s into the set void string_set_add(const char *s) { if (!string_set_has(s)) { // s is not already in the list element *newelem = malloc(sizeof(element)); if (newelem == NULL) { fprintf(stderr, "No space!\n"); exit(EXIT_FAILURE); } newelem->val = s; newelem->next = elements; elements = newelem; } assert(string_set_has(s)); } // Is s in the set? extern bool string_set_has(const char *s) { element *p = elements; while (p != NULL) { if (strcmp(p->val, s) == 0) { return true; } p = p->next; } return false; } // ... ------------------------------------------ Q: What is hidden in this implementation? The decision to use a linked list, the algorithms for linked lists, the decision to not allow duplicates the name elements, the type element (since it's not in a header) declaring names as static hides that name from clients Q: Can clients make the invariant untrue? No, (unless they directly manipulate the representation) that is established by the initialization, and preserved by each function Q: What is exported by this implementation? The functions: string_set_initialize, string_set_add, string_set_has, ... *** Abstract Data Type ------------------------------------------ ABSTRACT DATA TYPES def: an *abstract data type* (ADT) is Benefits: ------------------------------------------ ... a set of values (objects) and operations on them (functions/methods) with a specified behavior ... allows clients to ignore implementation details (so their programming is more efficient, effective) This is a key motivation for object-oriented programming Q: What are the drawbacks of using ADTs? The program may be slower, because data is manipulated in an abstract way However: an ADT is easier to optimize than a non-ADT implementation, since data structures and algorithms are all in one place