portability

18

Click here to load reader

Upload: chao-kung-liu

Post on 11-Jul-2015

102 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Portability

The Practice of Programming -

Portability

Author : Juggernaut Liu

Date : 2014/04/18

Page 2: Portability

Outline

• Introduction

• Language

• Headers and Libraries

• Program Organization

• Isolation

• Data Exchange

• Byte Order

• Portability and Upgrade

• Internationalization

• Summary

Page 3: Portability

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

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

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

Language (2)

• Signedness of char

Page 7: Portability

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

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

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

Avoid conditional compilation(1)

VS

Page 11: Portability

Avoid conditional compilation(2)

VS

Log4XXX can do it too.

Page 12: Portability

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

Data Exchange

• Use Text for data exchange

Page 14: Portability

Byte Order(1)

• Binary Data is more compact and faster

• little-endian vs big-endian

Page 15: Portability

Byte Order(2)

• Use a fixed byte order for data exchange

• Sender and receiver agree on the byte order in

transmission

Page 16: Portability

Portability and Upgrade

• Change the name if you change the specification

• Maintain compatibility with existing programs

and data

Page 17: Portability

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

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