ArticleslgStudy

computer science

Strongly typed identifier

Strongly typed identifier 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 Strongly typed identifier rather than just read about it. In short: A strongly typed identifier is user-defined data type which serves as an identifier or key that is strongly typed. This is a solution to the "primitive obsession" code smell as mentioned by Martin Fowler.

Strongly typed identifier — main illustration
Strongly typed identifier — illustration

Key takeaways

  • Strongly typed identifier 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 Strongly typed identifier to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Strongly typed identifier from memory before moving on to harder problems.

Reference excerpt

A strongly typed identifier is user-defined data type which serves as an identifier or key that is strongly typed. This is a solution to the "primitive obsession" code smell as mentioned by Martin Fowler. The data type should preferably be immutable if possible. It is common for implementations to handle equality testing, serialization and model binding. The strongly typed identifier commonly wraps the data type used as the primary key in the database, such as a string, an integer or universally unique identifier (UUID). Web frameworks can often be configured to model bind properties on view models that are strongly typed identifiers. Object–relational mappers can often be configured with value converters to map data between the properties on a model using strongly typed identifier data types and database columns.

Examples Passing a strongly typed identifier throughout the layers of an example application.

C# C# have records which provide immutability and equality testing. The record is sealed to prevent inheritance. It overrides the built-in ToString() method. This example implementation includes a static method which can be used to initialize a new instance with a randomly generated globally unique identifier (GUID).

C++ C++ has structs but not immutability so here the id field is marked as private with a method named getId() to get the value.

Crystal Crystal's standard library provides the record macro for creating records which are immutable structs and lets you create override the built-in to_s method.

D D have immutable structs.

Dart Dart have classes with operator overloading.

F# F# lets you create override the Equals, GetHashCode and ToString methods.

Go Go have structs which provide equality testing. Go however does not provide immutability.

Groovy Groovy have record classes which provide immutability and equality testing.

Haskell Haskell can create user-defined custom data types using the newtype keyword. It provides equality testing using the Eq standard class and printing using the Read and Show standard classes.

Java Java have records which provide equality testing. The record is declared using the final modifier keyword to prevent inheritance. It overrides the built-in toString() method.

JavaScript This JavaScript example implementation provides the toJSON method used by the JSON.stringify() function to serialize the class into a simple string instead of a composite data type. It calls Object.freeze() to make the instance immutable. It overrides the built-in toString() method and the valueOf() method.

Julia Julia have immutable composite data types.

Kotlin Kotlin have "inline classes".

Nim Nim have "distinct types".

PHP This PHP example implementation implements the __toString() magic method. Furthermore, it implements the JsonSerializable interface which is used by the built-in json_encode function to serialize the class into a simple string instead of a composite data type. The class is declared using the final modifier keyword to prevent inheritance. PHP has traits as a way to re-use code.

Python Python has data classes which provides equality testing and can be made immutable using the frozen parameter. It overrides the __str__ dunder method. This example implementation includes a static method which can be used to initialize a new instance with a randomly generated universally unique identifier (UUID).

Python also has NewType which can be used to create new data types.

Raku Raku have classes which provides equality testing and are immutable. It overrides the built-in Str method. This example overrides the default gist method. It uses roles which are mixed in into classes to be re-usable.

Ruby Ruby have data classes which provides equality testing and are immutable. It overrides the built-in to_s method. This example implementation includes a static method which can be used to initialize a new instance with a randomly generated universally unique identifier (UUID).

Rust In Rust this can be done using a tuple struct containing a single value. This example implementation implements the Debug and the PartialEq traits. The PartialEq trait provides equality testing.

Scala Scala have case classes which provide immutability and equality testing. The case class is sealed to prevent inheritance.

Swift Swift have the CustomStringConvertible protocol which can be used to provide its own representation to be used when converting an instance to a string, and the Equatable protocol which provides equality testing.

TypeScript TypeScript have unique symbol which can be used to create "branded types".

Zig Zig have structs with constants but by design does not have operator overloading and method overriding.

See also

Domain-driven design Type safety Value object

References

External links https://wiki.c2.com/?PrimitiveObsession

Illustrations

Strongly typed identifier: A UML class diagram for a strongly typed identifier.
A UML class diagram for a strongly typed identifier.
Strongly typed identifier illustration

Worked examples

Example 1 — a first encounter with Strongly typed identifier

Start with the simplest possible case. Write down what Strongly typed identifier 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 Strongly typed identifier 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 Strongly typed identifier 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 Strongly typed identifier

In research
Strongly typed identifier 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 Strongly typed identifier 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
Strongly typed identifier is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data types, Software design patterns, so understanding it makes those chapters shorter.
In everyday life
Look for Strongly typed identifier 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 “Strongly typed identifier” →

How to study Strongly typed identifier in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Strongly typed identifier 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 Strongly typed identifier out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Strongly typed identifier in simple terms?

A strongly typed identifier is user-defined data type which serves as an identifier or key that is strongly typed. This is a solution to the "primitive obsession" code smell as mentioned by Martin Fowler.

Why does Strongly typed identifier 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 Strongly typed identifier?

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 Strongly typed identifier.

Tags

  • Data types
  • Software design patterns

Keep exploring