Settings

Theme

Ask HN: Should IDs be strings or numbers in your codebase?

2 points by viviansolide 20 days ago · 2 comments · 1 min read


At work we're debating whether entity IDs should be typed as strings or numbers throughout our stack (API contracts, frontend, etc.).

Database auto-increments happen to be integers, but we never perform arithmetic on IDs – they're opaque identifiers.

My take: IDs are identifiers, not numbers. Typing them as strings makes this explici and makes migrating to UUIDs or other formats painless.

Am I missing a good reason to keep them as numbers?

Rotundo 20 days ago

Consider storage requirements. Strings (ASCII? UTF-8?) are not as efficient as integers or UUIDs. You're not storing UUIDs as strings, are you? They are binary, only converted to the string expansion for display and/or export.

jghn 20 days ago

Unless you're doing math with them for some reason, they're strings.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection