TDD - How To Start To Really Think TDD?

I read about Agile, XP, and TDD methodologies.

I have been in projects that say that he needs to do TDD, but most of the tests are somehow integration tests, or during the project, TDD is forgotten in an attempt to complete the codes faster.

So, as far as I know, I wrote unit tests, but first I start writing code instead of writing a test. I feel that the change in thought / design / paradigm is really huge. So, although you actually believe in TDD, you are actually reverting to the old style due to the temporary pressure / results of the project.

I have several classes in which I have clean code verified by the module, but I cannot continue this process when layouts fit into us. In addition, I sometimes see: "Isn't it too trivial to write a test for this."

What do you guys think I should handle this?

+64
tdd agile methodology extreme-programming agile-processes
Mar 25 '10 at 1:01
source share
19 answers

I’m curious that none of the answers so far has touched on what I consider to be a fundamental insight into modern development methods, and that this is an “old-fashioned” way of writing software, collecting requirements, doing analysis and modeling the desired system before writing any code is actually a lot for him.

TDD actually embodies this to some extent.

To write a test that you first need to know is very simple to express: what your inputs will be and what your expected results will be.

Once you have this knowledge, you can write a test to execute part of the mythical code, as well as to what extent other artifacts the code will interact with before writing this code or creating these artifacts.

This is what we previously called “requirements engineers” and “system analysis” in the old waterfall method (s).

In addition, you will find that as soon as you understand the requirements at this level, writing tests will be natural (this is, after all, just an expression in the code of the functionality operator embodied in these requirements).

And when writing code that expresses requirements in the form of tests, you will identify gaps and misunderstandings in the requirements before you complete these gaps and misunderstandings for your project in the form of executable code.

For modern Agile practitioners, the methods to recognize that they are engaged in a series of “waterfalls” are, I think, too embarrassing, so this need for developing requirements and understanding is clouded over by a language that speaks of the need to address these things, desperately trying to admit that " Agile "(as is usually understood or perhaps misunderstood) abandon a child with a large part of the bath.

+41
Mar 25 '10 at 3:18
source share

So, as far as I know, I have written unit tests, but I find myself first starting to write code instead of writing a test. I feel there is a thought / design / paradigm that is really huge. So, although one really believes in TDD, you actually end up returning to the old style because of the temporary pressure / project expected results.

You can try to be completely disciplined in relation to TDD for a certain period - say, a couple of weeks or a month. When you try to write code before testing, delete it and start with the test with an error. Do it until you know what you can, until you know what it is - and then you have the opportunity to do it, and the freedom to make an informed choice - and understand that it is normal if this choice is sometimes encoded first, But not let the habit distract you from good practices. First leave the good practices, and then choose when to apply them (and, of course, you can find the answer to this question “always”).

I have several classes where I have a clean, modular, tested code, but I can't seem to continue the process when the bullies come into the picture. In addition, I see once: "Isn't it too trivial to write a test for this" syndrome.

Cats are one thing: are you still good at them? If you don’t like layouts and you are in a situation where layouts are suitable, then practice until, like TDD, you succeed.

Regarding the "too trivial" problem - not within the first two weeks. Just TDD all the time. And when you find that TDD leads you to a better design - even for "too trivial" code - pay attention to this. Think about what your code would look like without it. And you may find that the code is not too trivial for TDD. Or not. But I want to say, seriously, for a considerable period, and then you can make a better choice. Feel comfortable then rate.

+21
Mar 25 '10 at 6:12
source share

It's simple:

By learning to think about the "What" __before__ you think about the "How" 

In other words, think about what exactly you want to do (interface), and not how you are going to do (implementation)

TDD, as a design tool based on my experience, really helps you look at things from the perspective of the user than the perspective of the encoder. Also, I think TDD helps your mind really think about what exactly you are trying to do, what the expected result, etc.

So, the next time you try TDD, ask yourself what exactly you are trying to do, and just start writing code that expresses your intention.

Example:

Say someone wants you to write them down as a whole adder.

A person who does not have the TDD skill will simply do:

 int add(int a, int b) { return a + b; } 

Is the above code correct? Of course yes. But this approach, based on my experience, fails when you have a complex recording component. (That's why you asked this question in the first place, I know, I was there before (maybe yet? Lol))

The great thing about TDD is that it forces you to first of all pay attention to the interface (what) of the system, without immediately asking you about the implementation (how).

In other words, if someone asked me to write an adder, I would have something like:

 void assertOnePlusTwoEqualThree() { assert( add(1,2) == 3 ); } 

Please note that even before I even thought about how add () should work, I already found out a little. That is, I already understood:

  • the interface of my adder as inputs and outputs
  • first trivial test example (unit test for free!)

then you implement add ().

See, it doesn’t matter if the logic discussed here is so simple to implement. To have TDD thinking, you need to apply it all the time without any exceptions. You have to do this so many times that you no longer think about it. This is just part of how you develop. I can say this because I saw how it happened to me professionally (it took a year of perseverance).

In the same way as if you always do a good job of programming, regardless of complexity, you approach your work in the same way.

Finally, I think TDD is comparable to a "code sketch." That is, you begin to check whether the interface works well for each scenario (test case). Therefore, it is ok if you end up changing the interface, names, etc. See what I learned that many times design is just for understanding the problem. TDD is one of the tools that allows you to do this.

The human mind, IMO, is easier to work with specific examples (test scripts / scripts), rather than abstract thinking (how to implement something). With test cases, it allows your mind to gradually learn about the problem that you are trying to solve at hand.

This is normal not to know (going back to the "traditional" way ... relax, let your mind adjust). This is normal if it is not perfect (writing the implementation code is all right ... your brain is just trying to figure it out). Just keep trying, keep reading, keep coding, but never give up! =)

+11
Apr 09 2018-10-09T00:
source share

"So, although someone really believes in TDD, you actually go back the old style because of the time pressure / design results."

Actually, I do not think that this may be true.

If you are "reverting to the old style," the only reason may be that you do not believe that TDD will create better code faster.

I think someone is stopping using TDD because they feel like it is producing bad code faster.

+7
Mar 25
source share

Disciplines.

Make the decision to use TDD and stick to it, it completely depends on your own ability to complete the process and see it to the end.

+4
Mar 25 '10 at 1:13
source share

Try practicing the kata code .

Kata is designed to be remembered. Kata students study it as a form, not as a conclusion. This is not the conclusion of the kata that matters, these are the steps that lead to the conclusion. If you want to rely on think about how I think I design for development, then you must learn to react to minutia the way I react. By following this form, you can do it. When you study the form and repeat it and repeat, you will make your mind and body respond as I respond to the minute factors that lead to the development of the solution.

The kata code let me know what TDD should feel like. I know earlier when I start to lose rhythm in order to return to old habits. At this moment I know that I need to take small steps, perform tests more often, make sure that I do not try to reorganize under red color, etc.

+4
Mar 25 '10 at 3:29
source share

Buy Kent Drick's Test Driven Development: By Example and read it.

Then write the failed unit test.

+3
Mar 25 '10 at 1:05
source share

Trivial code is where errors hide. Mostly I think, because it is human nature to read what was supposed to be there, and not what is.

I tried to discipline myself to keep an eye on TDD in all of my new projects - it's hard to break old habits, but I think it's worth it. This seems to take longer since there is more precoding, but debugging time is going down.

Do not think about how you will test the code while writing it. Write tests that show how the code should work, and then write the simplest thing that tests pass. Wet, rinse, repeat. Your code will probably not be as you imagined, but it will definitely work better.

In addition, it is important that your tests fail. Written tests that pass will not help to find defects. And this is another very good reason for writing tests in the first place - if you write the code first and then the tests, your tests will be written to pass the code and (inadvertently) biased to check the code, and not the desired result. And how do you know if your tests fail in the absence of the correct code? Since tests are just code, they are also error prone.

So first write your test, then code, differently in parallel. Use tests to claim that your code is correct, and use code to claim that your tests are correct (yes, it may happen that you are sure that your algorithm is perfect, but the tests still fail because you did a dumb typo in the test. I did it this morning).

+3
Mar 25 2018-10-25T00:
source share

A TDD couple program (ideally with someone better than you),

Even if it is during free time

TDD is one of those skills in which at least it’s easy for me to be worse than I think. My TDD skill is always improving when I am in a TDD project and programming a couple with someone who can tell me when I can do TDD better. Even programming with someone who doesn't know this better than me helps me translate my ideas better than working alone.

Find (or run) Code Rollback or dojo coding, with particular emphasis on the Pairing Program and TDD.

If your company allows you to organize it by organization in the company or at least on the spot in your company, even if it is only an hour a week or at lunch.

+2
Mar 25 '10 at 16:12
source share

What do you guys think I should handle this?

I suggest you think that your module tests like dialogs with your object (provided that you program using an OO-enabled language such as Java, C #, Ruby, PHP, etc.)

Create a test case class, think in your test method about the interaction that you want to have with your objects. Imagine that you are talking to an intelligent machine using the method that you pass to your objects as verbs, and then use Asserts to verify that the objects react the way you want them to react.

After a while, you can enjoy this programming style so much that you do not expect that you will program without it; -)

+2
Mar 29 '10 at 5:43
source share

TDD is really good practice (but at the same time, I don’t think we should try to achieve 100% code coverage).

Main advantages:

  • This makes the developer think of the API, because he must first use this API to write tests. At this step it is very easy to feel that something is wrong with your API (for most ordinary operations, incorrect method names are required, for many ordinary operations there are too many exceptions with checking odd checks or vice versa, the user does not have the ability to handle alleged exceptions). And you can improve it and change it as you like, because no one has used it yet.

  • You think about how your methods should work. This often opens up some implicit problems or misunderstandings in the early stages.

  • When you have tests, you have a good way to measure the percentage of work completed (i.e. if you have 20 tests and 15 of them passed, then 75% of the work is completed). This is more important to you as a developer than to your team or process. You simply divide the entire task into many smaller tasks (for example, 20) and you can compete with them one by one. This is much nicer than taking it all on.

  • Tests let you play with code. You can do refactoring, you can increase productivity, etc. And again, even if you do not, it is very pleasant to understand that you have the opportunity to do this. This is a kind of confidence that everything is in order.

How to force yourself to do TDD? You do not need! If you want to get all these advantages, then you will not become yourself - you simply will do TDD with pleasure and your free will. If you do not need / want / not get them, do not do it now.

+1
Mar 25 '10 at 1:20
source share

TDD is not so much about writing tests first. If all you need to do is write your tests first, then you need the Test-First technique. The problem is that the Test-First method in a vacuum (without TDD) ends up breaking up into something that is almost worse than no tests at all.

You said that your problem was on its own :

or during the project, TDD is forgotten in an attempt to complete the codes faster

However, if you were doing TDD and recognizing all the value that he could offer, your team would understand that it is impossible to make code faster without TDD, as this is what speeds up the programming process in the first place.

My suggestion is to spend some time understanding the value that it can give you, recognizing the types of artifacts that a good test can replace. With true TDD, one document fills all of the following roles / needs:

  • Test (does it work or not?)
  • Analysis (what are we trying to do?)
  • Design (how do we do this?)
  • Specification (how do we know when we are done?)
  • Finish line (hey, you're done!)
  • Progress Report (hey, are they done, yet?)
  • Process control (what should I do next?)

... and so on. My opinion is that the commitment to learn TDD is a tough discipline and takes a lot of practice to master - requires a clear understanding of the real prize that is at the end of the trail.

Once you clearly understand this, you can begin to worry about how to do TDD.

+1
Mar 25 '10 at 1:38
source share

When you are in a big mess of legacy code, I found "Working Effectively with Legacy Code" extremely helpful. I think that improve your motivation for highlighting TDD, even when it comes to writing unit tests, before you make any changes to your old legacy version code. And because of your question, it seems that this is the position that you were.

And, of course, as many others have pointed to discipline. After some time forcing yourself, you will forget why you have ever done it differently.

+1
Mar 25
source share

TDD is knowing what you are doing and what you have done. If I have the correct TDD setup, I can comment on some line of code that may not be needed, and then immediately run the tests and find out if this was really necessary when testing - or not, without testing. These tests should contain enough description to understand why this line of code is needed.

Without TDD, which determines which interruptions, when you change the code, it requires potentially massive regression testing of people before you make sure that something has not broken. The goal of TDD, as with all parts of Total Quality Management, is to remove most, if not all, of the need for expensive manual testing at the end of the product line. How? Knowing what you are doing. How? By measuring what you are doing, and learning how to improve it.

If you find an error in your code and fix it. Questions should be: how did this happen? Can I prevent this in the future? How?

This may be due to poor requirements. This may be due to factors beyond your control. The key is not just to brush the problem under the carpet, but try to understand and learn how to do it, and incorporate it into your processes.

TDD is only part of this and will not work without a compatible and encouraging environment.

To get this control, you need to buy the basic principle that flexible methods make change cheaper in the long run. Using flexible methods, the cost of change increases logarithmically, while development without flexible methods leads to the fact that the cost of change increases exponentially over time.

Read about it much more. Google following "William Edward Deming," "Full Quality Management," "Change Cost Curve."

+1
Dec 16 '15 at 16:50
source share

My team is trying to improve the TDD approach - I have to say that this is not the easiest thing with a larger development team.

Everyone on the team should think with their test first hat before writing any production code. This can be tricky, because despite the fact that people can say what they will do, there is no guarantee as soon as they get to their keyboard.

For some managers, TDD may seem like an expensive approach, as I believe it might take a little longer to encode something (and bean counters see it), but this is a short-term “pain” for long-term gain. You have (hopefully) well-written, tested code, with a mechanism to collect errors in the future when something changes.

For anyone who is not convinced, give a TDD as an example of what you read and try repeating the iteration of your software.

0
Mar 29 '10 at 8:52
source share

Many coding practitioners accept ... practice.

  • In a few hours, you will be able to learn and understand the concept of OO programming. But many programmers admit that only after practical OO programming for about 2 years did they suddenly feel “fluent”.

  • A C ++ programmer can easily "learn C #" in a few days. But it still requires a lot of benefit before they are "free" and understand the full implications of everything they write (instead of being a C ++ programmer who converts syntax to C #).

  • I initially learned to enter two fingers. This progressed to such an extent that I could type this method pretty quickly, but decided to try learning touch-typing. For several weeks I forced myself to feel pain and disappointment in the fact that you need to type everything very slowly using touch input. It was hard and required a real commitment, because with every line I typed, I just wanted to bash with what I already knew, in a way that would give me quick results. But in the end, my typing speed coincided with my bi-directional printing.

  • I resisted the MFC prefix conventions (for example, the "m_" and "p" prefixes) when I first started working with MFC. It seemed an ugly, funny, and pointless addition. Then I forced myself to use this system for a month, and after that time I could not understand how I ever wrote code without this naming convention - it helped me understand the code better and faster and reduced the number of stupid mistakes.

So how do you develop the discipline to do TDD? Just take yourself for TDD for a month. It will be slow. It will be hard. It will be tempting to simply bash output a few lines of the "old path". But after a while you will increase your experience and speed, and it will become much easier and more natural to work in this way. And then you can evaluate TDD and not TDD and decide which approach works best. You probably do not look back.

0
Apr 11 '10 at 19:56
source share

TDD is very difficult to start if you are in a vacuum, and it is almost impossible to do when other people in your team only speak out loud TDD. My advice is to find a place (at work or out of work) where you can either program on your own, or even better with a devoted friend or colleague, and just start doing it all the time and without exception.

TDD (and Agile, for that matter) is just starting to push when you and your team are all behind, and pledged to make it work. TDD , . . -, . .

0
06 . '11 21:53
source share

TDD- ...

T TDD - TODO Task Driven

, TDD - (, ). ( : , .)

, /, . , , TDD /.

, , , , .

0
06 . '13 23:31
source share

IMO, TDD // - , :

  • ,
  • , , , .
  • > > .
  • , , , . .

NTN.

:

  • , , , ( / ),
  • / , , , , . , , .
-one
25 . '10 1:21
source share



All Articles