Nugget 25: Code Representation and Information Density

The previous two Nuggets (N23, N24) explored how, Primary Constructor, and Comments have a direct influence in Token Economy Management (TEM). These examples, show that reducing code verbosity shift TEM in our favor. 

However, consider the following:

// Represents a customer identifier that must match our internal

// regular expression format

public string CustId {get; set;}


And compare with:


public CustomerId Id {get; set;}


This is not only a reduction of tokens. There is a transfer of information from natural language documentation into the programming language type system. There is semantics in the type. This is Typographic Semantics.

The type CustomerId now encapsulates validation rules. There is a caveat in this because the type must actually enforce the rules described in the documentation, and the LLM must have access to its definition when needed. However, TEM benefits from this approach.

From this particular scenario, it becomes evident that we are looking at two, related but intents:

Effective TEM Cost And Tokens Required for the Task


==================================================

      Typographic Semantics

==================================================

Explanation:

Instead of using comments to explain what a variable or

object represents, use C#'s strongly typed system to

make the code inherently unambiguous to the LLM.


Variable Without Semantics Token Count:  31

Strongly Typed Structure Token Count:      11


Tokens Saved:                                         20

Context Window Reduction:                64.52%

==================================================


The preceding TEM analysis illustrates a Typographic Semantics study case. This Nugget is not an argument for typing less or sacrificing type safety. Rather, it encourages a shift in thinking toward the economics of LLM token consumption through the lens of Token Economy Management (TEM).

Earlier C# version also introduce improvements that when used contribute to significantly improve TEM. I will be visiting these cases, through these series. Here are some of the test cases I have run thus far.

Experiment Investigation Controlled Experimental TEM Improvement
Primary Constructor Structural Boilerplate Reduction 57%
Coments Documentation Overhead 78%
Typographic semantics Expressing meaning through types rather than prose 65%
Pattern matching (C# 9+) Structural and syntactic compression 23%
Null-coalescing assignment (C# 8+) Eliminating repetitive control structures 53%
Using declarations (C# 8+) Reducing scope-management syntax 23%



Postscript

Once you read N23, N24, and N25, you may find yourself thinking intuitively in terms of TEM. I have certainly started doing so.

I think N23 and N24 are isotopes. They share the same TEM properties, reducing verbosity, but they carry different atomic weights.

"So, Jose Mendeleev, where do we place N25?" asked Dr. Watson in an amused tone.

Jose reclined in his comfortable armchair, removed his pipe from his coat pocket, placed it in his mouth, and held it there while gently stroking it with his thumb, his gaze fixed through the open window. He did not move a muscle. Then, as if emerging from a mesmerizing trance, he shifted his attention to Dr. Watson.

"Elementary, my dear doctor. We can place N25 in the same TEM Periodic Table Group!"

Comments

Popular posts from this blog

Nugget 11: Adding Filtering to a Blazor QuickGrid

Nugget 9: Building a JobBank App using Blazor. A Nugget with a Story.

Nugget 14: Connection to MongoDB Atlas - SSL Alert 80