Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Whether we should write an alogrithm first and then modify it to cover up all the test cases or
we should design our algorithm according to test cases???
Posted
Comments
[no name] 14-Sep-13 8:44am    
Yes
Brady Bar 14-Sep-13 8:55am    
Whether i should do the first thing or second thing????
[no name] 14-Sep-13 9:27am    
Yes, you should do the first thing or the second thing whichever one you want or neither.
Richard MacCutchan 14-Sep-13 9:27am    
Yes.
Joan M 14-Sep-13 9:28am    
Design, then implement. ALWAYS.

I think ThePhantomUpvoter is correct: you should use whatever technique is more appropriate for:
  • The problem complexity.
  • Your skills.

For instance, suppose the problem is 'easy'', then you can (and probably should) use the Joan Murt's approach: design the right algorithm, covering all the test cases.
On the other hand, suppose the problem is very difficult or you are lacking the required skills (or experience): you have to go through a more complex approach, for instance implementing a simple, not complete algorithm, suitable only for covering a small subset of the test cases. Then you might go on improving (or re-desing from scratch) such algorithm, based on the experience gained.
My two cents.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-Sep-13 21:08pm    
Agree, a 5.
—SA
CPallini 15-Sep-13 5:25am    
We agree! :-)
Thank you Sergey.
Sergey Alexandrovich Kryukov 15-Sep-13 11:39am    
Surprisingly. :-)
Did you see Solution 3 and my discussion with Harvey?
—SA
CPallini 15-Sep-13 14:49pm    
I've seen it just now, interesting.
Sergey Alexandrovich Kryukov 15-Sep-13 14:51pm    
:-)
What you are asking is somewhat central to the idea of Test Driven Development ("TDD"), which is turning out to be more coding religion than a best practice. I am an old dog learning new tricks and don't typically criticize something unless I have tried it and explored most of the corners.

I think I properly understand the issues surrounding TDD and the problems it is trying to solve but I have not been a convert to the central notion of writing an API, then the tests for it, then the actual code.

If you ask ten other people the same question you will likely get 4 or 5 unique opinions.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Sep-13 21:16pm    
Your opinion about "coding religion" may seem quite weird to many who used to trust and follow such conceptions, but I rather agree and vote 5.

You see, the more I look at many reputable works on development method and related issue, the more striking features of pseudo-science I can see.

It just needs some critical thinking and skepticism. Don't get me wrong: I'm strongly against purely practical approach without deep theoretical thinking. I only say that many "theoretical" works only pretend that they are logically proven, but in fact just accumulate some opinions based on limited set of practical applications (at best), and the applicability of their considerations is limited by the time and the level of technology of the time when those works were written. Authors pretend on universal character of their approaches, but they are not universal, by far.

—SA
H.Brydon 14-Sep-13 21:21pm    
Thanks Sergey (I think?). I think we belong to the same religious brotherhood.
Sergey Alexandrovich Kryukov 14-Sep-13 21:38pm    
:-)
nv3 15-Sep-13 18:00pm    
How true, Sergey! In my time as developer I have been going through several generation of so-called software engineers, all of whom pretended to know the one and only way of how to develop software the right way. And that indeed is similar to "religions" which mostly extend the same claims. My virtual 5.
Sergey Alexandrovich Kryukov 15-Sep-13 18:39pm    
Thank you very much.
—SA
I have two advices regarding test cases:

Briefly:
1. Sometimes (often) you don't need test cases at all.
2. The most important thing is to define the interface of the library well. The order in which you implement code on the two sides of the interfaces is unimportant (matter of taste). One side of the interface contains the code of the library itself, the other side contains different programs/other libs (including the test cases that counts as one user of the lib). I add an important note here: The world ins't perfect and requirements often change. Don't dream about the perfect interface and don't spend weeks designing it. Sometimes the interface must be changed when extending it isn't enough and in this case you have to adjust both the library code and the code of the library users (including the programs and test cases).

I have worked on some large codebases that made use of extensive unit testing. At the same time I've seen rolling and successful large projects without a single test case. Some projects (like the current one I'm working on) has test cases only for some of the libraries (for example our builtin script compiler) but our build (final built product) and its core features are constantly under testing (we have a department with a few QA professionals). Most of the code of our product changes too rapidly so the costs of unit testing would outweigh the advantages we would gain by unit testing.

When to make unit testing? Unit testing has several advantages and disadvantages.

- Advantages:
Easy to introduce new features without screwing up old features (you can test the old features after modifying the code). If the reliability of some libraries is important then writing tests for them is mandatory (for example libs used in the control software of an airplane). Another advantage in my opinion: with test cases the library interface has at least 2 users: The program that uses the library in the final product and the test case (that is another program using the lib) and this forces people to define cleaner interfaces and automatically prevents some lazy coders to push half of the code of a functinality to another libarary -> better separation of concerns on source code level.

- Disadvantages:
If a codebase is exensively tested then the test code can be half or more of the whole codebase! This involves extra maintenance that can be extremely costly (time/money) depending on the frequency of changes in old code. In rapidly changing code sometimes testing would terribly slow down the development process. For this reason sometimes the only interface that is tested is the user interface and important functionalities of the ready-made compiled program by QA people. :-)

Finally development is almost always about time and money. Someone at your company has to decide whether test cases are useful/useless in a given scenario and whether you have the time/money to invest for the advantages of test cases or not. Saying that unit testing is always good isn't fair, sometimes you pay much more than what you gain by unit testing.
 
Share this answer
 
v2
Comments
H.Brydon 15-Sep-13 19:34pm    
Whew! +5
pasztorpisti 16-Sep-13 3:12am    
Thank you!
Generally, you design your algorithm to include the the "special cases" - you then design tests to cover the algorithm, not the cases you have specified and coded.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900