一段代码被认为是可移植的,这意味着什么? [英] What is meant when a piece of code is said to be portable?

查看:80
本文介绍了一段代码被认为是可移植的,这意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在许多文档中都看到一段代码是可移植的.

I have seen in many documentations that a piece of code is portable.

任何人都可以向我解释确切地是什么意思将一段代码称为可移植的吗?

Can anyone explain to me exactly what it means to call a piece of code portable?

推荐答案

可移植代码是未紧密耦合到一个特定平台的代码,或者是尽可能宽松地耦合至特定于平台的API的代码.之所以便携",是因为将其从一个平台迁移到另一个平台所需的工作量很少.

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.

可移植性在编译语言中主要是一个问题,因为解释语言通常依赖于解释器在运行时提供统一的接口.依靠backticksexec之类的功能在本地环境中执行命令,而不是通过可能具有不同平台的库访问相同的功能,仍然有可能用解释性语言编写过多的特定于平台的代码特定的实现.

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.

库通常非常关心可移植性,因为它们的主要目的是在跨平台之间提供一致的API.

Libraries are often very concerned with portability as their primary purpose is to provide a consistent API across platforms.

编写可移植代码涉及将代码必须到达"并接触标准API之外的底层操作系统的位置的数量减至最少.通常,您将封装此类访问权限,以便在代码库中只有一个位置,该位置必须从一个平台移植到另一个平台,而大部分保持不变.

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.

这篇关于一段代码被认为是可移植的,这意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆