当函数/方法被调用时候,传递给它的任务参数都会成为执行上下文的一部分

https://stackoverflow.com/questions/6145091/the-term-context-in-programming

Apalala 的回答:

Context refers to the execution context, which is the symbols reachable from a given point in the code, and the value of those symbols in that particular execution.

Context is an important concept because:

1.Executable units (functions, procedures, instructions) may produce different results or behave differently under different contexts.
2.The larger or more complex the context, the more difficult to understand what a piece of code does (that's why global variables are shunned upon).

You do not have to write context classes or pass context parameters. Any parameter passed to a function/method becomes part of the execution context when it is invoked.

Even though you're not an English speaker, I recommend you go through a copy of Code Complete for a gentle yet thorough introduction to concepts like context, modularity, coupling, cohesion, and so forth.

上下文指的是执行上下文,也就是从代码中的某一点可以到达的符号,以及这些符号在特定执行中的值。

上下文是一个重要的概念,因为

可执行单元(函数、过程、指令)在不同的上下文下可能会产生不同的结果或行为。

上下文越大或越负责,就越难理解一段代码的作用(这就是全局变量被回避的原因)。

你不必编写上下文类或传递上下文参数。当函数/方法被调用时候,传递给它的任务参数都会成为执行上下文的一部分

即使你不懂英语,我也建议你去读一读《代码大全》(Code Complete),温和而透彻地了解上下文、模块化、耦合、内聚等概念。

Brian Kelly 的回答:

Let's say you go to the dentist to have a tooth pulled out.

When the receptionist asks you for your name, that's information they need in order to begin the appointment. In this example, your name is contextual information. So in the context of visiting the dentist, you need to provide your name to get your tooth pulled.

Now let's say you walk over to the bank.

At the bank, you ask to withdraw $100. The teller needs to establish your identity before giving you money, so you'll probably have to show them a driver's license or swipe your ATM card and enter your PIN number. Either way, what you're providing is context. The teller uses this information to move the transaction forward. They may then ask you which account you'd like to withdraw from. When you answer, "My savings account", that's even more context.

The more context you give, the more knowledge the other party has to help deal with your request. Sometimes context is optional (like typing more and more words into your Google search to get better results) and sometimes it's required (like providing your PIN number at the ATM). Either way, it's information that usually helps to get stuff done.

Now let's say you take your $100 and buy a plane ticket to fly somewhere warm while your mouth heals.

You arrive at a nice sunny destination, but your bag doesn't make it. It's lost somewhere in the airport system. So, you take your "baggage claim ticket" (that sticker with the barcode on it) to the "Lost Baggage office". The first thing the person behind the desk will ask for is that ticket with your baggage number on it. That's an example of some required context.

But then the baggage person asks you for more information about your bag like so they can find it more easily. They ask, "What color is it? What size is it? Does it have wheels? Is it hard or soft? While they don't necessarily need those pieces of information, it helps narrow things down if you provide them. It reduces the problem area. It makes the search much faster. That's optional context.

Here's the interesting part: for a lot of software and APIs, the required context usually ends up as actual parameters in a method signature, and optional context goes somewhere else, like a flexible key-value map that can contain anything (and may be empty) or into thread-local storage where it can be accessed if needed.

The examples above are from real life, but you can easily map them to areas within computer science. For example, HTTP headers contain contextual information. Each header relates to information about the request being made. Or when you're sending along a global transaction ID as part of a two-phase commit process, that transaction ID is context. It helps the transaction manager coordinate the work because it's information about the overall task at hand.