Professional Documents
Culture Documents
1
Lesson Learning Outcome
2
General Rules
3
Rule #1: Explicit is better than Implicit
Bad Good
▪ In the good code above, x and y are explicitly received from the
caller, and an explicit dictionary is returned.
▪ The developer using this function knows exactly what to do by
reading the first and last lines, which is not the case with the bad
example.
4
General Naming Guidelines
▪ Variables, functions, methods, packages, modules
lower_case_with_underscores
▪ Private methods
__double_leading_underscore(self, ...)
▪ Constants
ALL_CAPS_WITH_UNDERSCORES
5
Naming Conventions – contd.
6
Naming Conventions – contd.
person.set_age(42) person.age = 42
7
Indentation & Imports
▪ Use four(4) spaces - and never use tabs.
Bad Good
8
Use of Comments
▪ Use meaningful comments but use them sparingly
Bad
# If the sign is a stop sign
if sign.color == 'red' and sign.sides == 8:
stop()
Good
def is_stop_sign(sign): Prefer code readability to
return sign.color == 'red' and sign.sides == 8 writing a lot of comments.
Often, small methods are
if is_stop_sign(sign):
more effective than
stop()
comments.
9
Line Lengths
Good
wiki = (
"The Colt Python is a .357 Magnum caliber revolver formerly manufactured "
"by Colt's Manufacturing Company of Hartford, Connecticut. It is sometimes "
'referred to as a "Combat Magnum". It was first introduced in 1955, the '
"same year as Smith & Wesson's M29 .44 Magnum."
)
10
One Statement per line
Bad
print('one'); print('two')
if x == 1: print('one')
if <complex comparison> and <other complex comparison>:
# do something
11
One Statement per line – Contd.
Good
print('one')
print('two')
if x == 1:
print('one')
cond1 = <complex comparison>
cond2 = <other complex comparison>
if cond1 and cond2:
# do something
12
Lesson Summary
▪ General Rules
▪ Explicit is better than implicit
▪ Naming conventions
▪ Use of comments
▪ Indentation & Imports
▪ Line lengths
▪ One Statement per line
13