I want to make sure that the code that the written code is precisely doing what it is expected to do.

For example, if I want to search in an array particularly using linear search, I don't want binary search to pass the test. Unit test to test the method "search" would pass irrespective of the implementation which is not what I want. (I understand that binary search won't work if the data is unsorted, but lets say the data is sorted and I still want to make sure that it is a linear search and not the binary. In this case, I know that for sufficiently large input, BS would be way quicker than LS, but I want a generic answer where there might not be considerable difference in performance.)

Is there a way to achieve this?

An idea comes to my mind - Divide the method in extremely small submethods and make sure that all the submethods are implemented correctly and are used in main method. However this is not desirable, this might not be feasible always.

  • $\begingroup$ What do you want to do? Check if the code is well-written requires reading the code. I just don't see any way around it. $\endgroup$
    – vonbrand
    Commented Sep 13, 2019 at 13:40

1 Answer 1


To find out if it is e.g. doing binary search (or using hashing, or whatever) inside, you have to look at what it is doing. No way around it.

Why would you insist on a particular algorithm? The whole point of modularity (and thus of much of modern programming languages and techniques) is to be able to hide such irrelevant details, and have users depend only on observable behaviour (so you can go back tomorrow and replace a simple, inefficient algorithm by a complex, efficient one, and noone is the wiser).

  • $\begingroup$ You are correct about modularity. Think in terms of students. If someone only know, say merge sort and not quick sort. As long as the sort() method is needed, that person would get away with merge sort everytime. But there would be some problems where knowledge of quick sort would be critical. That person would struggle there. Thats something I want to avoid. In general, knowing internal details help in tweaking some things in future. Hence, sometimes its desirable that the implementation is in some specific way. $\endgroup$ Commented Sep 5, 2019 at 4:40
  • $\begingroup$ If you want a specific añgorithm, read the code and check. $\endgroup$
    – vonbrand
    Commented Sep 7, 2019 at 19:45
  • $\begingroup$ Don't be salty please. The title of the question itself says "without reading the code". Of course, if there is no way to do it, one can always read the code. $\endgroup$ Commented Sep 11, 2019 at 18:15
  • 1
    $\begingroup$ Best you can do is to request some "efficiency" measure, i.e., the algorithm has to sort with $O(n \log n)$ assignments/comparisons, instrument the data sorted, and run it on some (selected) examples. But that won't let you distinguish e.g. mergesort and quicksort. And they could cheat by sorting off-line by bubblesort and permuting into the result. $\endgroup$
    – vonbrand
    Commented Sep 13, 2019 at 13:39
  • $\begingroup$ Thanks for your input. Yeah, I can see that there just doesn't seem to be an automated way. One close enough way might be asking to write unit testable sub methods which combine to the whole algorithm. $\endgroup$ Commented Sep 14, 2019 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.