You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/blog/tdd-design-benefits.md
+10-4Lines changed: 10 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,38 +18,44 @@ As defined above, we write unit tests even before we write the code. This might
18
18
To make this easier to understand, try to think of it like this. It is not testing but stating what you expect the code to do. With this, you are trying to put your expectation before writing the code.
19
19
This shift in mindset, helps you be very clear and concise about what you want and how you want your software to behave.
20
20
21
-
Since we don't have any code to test, this inversion of thought process and development style now poses a few interesting questions.
21
+
Since we don't have any code to test, this inversion of thought process and development style now poses a few interesting questions:
22
+
22
23
1. What do we expect from the function and how do we want it to behave?
23
24
1. What should be the function/method name? What would make sense from the caller's perspective?
24
25
1. What would be the Class name in the case of Object Oriented language?
25
26
1. What does the function need as dependencies for it to perform its task?
26
27
27
28
Let's see how these questions help us one by one
29
+
28
30
### Q1: What do we expect from the function and how do we want it to behave?
31
+
29
32
Answering this question helps us to focus on the exact intended outcome before implementing the code.
30
33
This enables us to break the problem statement into smaller chunks as we need to test for each expectation separately.
31
34
This helps us build the software incrementally, and discover edge cases by asking how should it work for weird input values.
32
35
33
36
### Q2: What should be the function name? What would make sense from the consumer perspective?
37
+
34
38
Since we would be having the consumer of the function even before it is implemented, it forces us to think of a good function name.
35
-
This helps us in understanding how different pieces of code will use this new function.
39
+
This helps us in understanding how different pieces of code will use this new function.
36
40
This significantly improves readability and guides us to arrive at meaningful names.
37
41
38
42
### Q3: What would be the Class name in case of Object Oriented language?
39
-
Naming as we all know is one of the hardest things for software engineers.
43
+
44
+
Naming as we all know is one of the hardest things for software engineers.
40
45
One thing I have realised over the years is naming in a meaningful manner only takes iterations.
41
46
But one easy way to get started in the right direction is to name things from the consumer's point of view.
42
47
What this means is when we start to think from the consumer's point of view, it starts becoming clear as to how it could be used.
43
48
And test cases are the first consumers of the code.
44
49
This like the previous one helps us improve readability ad arrive at meaningful class names.
45
50
46
51
### Q4: What does the function/class need as dependencies for it to perform the task?
52
+
47
53
When we write code, we sometimes expect certain things to be present or need to be passed in for the new code to work.
48
54
This helps us understand how complicated the code setup could be if it needs a lot of things.
49
55
This acts as an indicator and surfaces tight coupling if any early on in the development lifecycle.
50
56
We could consider this as a tool which would prevent the design of the system from having high coupling and reduce complexity.
51
57
52
-
> ### TDD is not just about having tests but a lot more than that. It not only helps us verify the correctness of the software but if practised well, helps us design the software well. This is why I sometimes think of TDD as **Test Driven Design**.
58
+
> ### TDD is not just about having tests but a lot more than that. It not only helps us verify the correctness of the software but if practised well, helps us design the software well. This is why I sometimes think of TDD as **Test Driven Design**
53
59
54
60
All of this sounds too good, but it's easier said than done.
55
61
Here is one of the many reasons why people are not able to successfully adopt this practice into their development lifecycle.
0 commit comments