metasyntactic variables

Discussion in 'C Programming' started by Baboon, May 19, 2008.

  1. Baboon

    Baboon Guest

    In my "C" programs, I am always using heaps of metasyntactic variables.

    1) Is that a good idea?

    2) What variable names are recommended?
    Baboon, May 19, 2008
    #1
    1. Advertising

  2. Baboon

    Chris Dollin Guest

    Baboon wrote:

    > In my "C" programs, I am always using heaps of metasyntactic variables.


    Like foo, bar, baz, flarn, spoo, it, thing, wossname?

    > 1) Is that a good idea?


    Usually not.

    > 2) What variable names are recommended?


    Ones that say, briefly, what they mean, so they communicate to the
    human reader (who cares) as opposed to the compiler (which usually
    doesn't complain, even if it does care). The larger their scope,
    the more likely that the name is long (since it occurs more often
    out of context).

    Traditionally, loop indicies have names like `i`, `j`, `k`, although
    better names may present themselves/be required, especially if there
    are multiple nested/sequential loops where the meaning of the indexes
    can be obscure.

    (fx:imao)

    I often end up with 1-or-2-character names for function parameters
    when (a) the type of the argument has an explanatory name, (b) there's
    only one argument of that type, (c) the type name's constuituent
    words abbreviate to the name, (d) the function is short [1] (e) the
    result isn't stupid.

    Strata stratify( Premises p ) { ... }

    `p` may be a perfectly good name, certainly much better than `premises`.

    ResultSequence runQuery( Graph g, QueryHandler qh ) { ... }

    `g` and `qh` (or even `q`) are good enough names, better than
    `graphToQuery` or `queryHandlerToUse`.

    [1] One of my coding goals is "all functions should be short". That
    means any long functions I end up with must have a really good
    reason for being long [2].

    [2] "long" and "short" are really unfortunate word-choices here, yes?

    --
    /Questions? Answers! Answers? Questions!/ - Focus

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
    Chris Dollin, May 19, 2008
    #2
    1. Advertising

  3. Baboon

    John Bode Guest

    On May 19, 3:07 am, Baboon <> wrote:
    > In my "C" programs, I am always using heaps of metasyntactic variables.
    >
    > 1) Is that a good idea?
    >


    Generally, no.

    > 2) What variable names are recommended?


    Anything that clearly conveys what the variable (or function)
    represents and how it's used in the program. For a totally contrived
    example, say your code gathers weather data to prepare a weather
    report. The most logical names for your variables would be things
    like "temperature", "humidity", "windDirection", "windSpeed", etc.

    Don't be afraid to use plain English (or whatever your native language
    is) names. By that same token, don't go insane and use names like
    "temperatureFromSensorNumberSeven" or something like that.
    John Bode, May 20, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    0
    Views:
    3,347
    avnrao
    May 7, 2004
  2. Replies:
    1
    Views:
    923
  3. dwok
    Replies:
    7
    Views:
    683
    Hal Rosser
    Mar 4, 2005
  4. Replies:
    10
    Views:
    35,838
    jporter892
    Jun 6, 2011
  5. Replies:
    9
    Views:
    922
Loading...

Share This Page