<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: John Wright</title>
    <description>The latest articles on DEV Community by John Wright (@mrjjwright).</description>
    <link>https://dev.to/mrjjwright</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F221526%2F40d0dff5-fa85-4bd0-91de-f24116b31713.jpeg</url>
      <title>DEV Community: John Wright</title>
      <link>https://dev.to/mrjjwright</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrjjwright"/>
    <language>en</language>
    <item>
      <title>Context vs. State: Why Context is King in Software Systems</title>
      <dc:creator>John Wright</dc:creator>
      <pubDate>Tue, 07 Jan 2025 20:15:19 +0000</pubDate>
      <link>https://dev.to/mrjjwright/context-vs-state-why-context-is-king-in-software-systems-1jh6</link>
      <guid>https://dev.to/mrjjwright/context-vs-state-why-context-is-king-in-software-systems-1jh6</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Context vs. State: Why Context is King in Software Systems&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;In software systems, especially interactive ones like &lt;strong&gt;Visual Studio Code (VS Code)&lt;/strong&gt;, the concepts of &lt;strong&gt;state&lt;/strong&gt; and &lt;strong&gt;context&lt;/strong&gt; are often conflated. While &lt;strong&gt;state&lt;/strong&gt; represents a snapshot of the system at a given moment, &lt;strong&gt;context&lt;/strong&gt; is what gives meaning to that state and drives the system’s behavior. In this blog post, we’ll explore the distinction between state and context, how VS Code’s &lt;strong&gt;ContextKeyService&lt;/strong&gt; leverages context, and why context is fundamentally above state in the hierarchy of system design.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;State vs. Context: What’s the Difference?&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;State: A Snapshot&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;State&lt;/strong&gt; is the internal condition of the system at a specific point in time. For example:

&lt;ul&gt;
&lt;li&gt;In VS Code’s Git extension, the state might include whether a repository is initialized, whether there are unstaged changes, or whether an operation is in progress.&lt;/li&gt;
&lt;li&gt;It’s like knowing a ball’s location, direction, and speed at a given moment.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Context: The Bigger Picture&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Context&lt;/strong&gt; is the dynamic and situational convergence of:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Relevant system state&lt;/strong&gt;: Only the state that matters for the current situation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environment&lt;/strong&gt;: External factors like workspace structure, active files, or network availability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration&lt;/strong&gt;: User-defined settings and preferences.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User actions&lt;/strong&gt;: What the user is doing (e.g., clicking a button, typing).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UI hierarchy&lt;/strong&gt;: The current location in the user interface (e.g., active view, focused panel).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Context is what gives meaning to state and defines the boundaries of what actions are allowed and what the next state will be. For example:

&lt;ul&gt;
&lt;li&gt;Knowing a ball’s state (location, direction, speed) doesn’t tell you where it will be in 1 minute. You need the &lt;strong&gt;context&lt;/strong&gt; (gravity, air resistance) to predict its trajectory.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why Context is Above State in the Hierarchy&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Context is &lt;strong&gt;above state&lt;/strong&gt; in the hierarchy because:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Context Gives Meaning to State&lt;/strong&gt;: State is just data; context is what makes that data actionable. For example:

&lt;ul&gt;
&lt;li&gt;In VS Code, knowing that a file has unstaged changes (state) is meaningless without the context of the active editor and the user’s current workflow.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context Defines Boundaries&lt;/strong&gt;: Context determines what states and actions are allowed. For example:

&lt;ul&gt;
&lt;li&gt;If the context indicates that the user is in the Source Control view, the system enables Git-related commands and disables irrelevant ones.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context Drives State Transitions&lt;/strong&gt;: Context determines what the next state will be based on user actions and environmental factors. For example:

&lt;ul&gt;
&lt;li&gt;If the user stages changes in a file, the context (active file, Git status) determines the next state (changes moved to the staged area).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;VS Code’s &lt;a href="https://code.visualstudio.com/api/references/when-clause-contexts" rel="noopener noreferrer"&gt;ContextKeyService&lt;/a&gt;: A Case Study&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;VS Code’s &lt;strong&gt;ContextKeyService&lt;/strong&gt; is a powerful example of how context can be managed and leveraged in a software system. Here’s how it works at a high level:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Computed Context&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;ContextKeyService&lt;/strong&gt; dynamically computes context based on:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;System state&lt;/strong&gt;: E.g., whether a repository is initialized.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environment&lt;/strong&gt;: E.g., the active editor and its Git status.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration&lt;/strong&gt;: E.g., user preferences for handling parent repositories.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User actions&lt;/strong&gt;: E.g., clicking a button or typing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UI hierarchy&lt;/strong&gt;: E.g., the current view or panel.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;These factors are combined into &lt;strong&gt;context keys&lt;/strong&gt;, which are used to control the system’s behavior.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How Context Drives Behavior&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Command Enablement&lt;/strong&gt;: Commands are enabled or disabled based on context keys. For example:

&lt;ul&gt;
&lt;li&gt;The "Stage Changes" command is enabled only if the context key &lt;code&gt;git.activeResourceHasUnstagedChanges&lt;/code&gt; is true.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;UI Visibility&lt;/strong&gt;: UI elements are shown or hidden based on context. For example:

&lt;ul&gt;
&lt;li&gt;The Source Control view shows merge changes only if the context key &lt;code&gt;git.mergeChanges&lt;/code&gt; is set.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Adaptive Feedback&lt;/strong&gt;: Notifications and prompts are triggered based on context. For example:

&lt;ul&gt;
&lt;li&gt;If the context indicates an unsafe repository, the system shows a warning.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why We Confuse State with Context&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We often confuse state with context because:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;State is Easier to Measure&lt;/strong&gt;: State is concrete and quantifiable, while context is more abstract and situational.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context is Implicit&lt;/strong&gt;: Context often operates behind the scenes, making it easy to overlook.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State Feels Sufficient&lt;/strong&gt;: At first glance, state seems to provide all the information we need. However, without context, state is just data without meaning.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Ball Example: State vs. Context&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s revisit the ball example to illustrate the difference:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;State&lt;/strong&gt;: The ball’s location, direction, and speed at a given moment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context&lt;/strong&gt;: The forces acting on the ball (gravity, air resistance) and the environment (e.g., wind, surface friction).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without context, the state tells us nothing about where the ball will be in 1 minute. It’s the context that allows us to predict the ball’s trajectory and understand its behavior.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion: Context is King&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In software systems, &lt;strong&gt;context is above state in the hierarchy&lt;/strong&gt;. While state provides a snapshot of the system, context gives meaning to that snapshot, defines boundaries, and drives state transitions. VS Code’s &lt;strong&gt;ContextKeyService&lt;/strong&gt; is a prime example of how context can be managed and leveraged to create adaptive, intuitive, and efficient systems.&lt;/p&gt;

&lt;p&gt;So the next time you’re designing a system, remember: &lt;strong&gt;State is just data; context is what makes it meaningful.&lt;/strong&gt; By focusing on context, you can build systems that truly understand and adapt to the user’s needs.&lt;/p&gt;

&lt;p&gt;What are your thoughts on the role of context in software systems? Have you encountered situations where context made all the difference? Let’s discuss in the comments! 🚀&lt;/p&gt;

</description>
      <category>vscode</category>
      <category>react</category>
      <category>systemdesign</category>
      <category>ui</category>
    </item>
  </channel>
</rss>
