Home > Articles > Programming > C#

  • Print
  • + Share This
From the author of

From the author of

Style

Style is the manner or mode of expression in a program. It is distinct from the meaning of the code. Proper utilization of style elements can contribute significantly to understandability by programmers attempting to ascertain the semantics of an algorithm. These style elements include whitespace, placement of language elements, and naming conventions.

Whitespace characters separate language elements such as identifiers and keywords. These characters include newline, tab, form feed, and Control-Z characters. A program may have any amount of whitespace between language elements. The compiler will ignore any extra whitespace. Effective use of whitespace goes a long way toward making programs readable.

A consistent naming convention can also make code easier to read. There are essentially two forms of naming conventions in C#. The first is pascal casing, where the first letter of each word in a name is capitalized, such as HelloWorld, DotProduct, or AmortizationSchedule. This is normally used in all instances except for parameters and private fields of classes. In the case of private class fields and method parameters, use camel casing. This is where the first letter of the first word is lowercase and subsequent words are capitalized, such as bookTitle, employeeName, or totalCompensation. Again, keeping consistent standards will make code easier to read when going through it at a later date.

TIP

Seriously consider the practicality of the naming convention used. If it makes sense, do it. But don't hold on to old practices unless there's a good reason to do so. Recent style guidance by Microsoft and others suggests that a variable name should reflect its semantics more than type.

  • + Share This
  • 🔖 Save To Your Account