[go: up one dir, main page]

DEV Community

tiff
tiff

Posted on

What is meant by a "shape" in programming

Devs like to talk about imagining the shape of the code or data and when people say that, all I can think of is a circle and square, traditional shapes and I can't get past that mental model.

Whenever it is explained to me I'm lost and have no idea what it means.

Can someone help me understand this?

Top comments (6)

Collapse
 
peerreynders profile image
peerreynders • Edited

The way data is structured/organized defines it's "shape".

TypeScript specifically has type aliases so you may see something like:

type RefreshFn = () => boolean;

type Source<T> = {
  value: T | undefined;
  sinks: Set<Sink>;
};

type Sink = {
  sources: Set<Source<unknown>>;
  refresh: RefreshFn;
  isStale: boolean;
}
Enter fullscreen mode Exit fullscreen mode

So in terms of TypeScript Sink is viewed as a "type" - which defines it's shape - the way the data is structured/organized (rather than viewing it as an object literal); an isStale property with a boolean value, a refresh property with a function that returns a boolean and a sources property that nests zero or more Source data structure in a Set.

Now given that JavaScript is dynamically typed one might refer to the "shape" of a Sink instance when talking about how it organizes its data.

Given Duck Typing in JavaScript one may state that two instances of data have a "similar shape" if they both include all the pieces of data needed to perform the operation (function) in question while either of them may have potentially different additional pieces of data that are irrelevant to the operation (function).

Now the term "shape" isn't limited to JavaScript but applies anywhere where data can have structure/organization but I find it is more often used in reference to dynamically typed languages. It's also used more in circumstances where data and behaviour are separated - i.e. outside of class-based object orientation.

So while Rust has struct, JavaScript uses object literals to emulate structured data and both constructs are used to "shape" the data.

Collapse
 
tifflabs profile image
tiff

So basically an object literal? Hmm. I've heard of structs in C and languages like it. I'll check out the links you've linked here.

Thanks!

Collapse
 
peerreynders profile image
peerreynders • Edited

"shape" is a much looser concept than that.

It just happens that in JavaScript object literals (or dictionaries in early Python) are used to represent structured data. I would say that the term tends surface in contexts (or serialization formats) that are structurally typed rather than nominally typed.

For example in the Clojure spec guide:

In the success case, the parsed input is transformed into the desired shape for further processing.


From the Racket rebellion documentation:

Two kind-type? values are equal whenever the names, field counts, etc. of the types they describe are equivalent. That is, kind-type? values are compared using structural equality. The term shape of a type or simply shape, refers to a kind-type? value.


"shape" seems to be primarily used in contexts where structured data is represented by a collection of key/value pairs where order doesn't necessarily matter. Compare that to a C struct which specifies memory layout so order does matter and "structure" implies a more rigid organization. Though both terms are used in a rather informal manner.

Note that when arrays are used as tuples (wikipedia):

type SomeTriple = [string, number, boolean];
Enter fullscreen mode Exit fullscreen mode

they could manifest a shape.

  • They have a predetermined and fixed length
  • A particular type is expected at each particular element position.
  • The element position serves as the implicit "key"
  • The element itself is the "value".

Order matters as the key is coupled to the position so a tuple would have shape and structure.


In the context of class-based objection orientation "shape" sometimes refers to the public interface while "structure" also reveals the internal organization.

Collapse
 
jenc profile image
Jen Chan

Maybe it's to do with the structure of data, like how deeply nested a JSON object's properties go...

Collapse
 
fhinkel profile image
Franziska Hinkelmann, Ph.D.

I've never heard that expression. Do you have a concrete example? I've heard design of the code (often related to en.wikipedia.org/wiki/Software_des...) or structure or data. But not "shape".

Collapse
 
tifflabs profile image
tiff

Similar to what you're thinking but for dynamically types languages like JavaScript, according to this comment:

dev.to/peerreynders/comment/1mhj3