Lecture Notes for Advanced Programming II

29 March 2001 - Portability


  1. what is portability

    1. it is easier to move and modify portable code than it is to toss and rewrite it

    2. this ignores the information embedded in code

  2. why worry about portability - nobody else is ever going to use this code

    1. you're probably wrong

    2. portable in space and time

    3. good programmers should worry about portability

    4. bad programmers don't need to

  3. writing portable code

    1. if there's a standard, write to it

      1. watch out for old languages standardized - c, c++, pascal, java (eventually)

      2. some languages are born standard - ada

      3. some languages are defined by an operational standard - java, perl, tcl, python

      4. don't stray from the well-trod path

        1. if you don't know what it is don't use it - trigrams

        2. if it helps portability, use it - prototypes

      5. what if there's several standards

        1. you can cover all features or only the common ones

        2. covering all features is expensive, complicated, and unreadable

        3. covering only common features is easier, more straightforward, but limited

    2. avoid trouble spots

      1. data-type size - sizeof(int) ? sizeof(int *)

      2. order of evaluation - a[i++] = i; parameter arguments

      3. char signedness

      4. arithmetic or logical shifts

      5. byte order

  4. headers and libraries

    1. use standard libraries

    2. don't use header files for portability

  5. program organization

    1. do not use conditional compilation

    2. use interface-implementation

      1. .h file gives the interface - socket.h

      2. .cc files give the implementation - solaris-socket.cc, linus-socket.cc, nt-socket.cc

      3. virtual classes are good for this; so is wrappers

  6. create portable data too

    1. text, text, text - inherently portible, self documenting

    2. store documentation in the text itself - descriptions, generation commands

    3. watch out for the end-of-line character - '\n' helps here

    4. binary is non-portable, incomprehensible, compact and fast

    5. byte order

      1. the endian-ness of the data - where's the most significant byte

      2. this is a common problem - between sparcs and pc

      3. write multi-byte values byte-by-byte in a standard order

  7. dealing with unportability

    1. don't make portable code unportable by including changes made on one system and not the other

    2. don't gratuituously change function - compatability is important

    3. if you do, rename the code

  8. internationalization - it's a big world

    1. many things change from country to country - language, time and date, money, big numbers

    2. character sets - ascii (latin-1), Unicode (iso 10646); wide chars

    3. the c++ standard locale library helps

      1. the local object encapsulates the changes between countries
        #include 
        #include 
        #include "date.h" 
        
        int main() {
          using namespace std; 
          using ChronLib::Date;
          cout.imbue(locale(""));
          cout << Date(1942, Date::dec, 7) << endl;
          return 0;
          }
        

      2. facets of a locale object deal with difference in a particular aspect of the environment
        time_put<char> const&  timeFacet = use_facet< time_put<char> >(os.getloc());
        

    4. you can get a job doing it
      JOB TITLE: WEB LOCALIZATION ENGINEER

      POSITION LOCATION: San Francisco

      Overall responsibility for all HTML, PERL, CGI, JavaScript, ASP, and related coding analysis and production; interface and coordinate with project management and art/graphics personnel, sales, and customers; provide input to pre-sale planning and estimating of projects; provide support to operations and sales management, as required.


This page last modified on 29 March 2001.