Format and Parse Naming Convention
Here are some conventions I love for naming data manipulation functions.
The Names
The names are parse
and format
. Prefixes, as in: parseAThing
or formatAThing
.
I love these names. There are many std libs that use these names for things like dates and strings.
I like to apply these names more broadly. They can apply to more than just de/serialization.
Leave Other Names
Once we have the parse
/format
convention, we can leave so many others behind and be more consistent. Without these, we might have a clutter of similar-meaning but different-sounding names:
parse
-equivalent: determine
, find
, get
format
-equivalent: build
, gen/generate
, make
, transform
, map
These two prefixes, parse
and format
, can be our common "data transform" naming conventions.
When To Use
So, when do we use these names? Think in terms of opposites. These are companion names.
Easiest situation: To a string: format
. From a string: parse
. But this can be used for turning any object into another object.
Reading: parse
. Writing: format
.
Processing input: parse
. Creating output: format
. Sometimes it's tricky, right, because every function essentially has an input and an output. So it depends on the context of usage. How do you think about the function's primary purpose, and how do you want others to think about it?
Discovering: parse
. Creating: format
.
Receiving response: parse
. Prepping request: format
.
Most abstract rule: Beginning transform: parse
. Ending transform: format
.
Helpful? Yes, I've found a lot of mileage in this convention.