-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29_writing_clean_functions.py
More file actions
94 lines (74 loc) · 4.5 KB
/
Copy path29_writing_clean_functions.py
File metadata and controls
94 lines (74 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#======================================================================================
# Bad Style
#======================================================================================
def DiscPrint(p, r):
print('Calculating discount')
p = p - (p * r/100)
print(p)
DiscPrint(80, 20)
#======================================================================================
# write functions that are easy to read and understand(for you and others!)
#======================================================================================
#--------------------------------------------------------------------------------------
# Rule:1 Use snake_case for Function Names
#--------------------------------------------------------------------------------------
# write function names in lowercase and separate words with underscores
# ex:load_data_from_file
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# Rule2: Use Clear Descriptive Function Names
#--------------------------------------------------------------------------------------
# Describe exactly what the function does
# Start with a verb
# Use full words, avoid abbreviations
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# Rule3: Parameters Names Describe Their Values
#--------------------------------------------------------------------------------------
# Use full, meaning words
# Avoid abbreviations and single letters
# ex: "price", 'country_code"
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# Rule4: Always Describe Functions using Docstring
#--------------------------------------------------------------------------------------
# A docstring is a description of a function written inside triple quotes(""" """).
# Help teammates understand your code
# Help future you remember the logic
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# Why not use comments instead of docstring?
#--------------------------------------------------------------------------------------
# comment #(python ignores comment, they are only notes for us)
# Docstring""" """(python stores it inside the function and can be reused by tools, editors..)
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# Rule5: Replace Prints with Return to send data back to program
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# Rule6: Don't change parameter values directly, create local variables for any processing.
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# Rule7: Use Data Type Hints
# Always add type hints to parameters and return to make the function easier to understand
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# Rule8: Explain Args and Return in Docstring
#--------------------------------------------------------------------------------------
# Always describe what goes in and what comes out of the function in the docstring
#--------------------------------------------------------------------------------------
#======================================================================================
# Good Style
#======================================================================================
def calculate_discount(price:float, rate:float)->float:
"""
Calculate the final price after applying a discount.
Args:
price (float): Original Product Price.
rate (float): Discount Rate as numbers(e.g 20 for 20%)
Returns:
final_price (float): Final Price after applying discount.
"""
final_price = price - (price * rate/100)
return final_price
calculate_discount(100,20)