Skip to content

Symbol Names

20081208 (2008 December 08 Monday)

Name Rules

General

  • Don’t use abbreviations, unless they are normally used as ordinary words. E.G. tv, lcd, dvd Not: cmptr, kbd,
  • Use clear names, underscore_separation is clearer than CamelCase.
  • Class names need to be very clear and are not typed as much, so make them very clear don’t worry about length.
  • It you have a class ACCOUNT and a method to get a balance, call it balance not account_balance.
    • so a client looks like this: freds_account.balance()
    • not: freds_account.account_balance()
  • Be self consistent, follow the style of a class you are editing.

Grammar

  • Class names should be nouns.
  • Commands should be verbs.
  • non-boolean Queries should be nouns.
    • so a clent looks like this: freds_account.balance()
    • not: freds_account.get_balance()
  • Boolean Queries should be adjectives, and because of verb adjective ambiguity in English should be prefixed with is_ or one of the tenses of is_, such as are_ or was_ (but never future tense)

Booleans

  • Don’t compare booleans
    • so a client looks like this: if (freds_account.is_open())
    • not: if (freds_account.is_open() == true)

Example

Don’t do this

if ( (fredsaccount.getaccountbalance() < -100 ) == True ) {
  overdraft = True; 
} else {
  overdraft = False;
}

In English this is: When the get account balance of freds account is less than -100 is true then overdraft[what ever that is] is true else overdraft[what ever that is] is false.

Do this

const is_overdraft_limit_exceeded := (freds_account.balance() < -100 );

In English this is: overdraft limit is exceeded when and only when balance of freds account is less than -100

A Little Hungarian — prefixes and postfixes

Just a pinch of Hungarian notation, we don’t want to over do it. Be pragmatic with this.

  • Prefixes and suffixes are lower-case and separated by underscores,
    • Prefixes, we mainly use these to avoid name-space collisions
      • Scope
        • p_ for method parameter, I don’t like this one as it clutters the public interface.
        • l_ for local, can also use very short names e.g. x,y,a,x2 if methods are very short
        • m_ for private member variable (not universally a good idea, but avoids name conflicts in c++, c#, Java)
        • no scope prefix for public features: methods, properties.
      • Yes/no queries returning boolean. To avoid adjective/verb ambiguity in English.
        • is_, are_, was_, etc.
      • If you need to have a scope prefix and a yes/no query, the scope comes first.
        • p_is_Very_Happy
    • The only suffixes are for pointers(C/C++)
      • _pt for pointers
      • _cpt for constant pointers that are guaranteed to be valid.
  • Class names are all capitals separated by underscores.
Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: