Skip to content

Instantly share code, notes, and snippets.

@salarmehr
Last active January 17, 2020 00:22
Show Gist options
  • Save salarmehr/88cbd17b264a711f4928f7198fdeee6f to your computer and use it in GitHub Desktop.
Save salarmehr/88cbd17b264a711f4928f7198fdeee6f to your computer and use it in GitHub Desktop.
Art of identifier namining

Dynamic identifiers (functions and methods)

Functions are verbs. If they do some actions on the system, we use imperative verb. A single verb is preferred when possible. e.g.

$user->activate();
$order->confirm();

if the functions are handling and event just prefix the event name with on.

$user->onFaiedLogin(); // it can be triggered by $user->login($parmas);
$order->onConfirm(); // it can be triggered by $order->confirm();

if the function returns a Boolean value and that is the result of an action (it has a dynamic nature) you can use none imprative form, most often simple past (expired), some times, continius present (expiring) and rarely times simple present (e.g. expires). e.g.

$user->validated(); // and not `isValid`
$order->confirmed(); // and not `isConfirmed`
$process->running();

If testing "ownership" of an attribute you can use "is + adjective" or "has + noun".

$user->isGeek();
$order->hasSurcharge();

use can use noune as an identifer for dynamic values when they are used as setter/getter.

$user->name(); // from the function signature its obvious that this is a getter.
$user->name("Reza"); // from the function signature its obvious that this is a setter.

Static identifiers (properties, variables, constants)

We use adjective and nouns to name them.

if they are Boolean we use adjective or adverb

$user->happy=true; // and not "isHappy"
$order->expired; // and not "isExpired" 

if not Boolean we use noun. e.g.

$user->name="Reza";
$user->height=175;
$order->tax=233;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment