portability
TRANSCRIPT
![Page 1: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/1.jpg)
The Practice of Programming -
Portability
Author : Juggernaut Liu
Date : 2014/04/18
![Page 2: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/2.jpg)
Outline
• Introduction
• Language
• Headers and Libraries
• Program Organization
• Isolation
• Data Exchange
• Byte Order
• Portability and Upgrade
• Internationalization
• Summary
![Page 3: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/3.jpg)
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.
![Page 4: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/4.jpg)
Introduction
Why do we worry about portability?
• Less maintenance and more utility
• Environments change
• A portable program is a better designed program
![Page 5: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/5.jpg)
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()
![Page 6: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/6.jpg)
Language (2)
• Signedness of char
![Page 7: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/7.jpg)
Language (3)
• Alignment of structure and class members
o sizeof (struct X) bytes,
o not sizeof (char) + sizeof(int)
• Try several compilers
![Page 8: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/8.jpg)
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
![Page 9: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/9.jpg)
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
![Page 10: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/10.jpg)
Avoid conditional compilation(1)
VS
![Page 11: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/11.jpg)
Avoid conditional compilation(2)
VS
Log4XXX can do it too.
![Page 12: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/12.jpg)
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
![Page 13: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/13.jpg)
Data Exchange
• Use Text for data exchange
![Page 14: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/14.jpg)
Byte Order(1)
• Binary Data is more compact and faster
• little-endian vs big-endian
![Page 15: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/15.jpg)
Byte Order(2)
• Use a fixed byte order for data exchange
• Sender and receiver agree on the byte order in
transmission
![Page 16: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/16.jpg)
Portability and Upgrade
• Change the name if you change the specification
• Maintain compatibility with existing programs
and data
![Page 17: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/17.jpg)
Internationalization
• Do not assume ASCII
o Unicode
o UTF-8
o Wide Characters (C/C++)
• Do not assume English
o L10N
o UI
![Page 18: Portability](https://reader038.vdocuments.net/reader038/viewer/2022100606/55a04fb31a28abad578b4633/html5/thumbnails/18.jpg)
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