Coding Conventions

update: Oops, I thought I posted this a “private” in wordpress, but I guess not. This post was really meant as a note to myself so I could refer back to it later on. It is directly ripped from the Project Ardvark specification which Joel Spolsky made public. Sorry for filling your RSS or whatever with notes to myself, I’ll try to do a better job using WordPress.

Always use the same name for the same thing. For example, the name of an INPUT in HTML should
correspond to the name of the variable it gets stored in, which should correspond to the name of the
column in the SQL database.
In C# code use Apps Hungarian, a.k.a. Aardvark Ukrainian. Having a stronger coding convention for
names helps keep code correct, but it also helps you remember variable names because theres less
variability, and it saves mental stress when you have to figure out a new variable name because so many
are predictable.
Variable names are lowerCamelCase, e.g. prefixName
Common prefixes:

  • s = a Unicode string
  • ch = a single Unicode char
  • a = ANSI modifier. as = ANSI string; ach = ANSI char.
  • utf8 = A UTF8 string
  • c = count of something. So cb = count of bytes (buffer size), cch = string length
  • ix = index the unique primary key of a database or an index in a loop or an index
    into an array
  • rg = (range) an array, usually as a prefix. For example rgs is an array of strings
  • f = Boolean flag, always true/false. Although traditional Apps Hungarian allows threestate
    flags we dont.
  • fl = File
  • fn = Function (when you have a function pointer)
  • path = Path, the full path name of a file
  • n = n-way flag or enum
  • b = byte.
  • db = database
  • dbl = floating point number
  • l = long, by which we mean, a long integer that is not a count of something
  • x, y = x or y coordinates in pixels.
  • d = difference. Commonly used in dx or dy to indicate width or height, but could also
  • be used as ddt (timespan), dix (offset in an array)
  • dt = date/time (includes both)
  • rs = recordset
  • sql = sql statement
  • tbl = database table
  • url = a complete URL.
  • urls = a string which has been URL-encoded
  • html = a string which contains HTML and is suitable to dump to the browser as-is
  • p = pointer modifier. Used for a pointer in C/C++; not needed in C# code.
  • o = object.

When you define new classes, pick a Ukrainian prefix to use for that class of 2-4
letters, and document it at the top of the class definition. So for example if you defined
a class called CSession you might decide to use sess as the prefix.
Common names or suffixes:

  • Min, Max = minimum and maximum
  • Prev, Next, Curr = previous, next, and current
  • Tmp = a temporary value
  • Src, Dest = when copying things

The name part is often omitted when there is only one local object of the type youre dealing
with or one which is most important. For example, if you write a string function that takes one
string argument, its just going to be named s. The arguments to a function that writes a string
to a file would obviously be named (fl, s).
Function names are UpperCamelCase. Use TypeFromType for conversion functions, for
example, Utf8FromS would be a function that converted Unicode to UTF-8.
Class names start with C or E. (Objects of that class should use their own prefix which you
coin.) Use E for entity classes, that is, classes which correspond to a single row from a table or
view in the database. Use C for all other classes.
SQL Tables are always UpperCamelCase and named in the singular, for example, Customer
and Person, never Customers and People.
Virtually every table contains an autonumber (identity) it is always the leftmost column, is
always the primary key, and always named ixFoo where Foo is the table name. For example if
your table is called Customer there is certain to be a PK named ixCustomer as the first column.
If any other tables have foreign keys pointing into your table, they will be named ixCustomer,
too, unless they have several, in which case, append a modifier. For example ixCustomerFirst,
ixCustomerCurrent, ixCustomerReferrer.
In SQL tables, we use the same prefixes as we use in code. The most common are dt, f, n, c, and
ix. Strings are usually just s unless html or url is more appropriate, but we sometimes use txt for large text (memo) fields. All SQL tables start with tbl, views start with view, and we hope never to use stored procedures.

3 thoughts on “Coding Conventions”

  1. i agree. keeping things similar like that makes it much less taxing when you are working on huge apps. unfortunately, mortgage apps often have fields with VERY similar names.

  2. The mental stress you feel when you have to figure out a variable name comes from having to find the appropriate plain-language words to describe the algorithm or structure that you are working on. This is a very valuable type of mental stress, as it helps you to build a model of your code that is comprehensible to others and relates to the surrounding world. Importantly, you build this model while you are writing the software, not before or after (or not at all) as you would with comments or design documentation. Any naming convention that removes this kind of stress also detracts from the quality of your software.

    Also, it seems that roughly half of your naming standard just duplicates things that the type system already does for you.

Comments are closed.