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 ...

Get Quality Code: Software Testing Principles, Practices, and Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.