portability
TRANSCRIPT
The Practice of Programming -
Portability
Author : Juggernaut Liu
Date : 2014/04/18
Outline
• Introduction
• Language
• Headers and Libraries
• Program Organization
• Isolation
• Data Exchange
• Byte Order
• Portability and Upgrade
• Internationalization
• Summary
Robert Venturi, Complexity and Contradiction in Architecture
Finally, standardization, like convention, can be another manifestation of the strong order. But unlike convention it has been accepted in Modern architecture as an enriching product of our technology, yet dreaded for its potential domination and brutality.
Introduction
Why do we worry about portability?
• Less maintenance and more utility
• Environments change
• A portable program is a better designed program
Language (1)
• Stick to the standard
• Program in the mainstream
• Sizes of data types
o sizeof (char) <= sizeof (short) <= sizeof (int) <= sizeof (long)
o sizeof (float) <= sizeof (double)
• Order of evaluation
o n = (getchar() <<8) | getchar()
Language (2)
• Signedness of char
Language (3)
• Alignment of structure and class members
o sizeof (struct X) bytes,
o not sizeof (char) + sizeof(int)
• Try several compilers
Headers and Libraries
• Use standard libraries
• Problems
o Headers tend to be cluttered
o Header files also can "pollute" the namespace by
declaring a function with the same name
• Solutions
o Use a different header for each compiler or environment
o Redefining the name
Program Organization
• Union vs Intersection
o Conditional compilation
o Include a header which defines all
• Suggestion
o Use only features available everywhere
o Avoid conditional compilation
Avoid conditional compilation(1)
VS
Avoid conditional compilation(2)
VS
Log4XXX can do it too.
Isolation
• Localize system dependencies in separate files
• Hide system dependencies behind interfaces
o Abstraction
o The I/O libraries are good samples
• Java is a good sample
Data Exchange
• Use Text for data exchange
Byte Order(1)
• Binary Data is more compact and faster
• little-endian vs big-endian
Byte Order(2)
• Use a fixed byte order for data exchange
• Sender and receiver agree on the byte order in
transmission
Portability and Upgrade
• Change the name if you change the specification
• Maintain compatibility with existing programs
and data
Internationalization
• Do not assume ASCII
o Unicode
o UTF-8
o Wide Characters (C/C++)
• Do not assume English
o L10N
o UI
Summary
• Portable code is an ideal that is well worth striving for, since
so much time is wasted making changes to move a program
from one system to another or to keep it running as it evolves
and the systems it runs on changes.
• The intersection approach is better than the union one.
o Loss of efficiency and features
o Z>B