I am reading Algorithms 4th Edition by Robert Sedgewick and in chapter 1.2 it di
ID: 655496 • Letter: I
Question
I am reading Algorithms 4th Edition by Robert Sedgewick and in chapter 1.2 it discusses API design. It says:
"The key to success in modular programming is to maintain independence between modules. We do so by insisting on the API being the only point of dependence among modules."
Now the book is based on Java. I am considering this in terms of C++ development (but I imagine concepts are similar).
Is this suggesting that if I have a library eg to do networking, that if a client #includes my net.hpp header (fictional name) and maybe sets to link with net.lib then no other includes or libraries should be required? Is that the jist of this? Or is there something else?
Explanation / Answer
No, the point is that to use one specific library, all you need to know about it should be in its public interface (in C++, typically the header file). You should not have to know anything other than that, particularly not anything about what algorithms, data types, variables etc. exist within the library. If you did know, you might start relying on this information, and you would be burnt when the library implementation is changed one day.
It is quite common that using one library requires you to include more headers from other libraries - for instance, using a HTTP library might require you to import the definitions of request or packet definitions that the HTTP library itself uses in its API. But it shouldn't require you to import a LinearTrie data structure merely because the HTTP library uses one in its private code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.