Skip to content

Instantly share code, notes, and snippets.

@sergeysova
Last active April 17, 2023 17:29
Show Gist options
  • Save sergeysova/9f6dc24e21efe5f5bb4f9878216c2ce9 to your computer and use it in GitHub Desktop.
Save sergeysova/9f6dc24e21efe5f5bb4f9878216c2ce9 to your computer and use it in GitHub Desktop.

There is a useful pattern to follow when naming functions:

namespace? + high context (HC) + low context? (LC) + action (A)

Take a look at how this pattern may be applied in the table below.

Name Namespace High context (HC) Low context (LC) Action (A)
userGet User Get
userMessagesGet User Messages Get
clickOutsideHandle Click Outside Handle
shouldMessageShown should Message Shown

Note: The order of context affects the meaning of a variable. For example, shouldUpdateComponent means you are about to update a component, while shouldComponentUpdate tells you that component will update on itself, and you are but controlling when it should be updated. In other words, high context emphasizes the meaning of a variable.

Why action goes the latest?

When you write code in IDE, you have an "code intelligency" or autosuggestion. If you type action first "get", you'll see all function in project that has "get" action, and you need to type High context and Low context to reduce amount of methods. But if you first type High context you can see all functions that can operate with that context and with lowest contexts, then you can narrow the search typing the Low context name, and now you see all functions you can do with that context and nothing more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment