-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.txt
191 lines (110 loc) · 5.33 KB
/
Functions.txt
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
Intro :
In early days of programming there was routines subroutines
Then programs subprograms and functions
now functions plays the major role in programming and it is important
to write a better function .
Small
The first rule of a function is small
smaller the function is easier to understanding it and bug free
Also Each function should tell a story and in a conseruence order
Blocks and Indenting :
A function with statements such as if should be only one or two lines
and if it is a function call it is much better
This makes the function easy to read and understand
Do One thing :
A function must do only one thing and must do it well if a function does more then one
we must split the funtcion
Doing one thing is one level of abstraction
eg : To check the pass mark of student
We get the mark
if it is above 40
we return pass
but the intention is to get the pass mark of the student which it does
Section within function :
Separating a section within a function is a bad idea as it again does multiple tasks
eg if we section decleration ,initializaztion in afunction it does two task
so we must not do it as function always does one thing
One level of abstraction per function
In order to make sure function does "One thing "we must also make sure that the
statement within a function does only one level of abstraction
Read from Top to Bottom :
We want function to be in consequtive order in next level of abstraction
so that it can be easily understandable and modifyable by the reader
Switch statement :
Do not prefer to use switch statements in a function as it does more than one
thing
if so in unavoidable cases make sure the switch statement is inherted and burried in
an low level of class and never repeated this can be acheiverd by polymorphism
hiding the switch stament in an interface so that it does not grow implicitly
Use Descriptive Names :
Take time and make sure to use a descriptive names in a function so that the intention
can be understood easily
A long descriptive function name is better than a long descriptive comment
Function Arguments
Argyments types
Niladic -0
Monadic -1
Diadic -2
Triadic -3
Most prefered Niladic and Mondic
Not prefered more than Triadic
Reason
Hard for Testing
Output Arguments :
They are harder to understand than the input argument
As we usually return the value in return method and passing them as an argument makes us
to double check them
Eg : append footer (s) :does s is input or output this can be only clarified by checking
initialization of function which is a ddouble check .
Here comes the Object Oriented to pass the args
One input Argument :
Next Best Arguments to none is one argument
Common Monadic form
There are two reason to pass single arguments in a function
We can pass single argument to check for the existance of a file
We can pass an input argument and modify them as perou need and return them
Transfomation should have a return value
Flag Arguments :
Flag arguments will fail the task of do one thing so never pass boolean as an argument
Dyadic Args :
Hard for Test
We may skip to check an arguments which leads to hide a bug
Best place to use dyadic args are caeristian pointers
eg :point =poinyt(0,0)
Triads args :
Harder than Dyadic and be much careful if we are to use them
Arguments Objects :
If we want to use two or more argument we can better wrap them into classes
Argument List :
We can use String.format method to pass an argument as a list
Verbs and Keywords :
We can better use verbs in method such as writefield(name)
Also assertexpectedequalsactual(expectedd ,actual) is much better
Have No Side effect :
A method should always do one thing and not have any side effect
Eg : check password validation
Command and Query Seperation :
If set("username","Uncle bob")
if (attributeexist(username )
Set ("username","Uncle bob") is better by seperating information and modification
Prefer exception to return Error code
If we handle the errors in a code it can be easy to seperate happy code and cann be simplified
Try/catch block :
As they are complicated it is better to use them in a seperate methods
Error shpuld handle one thing
It should do only one task and not more than that
Error in java dependency mangnet
Exception handling is much better than an error handling as we donot need to reconstruct
errors in Exception handling
Don't repeat yourself :
Do not make duplicates in code
As it takes modification complicated if we are to change
And more erroes if there is error in code
The main readon for base clss in OOPS is also to avoid duplicates
Structured Programming :
Djikstras says every functions and blocks between a functions should have one entry
and one exit
There should be only one return in a function and no break and continue and never goto
statement in a function
How to write a function ?
Refactor with this rules and also should follow the tests simultaneously