0% found this document useful (0 votes)
3 views12 pages

Code Style

Programming style, or code style, consists of guidelines for writing source code that enhance readability and reduce errors, varying by programming language. Key elements of good style include code layout, indentation, spacing, and the use of comments, with specific rules for each aspect. Proper indentation and spacing are crucial for clarity, and each statement should be on its own line, with braces placed correctly in function definitions.

Uploaded by

18-QADEER AHMAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Code Style

Programming style, or code style, consists of guidelines for writing source code that enhance readability and reduce errors, varying by programming language. Key elements of good style include code layout, indentation, spacing, and the use of comments, with specific rules for each aspect. Proper indentation and spacing are crucial for clarity, and each statement should be on its own line, with braces placed correctly in function definitions.

Uploaded by

18-QADEER AHMAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CODE STYLE

INTRODUCTION

• Programming style, also known as code style, is a set of rules or


guidelines used when writing the source code for a computer program.
• It is often claimed that following a particular programming style will
help programmers read and understand source code conforming to the
style, and help to avoid introducing errors.
• Programming styles are often designed for a specific programming
language.
• Style considered good in C source code may not be appropriate
for BASIC source code.
10 CLS
20 PRINT "Helloooooooooooooo, world!"
30 PRINT "I'm making the sample program clear and understandable."
40 PRINT "This text is being printed via the PRINT command."
50 PRINT "On the next line, I'll use CLS, which will clear everything I just printed, so
you won't even see the preceding text."
55 PRINT "Also, you can't give CLS a line to PRINT; it won't actually do anything"
60 CLS "These words actually do nothing; they do not PRINT or anything."
70 PRINT "Finally, on line 80, I'll use END, which will keep me from reaching line 90."
80 END "Like CLS, putting a string here does nothing; it does not PRINT or anything."
90 PRINT "this is not really my answer."
ELEMENTS OF GOOD STYLE

• Good style is a subjective matter, and is difficult to define.


• However, there are several elements common to a large number of
programming styles.
• The issues usually considered as part of programming style include
the layout of the source code, including indentation; the use of white
space around operators and keywords; the capitalization or otherwise
of keywords and variable names; the style and spelling of user-defined
identifiers, such as function, procedure and variable names; and the
use and style of comments.
INDENTATION

• Indentation styles assist in identifying control flow and blocks of code.


• In some programming languages, indentation is used to delimit logical
blocks of code; correct indentation in these cases is more than a matter
of style.
• Use spaces, not tabs. Tabs should only appear in files that require them
for semantic meaning, like Makefiles.
INDENTATION

• Right: • Wrong:
int main() int main()
{ {
return 0; return 0;
} }
INDENTATION

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

} {
return false;
}
INDENTATION

if ( hours < 24 • The first two examples are


&& minutes < 60 probably much easier to read
because they are indented in an
&& seconds < 60
established way (a "hanging
)
paragraph" style).
{return true
• This indentation style is
;} else
especially useful when dealing
{return false with multiple nested constructs.
;}
SPACING
Do not place spaces around unary operators.

• Right: • Wrong:
i++; i ++;
Do place spaces around binary and ternary
operators.

• Right: • Wrong:
y = m * x + b; y=m*x+b;
f(a, b); f(a,b);
c = a | b; c = a|b;
return condition ? 1 : 0; return condition ? 1:0;
LINE BREAKING
Each statement should get its own line.

• Right: • Wrong:
x++; x++; y++;
y++; if (condition) doIt();
if (condition)
doIt();
BRACES
Function definitions: place each brace on its own line.

• Right: • Wrong:
int main() int main() {
{ ...
... }
}

You might also like