Programming style

Programming style (also called coding standards or code convention) is a term that describes conventions for writing source code in a certain programming language.

Programming style is often dependent on the actual choice of programming language one is writing in. C style will vary from BASIC style, and so on.

Contents

Characteristics of style

Good style, being a subjective matter, is difficult to concretely categorize; however, there are a number of general characteristics. With the advent of software that formats source code automatically, the focus on how source code looks should yield to a greater focus on naming, logic, and higher techniques. As a practical point, using a computer to format source code saves time, and is possible to then enforce company-wide standards without religious debates.

Appropriate variable names

Appropriate choices for variable names is seen as the keystone for good style. Poorly-named variables make code harder to read and understand.

For example, consider the following pseudocode snippet:

get a b c 
if a < 12 and b < 60 and c < 60
  return true
else
  return false

Because of the choice of variable names, the function of the code is difficult to work out. However, if the variable names are made more descriptive:

get hours minutes seconds 
if hours < 12 and minutes < 60 and seconds < 60
  return true
else
  return false

the code's intent is easier to discern, namely, "Given a 24-hour time, true will be returned if it is in the morning and false otherwise."

Indent style

Indent style, in programming languages that use braces or indenting to delimit logical blocks of code, such as C, is also a key to good style. Using a logical and consistent indent style makes one's code more readable. Compare:

if (hours < 12 && minutes < 60 && seconds < 60)
{
   return true;
}
else
{
   return false;
}

with something like

if(hours<12&&minutes<60&&seconds<60){return true;}
else{return false;}

The first example is much easier to read because it is indented well, and logical blocks of code are grouped and displayed together more clearly.

Boolean values in decision structures

Some programmers think decision structures such as the above, where the result of the decision is merely computation of a Boolean value, are overly verbose and even prone to error. They prefer to have the decision in the computation itself, like this:

return hours < 12 && minutes < 60 && seconds < 60;

The difference is often purely stylistic and syntactic, as modern compilers produce identical object code for both forms.

Looping and control structures

The use of logical control structures for looping adds to good programming style as well. It helps someone reading code to understand the program's sequence of execution (in imperative programming languages). For example, in pseudocode:

 count = 0
 while count < 5
   print count * 2
   count = count + 1
 endwhile

The above snippet obeys the two aforementioned style guidelines, but however the following using the "for" construct makes the code much easier to read:

 for count = 0, count < 5, count=count+1
   print count * 2

In many languages, the often used "for each element in a range" pattern can be shortened to:

 for count = 0 to 5
   print count * 2

Spacing

Free-format languages often completely ignore whitespace. Making good use of spacing in one's layout is therefore considered good programming style.

Compare the following examples of C code.

 int count;for(count=0;count<10;count++){printf("%d",count*count+count);}

with

 int count;
 for (count = 0; count < 10; count++)
 {
    printf("%d", count * count + count);
 }

In the C-family languages, it is also recommended to avoid using tab characters as different text editors render their width differently. Usually 2 or 4 spaces are used instead.

Python forces the use of indentation to mark control structures. By doing this, the need for bracketing with curly braces ({ and }) is eliminated, and readability is improved while not interfering with common coding styles. However, some programmers do not like being forced to use a style they didn't choose.

See also

External links

Coding conventions for languages

Coding conventions for projects

zh-cn:程序风格

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools