Portable code is code that is not tightly coupled to one specific platform, or which is coupled as loosely as possible to platform-specific APIs. It is "portable" in that the amount of work required to move it from one platform to another is low.
Portable code is desirable when you intend to write code meant to be used by a large audience, on a wide variety of platforms.
Portability is primarily a concern in compiled languages, as interpreted languages typically rely on an interpreter to provide a uniform interface at runtime. It is still quite possible to write overly platform-specific code in an interpreted language by relying on features like backticks
or exec
for executing commands in the local environments, rather than accessing the same features through a library which may have different platform-specific implementations.
Libraries are often very concerned with portability as their primary purpose is to provide a consistent API across platforms.
Writing portable code involves minimizing the number of places your code must "reach down" and touch the underlying operating system outside of standard APIs. Typically you would encapsulate such access so that there is a single place in your codebase which must be ported from platform to platform while the bulk remains unchanged.