O'Reilly logo

Quality Code: Software Testing Principles, Practices, and Patterns by Stephen Vance

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Becoming friend-ly

A relatively small number of languages have the equivalent of the C++ friend keyword. For example, Microsoft C# has the InternalsVisibleToAttribute class that can be used to grant access from specified contexts.

While many consider the friend keyword to be “dirty programming” and to violate principles of encapsulation,4 it is often cited as having positive value for testing purposes, especially unit testing. Consider a C++ implementation of a poker hand (Listing 9-10), in this case in a simple game of five-card stud. Of course, you do not want to reveal your cards to anyone else.

4. You can find a good discussion of this at www.cprogramming.com/tutorial/friends.html. Scott Meyers infers something similar when he says, “It ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required