Notes on Sections 2.1-

Download Report

Transcript Notes on Sections 2.1-

Room 408 Indention Guide
• Every programmer has his/her own way of
indenting
• Most places you will work as a programmer will
have uniform indentation standards
• These will be ours
– They are not universal. Many programmers might
disagree with some of them.
– However, your programs are expected to conform
Indenting Classes
• Members and methods in a
class should be indented
• Close bracket at end should
be indented the same as
class
• Open bracket can be on same
line as class or next line
• Classes are usually in their
own file
class myClass{
void method1(){
code
}
}
Indenting Methods and Functions
• Statements inside the
method or function must
be indented
• Close bracket at end
should be indented the
same as the definition
• Open bracket can be on
same line or next line
void myMethod(){
statement1
}
Indenting Ifs
• Dependant statements
– Must be on subsequent
lines
– Must be indented
if (a==b)
a=a+1;
• Open brackets may be on
the same line or the next
if (a==b) {
-ORif (a==b)
{
• Each level uses the same
number of spaces
if (a==b)
if (b==c)
c=c+1;
Indenting Ifs cont’d
• Else statements must be
indented the same as the
corresponding if
• Statements dependent on
the same if or else must
be indented the same
• Close curly brackets
must be indented the
same as their if or else
if (a==b)
a=a+1;
else
b=b+1;
if (a==b) {
a=a+1;
b=b+1;
}
Indenting Ifs cont’d
• The if should be even
with the statement before
it
a=a+1;
if (a==b)
b=b+1;
Indenting Loops
• Dependant statements
– Must be on subsequent
lines
– Must be indented
• Open brackets may be on
the same line or the next
• The top of the loop
should be even with the
statement before it
while (a<b)
a=a+1;
do {
-ORdo
{
a=a+1;
while (b<a)
b=b+1;
Indenting loops cont’d
• Statements dependent on
the same loop must be
indented the same
• Close curly brackets
must be indented the
same as the top of the
loop
while (a==b) {
a=a+1;
b=b+1;
}
No line should “wrap” to the next line
• Avoid:
System.out.print(“This line is
too long to fit”);
if (myInstance.myMethod(4,6)>=M
ath.random()){
• Instead keep code indented:
System.out.print(“This line is ”
+”too long to fit”);
if (myInstance.myMethod(4,6)
>=Math.random()){