Conventions/code/naming: Difference between revisions

From Woozle Writes Code
Jump to navigation Jump to search
Line 51: Line 51:
* '''value''' means a read-only value, assumed to exist (throws error if not)
* '''value''' means a read-only value, assumed to exist (throws error if not)
==Namespace Aliases==
==Namespace Aliases==
When creating an alias for a namespace, I use a single capital letter as shorthand for parent-folders of the current namespace -- ''not'' for folders outside of it.
When creating an alias for a namespace, I use capital letters to indicate that the namespace is an alias. (For a little while, I only used caps for parents of the current namespace, but this got confusing and made code less portable.)


'''Example''': In <code>namespace ferret\data\bank</code>, I might use <code>D</code> as an alias for <code>ferret\data</code> (<code>use ferret\data as D;</code>), but I would ''not'' use that alias in <code>namespace ferret\globals</code>. I would, however, use <code>F</code> as an alias for <code>namespace ferret</code> in both of them.
'''Examples''':
<syntaxhighlight lang=php>
use ferret as F;
use ferret\data as FD;
</syntaxhighlight>

Revision as of 16:02, 25 February 2022

I tend to use the following naming conventions within my code.

Prefixes

These are more or less in keeping with the Apps (original) variation of Hungarian notation.

Variable Prefixes

  • f: floating-point number
  • k or K: a constant, i.e. anything whose value should not (or cannot) change once defined
  • n: a count (int; use f for float)
  • o: object
    • or: a ref object (see Value Types)
    • os: a status object (see Value Types)
  • s: string
    • sc: string that is the name of a class
    • sq or sql: string formatted as SQL, or safe to be used within SQL
  • url: string formatted as a complete URL
  • uri: string formatted as a URI, i.e. part of a URL

see also General Affixes

Declaration Prefixes

  • c: class
    • ca: abstract class (cannot be instantiated)
    • cs: static class (not intended to be instantiable; all methods static)
  • ch: a string that is always a single character
  • f: [DEPRECATED; use namespaces instead]: a Ferreteria class/trait/interface
  • if: interface
  • t: trait

see also General Affixes

I wanted to use "c1" (cee-one) as a prefix for singleton classes (i.e. a class for which there should never be more than one object), but it looks too much like "cl" (cee-ell) or possibly a typo, so I dropped that idea.

General Affixes

In the prefix but not necessarily as the first characters:

  • db: a database
  • rc: a single-record class/object
  • rs: a recordset class/object
  • t: a table class/object

These may be used in declarations or variable names, but are generally used only for classes and objects. For classes, the c prefix comes first; for objects, these replace the o prefix, and sometimes may be the entire name of the variable.

Value Types

  • actual means a reference-container object
    • I prefer this to "reference" where possible. It's shorter, less misleading (all PHP variables are references), and correctly implies that the entity in question is the actual thing and not a copy.
  • cell means access to a value in a named location (e.g. an array element)
  • ref is another shorthand for "reference"; see "actual".
  • slug means a string which can unambiguously represent any expected value or state of a given item
    • needed for representing record IDs in URLs, where one possible state is "new" (no ID yet)
  • status means a presence-info object
    • preferably descended from cThingHolder
  • unit means something that knows its own name and has awareness of its container-object/structure
    • preferably a PortRow Unit object
  • value means a read-only value, assumed to exist (throws error if not)

Namespace Aliases

When creating an alias for a namespace, I use capital letters to indicate that the namespace is an alias. (For a little while, I only used caps for parents of the current namespace, but this got confusing and made code less portable.)

Examples:

use ferret as F;
use ferret\data as FD;