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 choice of programming language one is writing in. C style will vary from BASIC style, and so on.
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."
if(hours < 12 && minutes < 60 && seconds < 60){ return true; }else{ return false; }
or
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 two examples are much easier to read because they are indented well, and logical blocks of code are grouped and displayed together more clearly.
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.
count = 0 while count < 5 print count * 2 count = count + 1 endwhile
The above snippet obeys the two aforementioned style guidelines, but the following use of 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
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 in the middle of a line as different text editors render their width differently.
Python uses indentation to indicate control structures, so correct indentation is required. 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, this frequently leads to problems where code is copied and pasted into a Python program, requiring tedious reformatting. Additionally, Python code is rendered unusable when posted on a forum or webpage that removes whitespace.
Programmierstil | Programavimo stilius | Стандарт оформления кода | Quy ước viết mã nguồn | 程序风格
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Programming style".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world