Portable code
Portable code is portable, because it was designed to be portable.A fairly simple statement but rarely followed. Here is a list of rules to keep inmind when designing your code. You cannot be expected to follow everything, justkeep these ideas in mind.
- Design your code before you write your code. Here you can consider porting issues.
- Encapsulate. This generally means do not call the operating system or compiler specific API procedures directly unless you have source code to the API procedures. You should encapsulate types, constants and procedures. For example the runtime library modules are providing an encapsulation of operating system services such as file operations and executing programs.
- Encapsulate procedure function, not just procedure names. When writing an encapsulation layer do not just hide the names of procedures and parameters. You should encapsulate a logical function. If this maps directly to a single procedure call of what you are encapsulating then great. You should define the API you want, and then make that work using the services available to you.
- Avoid type casts. This is an issue when converting compilers, and when converting from a little endian data format to a big endian data format. Type conversions are always better. The best is to never need the casts or conversions because the types are the same.