Code as Part of the Software Development Process
Diomidis Spinellis
Department of Management Science and Technology
Athens University of Economics and Business
Athens, Greece
dds@aueb.gr
Code as a Scientific Communication Vehicle
Social processes that contributed to the success of mathematical theorems:
- Publication and review
- Discussion, internalizing, generalizing, paraphrasing
- Used for solving real problems
Gains from High-quality Code
We learn:
- New architectural patterns
- Data structures
- Coding methods
- Algorithms
- Style conventions
- Documentation mechanisms
- APIs
- Languages
Recognize Low-quality Code
- Inconsistent coding style
- Gratuitously complicated or unreadable structure
- Obvious logical errors, or omissions
- Overuse of non-portable constructs
- Lack of maintenance
- Inefficient or insecure
Requirements Can Lead to Strange Code
- Portability (e.g. 6 character identifiers)
- Efficiency
- Space restrictions
- Present an idea
- Protect against reverse engineering
How to Familiarize Yourself with a Body of Code
Don't Panic! (typically you don't have to understand all the code)
Four steps:
- Build
- Browse
- Improve
- Contribute
What you gain from OSS is a loan that you have to repay.
Code Reading Reasons
- Code as Exemplar
- Maintenance
- Evolution
- Porting
- Refactoring
- Reuse
- Inspections
Code as Exemplar
Reasons
- Understand how it works
- Create compatible software
Strategy
- Try multiple approaches
- Search
- Browse
- Read documentation
- Run
- Run under a debugger
- Trace
- Ignore irrelevant details
Example: tail -f (NetBSD)
while ((ch = getopt(argc, argv, "b:c:fn:r")) != -1)
switch(ch) {
[...]
case 'f':
fflag = 1;
break;
}
[...]
for (;;) {
while ((ch = getc(fp)) != EOF)
if (putchar(ch) == EOF)
oerr();
[...]
if (!fflag)
break;
/*
* We pause for one second after displaying any data that has
* accumulated since we read the file. Since sleep(3) takes
* eight system calls, use select() instead.
*/
second.tv_sec = 1;
second.tv_usec = 0;
if (select(0, NULL, NULL, NULL, &second) == -1)
err(1, "select: %s", strerror(errno));
clearerr(fp);
}
Example: tail -f (FreeBSD)
if (fflag) {
kq = kqueue();
if (kq < 0)
err(1, "kqueue");
action = ADD_EVENTS;
}
for (;;) {
while ((ch = getc(fp)) != EOF)
if (putchar(ch) == EOF)
oerr();
[...]
if (! fflag)
break;
clearerr(fp);
switch (action) {
case ADD_EVENTS:
n = 0;
ts.tv_sec = 0;
ts.tv_nsec = 0;
if (Fflag && fileno(fp) != STDIN_FILENO) {
EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
EV_ADD | EV_ENABLE | EV_CLEAR,
NOTE_DELETE | NOTE_RENAME, 0, 0);
n++;
}
EV_SET(&ev[n], fileno(fp), EVFILT_READ,
EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
n++;
if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
action = USE_SLEEP;
} else {
action = USE_KQUEUE;
}
break;
kqueue, kevent - kernel event notification mechanism
Maintenance and its Tools
- Use tools
- Compiler warnings
- Examine symbolic code
- System call tracing
- Debugger
- SQL log
- Packet dump
- Message spy
- Work from problem manifestation to problem source
- Eschew unrelated paths
- Compile with debugging options
- Run under a debugger
- Single step
- Stack trace
- Code breakpoint
- Data breakpoint
- Add print statements
Evolution
Maintenance activities often take 80% of the effort over a complete
life cycle.
Maintenance Activities
- Add new functionality
- Modify existing features
- Adapt to new environments
- Refactor (enhance nonfunctional qualities)
Strategy
- Locate code of interest
- Understand part in isolation
- Infer relationship with rest of the code
Example: locating the authentication code in the ftp command.
Porting
- Compile
- Remove compiler errors
- Remove linker errors
- Examine warnings
- Configure
- Test
If the two environments differ a lot: focus on interfaces.
Refactoring
- Leaves external behavior unchanged.
- Improves:
- Simplicity
- Flexibility
- Understandability
- Performance
- In common with cosmetic surgery: start and end with a working system.
- Use test cases
- Scenarios:
- Fix trouble spot
- Spending "quality time"
Reuse
- Limit your expectations
- Reusable software is software that has been reused
- Making software reusable can add 50% to the effort
- Strategy:
- Isolate code
- If fails, try larger granularity packaging (library, component, process, system)
- Make it a habit to look for reusable code
Inspections
- Technical review
- Walkthrough
- Inspection
- Pair programming
- Strategy:
- Thoroughness
- Examine tricky constructs
- Verify against specifications
- Remember nonfunctional properties
- Remember non-code elements
Further Reading
- Phillip G. Armour.
The case for a new business model: Is software a product or a medium?
Communications of the ACM, 43(8):19–22, August 2000.
- Kent Beck.
Extreme Programming Explained: Embrace Change.
Addison-Wesley, Boston, MA, 2000.
- Jon Louis Bentley
and Donald E. Knuth.
Programming pearls: A tt WEB program for sampling.
Communications of the ACM, 29(5):364–369, May 1986.
- Jon Louis Bentley,
Donald E. Knuth, and Douglas McIlroy.
A literate program.
Communications of the ACM, 19(6):471–483, June 1986.
- Barry W. Boehm, Bradford
Clark, Ellis Horowitz, Ray Madachy, Richard Shelby, and Chris Westland.
Cost
models for future life cycle processes: COCOMO 2.
Annals of Software Engineering, 1:57–94, 1995.
- Barry W. Boehm.
Software Engineering Economics.
Prentice-Hall, Englewood Cliffs, NJ, 1981.
- Barry W. Boehm.
The economics of software maintenance.
In Software Maintenance Workshop, pages 9–37, 1983.
- R. DeMillo, R. Lipton,
and A. Perlis.
Social processes and proofs of theorems and programs.
In Proc. Fourth ACM Symposium on Principles of Programming
Languages, pages 206–214, New York, January 1977. ACM Press.
- Khaled El-Emam.
Ethics and open source.
Empirical Software Engineering, 6(4):291–292, December 2001.
- Michael Feathers.
Working Effectively with Legacy Code.
Prentice-Hall, Englewood Cliffs, NJ, 2005.
- Martin Fowler.
Refactoring: Improving the Design of Existing Code.
Addison-Wesley, Boston, MA, 2000.
With contributions by Kent Beck, John Brant, William Opdyke, and Don
Roberts.
- Richard P. Gabriel
and Ron Goldman.
Mob software: The erotic
life of code.
Presented at the ACM Conference on Object-Oriented Programming, Systems,
Languages, and Applications on October 19, 2000, in Minneapolis, Minnesota.,
October 2000.
Online http://www.dreamsongs.com/MobSoftware.html. Current May 2002.
- Robert L. Glass.
The sociology of open source: Of cults and cultures.
IEEE Software, 17(3):104–105, May/June 2000.
- Eric Hamilton.
Literate programming—expanding generalized regular expressions.
Communications of the ACM, 31(12):1376–1385, December
1988.
- David R. Hanson.
Literate programming—printing common words.
Communications of the ACM, 30(7):594–599, July 1987.
- Andy Hunt and Dave
Thomas.
Software archeology.
IEEE Software, 19(2):20–22, March/April 2002.
- Michael A. Jackson.
Literate programming—processing transactions.
Communications of the ACM, 30(12):1000–1010, December
1987.
- Brian W. Kernighan
and P. J. Plauger.
Software Tools.
Addison-Wesley, Reading, MA, 1976.
- Gregor Kiczales, Erik
Hilsdale, Jim Hugunin, Mik Kersten, Jeffrey Palm, and William G. Griswold.
Getting started with AspectJ.
Communications of the ACM, 44(10):59–65, October 2001.
- Donald E. Knuth.
METAFONT: The Program.
Addison-Wesley, Reading, MA, 1986.
- Donald E. Knuth.
TeX:
The Program.
Addison-Wesley, Reading, MA, 1986.
- Donald E. Knuth.
Literate Programming.
CSLI Lecture Notes Number 27. Stanford University Center for the Study of
Language and Information, Stanford, CA, 1992.
Distributed by the University of Chicago Press.
- Charles W. Krueger.
Software reuse.
ACM Computing Surveys, 24(2):131–183, June 1992.
- B. P. Lientz and
E. B. Swanson.
Software Maintenance Management.
Addison-Wesley, Reading, MA, 1980.
- John Lions.
Lions'
Commentary on Unix 6th Edition with Source Code.
Annabooks, Poway, CA, 1996.
- Hafedh Mili, Fatma Mili,
and Ali Mili.
Reusing software: Issues and research directions (http://dlib.computer.org/dynaweb/ts/ts1995/@Generic__BookTextView/101764;hf=0).
IEEE Transactions on Software Engineering, 21(6):528–562, June
1995.
- Charles Petzold.
Code:
The Hidden Language of Computer Hardware and Software.
Microsoft Press, Redmond, WA, 1999.
- Roger S. Pressman.
Software Engineering: A Practitioner's Approach.
McGraw-Hill, London, fifth edition, 2000.
European Adaptation. Adapted by Darrel Ince.
- Cristiane S. Ramos,
Káthia M. Oliveira, and Nicolas Anquetil.
Legacy software evaluation model for outsourced maintainer.
In Eighth Euromicro Working Conference on Software Maintenance and
Reengineering (CSMR'04), pages 48–57. IEEE Computer Society, March
2004.
- Eric S. Raymond.
The
Cathedral and the Bazaar: Musings on Linux and Open Source by an Accidental
Revolutionary.
O' Reilly and Associates, Sebastopol, CA, 2001.
- Diomidis Spinellis
and Konstantinos Raptis.
Component mining: A process and its pattern language (http://www.dmst.aueb.gr/dds/pubs/jrnl/2000-IST-Components/html/comp.html).
Information and Software Technology, 42(9):609–617, June
2000.
- Diomidis Spinellis.
Explore, excogitate, exploit: Component mining (http://www.dmst.aueb.gr/dds/pubs/jrnl/1999-Computer-Components/html/comp.html).
IEEE Computer, 32(9):114–116, September 1999.
- Diomidis Spinellis.
Code Reading: The Open
Source Perspective, pages 1–17.
Effective Software Development Series. Addison-Wesley, Boston, MA, 2003.
- Christopher J. Van Wyk
and Donald C. Lindsay.
Literate programming: A file difference program.
Communications of the ACM, 32(6):740–755, June 1989.
Exercises and Discussion Topics
- Are there examples of "open-source" communication in other
engineering disciplines?
- Discuss possible business models behind an open-source distribution.
- Download, build from source, and install
a small open-source program. (easy)
- Install an open-source operating system on your PC (e.g.
FreeBSD (http://www.freebsd.org),
Debian (http://www.debian.org),
NetBSD (http://www.netbsd.org)).
(moderate)
- Modify your system's kernel to log all accesses to files in the
/etc directory. (difficult)