ArticleslgStudy

computer science

Code as data

Code as data is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Code as data rather than just read about it. In short: In computer science, the expression code as data refers to the idea that source code written in a programming language can be manipulated as data, such as a sequence of characters or an abstract syntax tree (AST), and it has an execution semantics only in the context of a given compiler or interpreter. The notion is often used in the context of Lisp-like languages that use S-expressions as their main syntax, as writ…

Key takeaways

  • Code as data belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Code as data to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Code as data from memory before moving on to harder problems.

Reference excerpt

In computer science, the expression code as data refers to the idea that source code written in a programming language can be manipulated as data, such as a sequence of characters or an abstract syntax tree (AST), and it has an execution semantics only in the context of a given compiler or interpreter. The notion is often used in the context of Lisp-like languages that use S-expressions as their main syntax, as writing programs using nested lists of symbols makes the interpretation of the program as an AST quite transparent (a property known as homoiconicity). These ideas are generally used in the context of what is called metaprogramming, writing programs that treat other programs as their data. For example, code-as-data allows the serialization of first-class functions in a portable manner. Another use case is storing a program in a string, which is then processed by a compiler to produce an executable. More often there is a reflection API that exposes the structure of a program as an object within the language, reducing the possibility of creating a malformed program. In computational theory, Kleene's second recursion theorem provides a form of code-is-data, by proving that a program can have access to its own source code. Code-as-data is also a principle of the Von Neumann architecture, since stored programs and data are both represented as bits in the same memory device. This architecture offers the ability to write self-modifying code. It also opens the security risk of disguising a malicious program as user data and then using an exploit to direct execution to the malicious program.

Data as Code In declarative programming, the data as code (DaC) principle refers to the idea that an arbitrary data structure can be exposed using a specialized language semantics or API. For example, a list of integers or a string is data, but in languages such as Lisp and Perl, they can be directly entered and evaluated as code. Configuration scripts, domain-specific languages and markup languages are cases where program execution is controlled by data elements that are not clearly sequences of commands.

References

Worked examples

Example 1 — a first encounter with Code as data

Start with the simplest possible case. Write down what Code as data claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Code as data before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Code as data ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Code as data

In research
Code as data appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Code as data in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Code as data is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer programming stubs, Computer science stubs, Programming language topics, so understanding it makes those chapters shorter.
In everyday life
Look for Code as data outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Code as data” →

How to study Code as data in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Code as data means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Code as data out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Code as data in simple terms?

In computer science, the expression code as data refers to the idea that source code written in a programming language can be manipulated as data, such as a sequence of characters or an abstract syntax tree (AST), and it has an execution semantics only in the context of a given compiler or interpre…

Why does Code as data matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Code as data?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Code as data.

Tags

  • Computer programming stubs
  • Computer science stubs
  • Programming language topics

Keep exploring