Online Book Reader

Home Category

Professional C__ - Marc Gregoire [30]

By Root 1188 0
the licensing issues before using any library. For example, some open-source libraries require you to make your own code open-source.

Another consideration with reusing code is cross-platform portability. If you want to write a cross-platform application, make sure the libraries you use are also cross-platform portable.

Reusing code requires a trust factor. You must trust whoever wrote the code by assuming that he or she did a good job. Some people like to have control over all aspects of their project, including every line of source code.

Upgrading to a new version of the library can cause problems. The upgrade could introduce bugs which could have fatal consequences in your product. A performance related upgrade might optimize performance in certain cases but make it worse in your specific use-case.

Putting It Together to Make a Decision

Now that you are familiar with the terminology, advantages, and disadvantages of reusing code, you are better prepared to make the decision about whether or not to reuse code. Often, the decision is obvious. For example, if you want to write a graphical user interface (GUI) in C++ for Microsoft Windows, you should use a framework such as MFC. You probably don’t know how to write the underlying code to create a GUI in Windows, and more importantly, you don’t want to waste the time to learn it. You will save person-years of effort by using a framework in this case.

However, other times the choice is less obvious. For example, if you are unfamiliar with a library or framework, and need only a simple data structure, it might not be worth the time to learn the entire framework to reuse only one component that you could write in a few days.

Ultimately, the decision is a choice that you need to make for your own particular needs. It often comes down to a tradeoff between the time it would take to write it yourself and the time required to find and learn how to use a library to solve the problem. Carefully consider how the advantages and disadvantages listed previously apply to your specific case, and decide which factors are most important to you. Finally, remember that you can always change your mind, which might even be not too much work if you handled the abstraction correctly.

Strategies for Reusing Code

When you use libraries, frameworks, coworkers’ code, or your own code, there are several guidelines you should keep in mind.

Understand the Capabilities and Limitations

Take the time to familiarize yourself with the code. It is important to understand both its capabilities and its limitations. Start with the documentation and the published interfaces or APIs. Ideally, that will be sufficient to understand how to use the code. However, if the library doesn’t provide a clear separation between interface and implementation, you may need to explore the source code itself. Also, talk to other programmers who have used the code and who might be able to explain its intricacies. You should begin by learning the basic functionality. If it’s a library, what behaviors does it provide? If it’s a framework, how does your code fit in? What classes should you subclass? What code do you need to write yourself? You should also consider specific issues depending on the type of code.

Here are some points to keep in mind for any library or framework:

Is the code safe for multithreaded programs?

What initialization calls does the library or framework need? What cleanup does it need?

On what other libraries does the library or framework depend?

Here are some points to keep in mind for any library call you use:

If a call returns memory pointers, who is responsible for freeing the memory: the caller or the library? If the library is responsible, when is the memory freed? It’s highly recommended to find out if you can use smart pointers to manage memory allocated by the library. Smart pointers are discussed in Chapter 21.

What error conditions does the library call check for, and what does it assume? How does it handle errors? How does it notify the client program about errors? Avoid using

Return Main Page Previous Page Next Page

®Online Book Reader