A single rule for apostrophes.

a sad apostropheApostrophes don’t have to be complicated. Normally we are taught several rules and many exceptions, and are expected to remember them. I have one rule and some examples. (If you already know how to do apostrophes then skip this post, unless you are teaching.)

Rule: Use apostrophes for contractions, that is when a word or words are made shorter by removing letters, use the apostrophe as a place-holder for the missing letters.

Examples simple contraction:

  • it is → it’s
  • it is → ’tis
  • do not → don’t
  • will not → won’t
  • can not → can’t
  • are not → aren’t
  • of the clock → o’clock
  • Holloweven → Hollowe’en
  • cat-of-nine-tails → cat-o’-nine-tails
  • will-of-the-wisp → will-o’-the-wisp
  • it was → ’twas

But not all contractions.

  • gymnasium → gym
  • advertisement → ad
  • etc.

Rule: Use apostrophes for contractions, that is when a word or words are made shorter by removing letters, use the apostrophe as a place-holder for the missing letters.

Examples possession:

  • John his ball → John’s ball
  • the cat its ball → the cat’s ball
  • Sarah hers ball → Sarah’s ball
  • the bus its wheels → the bus’s wheels
  • its ball → its ball
  • his ball → his ball
  • her ball → her ball
  • their ball → their ball

These seem a bit odd in the long form, nobody uses them any more, we always use the short form. However they are useful to help us remember where to put the apostrophe. (The history in this post is made up, it is just an aid to memory.)

We don’t have an apostrophe for Plurals, there is no contraction.
Rule: Use apostrophes for contractions, that is when a word or words are made shorter by removing letters, use the apostrophe as a place-holder for the missing letters.

Example:

  • cats → cats
  • dogs → dogs
  • buses → buses
  • TVs → TVs
  • LEDs → LEDs

So what about plurals, possession and words ending in s? The answer is the same.

Rule: Use apostrophes for contractions, that is when a word or words are made shorter by removing letters, use the apostrophe as a place-holder for the missing letters.

Example plurals and possession:

  • the cats there ball → the cats’ ball
  • the girls there ball → the girls’ ball

If a singular noun ends with an s-sound (spelled with -s, -se, for example), practice varies as to whether to add ‘s or the apostrophe alone. A widely accepted practice is to follow whichever spoken form is judged better: the boss’s shoes, Mrs Jones’ hat (or Mrs Jones’s hat, if that spoken form is preferred). In many cases, both spoken and written forms differ between writers. — (paragraph taken from wikipedia)

Years 1990 to 1999 that is the 1990s, the 10 years between 1990 and 1999, but for some reason Americans write 1970’s.


Now for the other anomaly:

Individual letters and numbers when pluralised have an apostrophe.
e.g.
12341567189 has 3 1’s, not 3 1s, or better 1 appears three times.
abcdaefgahi has 3 a’s, not 3 as, or better a appears three times.

There are a few other exceptions, however this is the simplest system I could think of. One rule that covers over 90% of it, and a few exceptions. If you go by the other systems then these exceptions are exceptions to exceptions to exceptions.

If you do not learn the exceptions then you will be well ahead of every one else. If you need to look up an exception I recommend the Penguin guide to punctuation. Its rules are simple (though the rules on apostrophise, are not as simple as mine), take the result and fit it into your newly learnt model (or just look it up each time, until you learn it).

Symbol Names

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.

Command Query Separation

Definitions:

  • A command is a feature that changes the state, but returns nothing: a procedure.
  • A query is a feature that changes nothing and returns something: a function.
  • A procedure is a routine that is a command, that does something.
  • A function is a routine that is a query, or non-writeable attribute.

So why a separation?

  • It makes code simpler and life easier. Yes, your code will be cleaner, simpler, more elegant, faster. You may struggle at first. In the early days, I had problems with situations where it seemed not possible. But then I learnt how. It is worth learning, the effort will force you to change your design in a subtle way that will make it better.
  • It reduces use of temporary variables. As now, you use object variables; When you have commands that return something, you end up having to store the value in a temporary variable until you are ready. With command query separation, you keep it in the object until you are ready.
  • It makes Design-Contracts / Code-Contracts possible.

Main objections

But I need to return a state

No you don’t, store the state in the this/self/current object. Consider how this state affects post-condition. Consider how this state affects pre-condition of other methods. Write these pre- and post-conditions; This will lead you to write an accessor to this new state. The client code can use this accessor.

It stifles creativity

So does goto, yet we avoid goto when ever the language is sufficiently structured to allow it. Command Query Separation will one day be like structured programming, totally ignored as it is built in to the languages and no one notices it.

C++ tip

Make query methods const. i.e.
return_type method_name(args) const {}

Turn on deep const checking. The standard for C++ says to do only shallow const checking. But deep checking is needed (any code written with deep checking, will conform to the shallow checking rules).

Turn on methods whose return type is not void must be const check.

We use PC-lint to do these checks (and other checking). But don’t use C++.