0% found this document useful (0 votes)
2 views10 pages

6 Namespaces

The document discusses the importance of modularity in programming, particularly in telecommunications, highlighting how namespaces and interfaces can help manage complexity by separating code into distinct parts. It explains how namespaces can prevent naming conflicts and facilitate access to code components, while interfaces ensure that implementation details remain hidden. The document also provides examples of how to effectively use namespaces and interfaces to create a modular codebase.

Uploaded by

Majito jungle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

6 Namespaces

The document discusses the importance of modularity in programming, particularly in telecommunications, highlighting how namespaces and interfaces can help manage complexity by separating code into distinct parts. It explains how namespaces can prevent naming conflicts and facilitate access to code components, while interfaces ensure that implementation details remain hidden. The document also provides examples of how to effectively use namespaces and interfaces to create a modular codebase.

Uploaded by

Majito jungle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Namespaces and

code modularization
Programming for Telecommunications
Filippo Campagnaro
Programming for TLC

[email protected]
Outline

1. Modularity

2. Namespaces

3. Access to namespaces
Programming for TLC

4. Interfaces

[c++pl] Chapter 14
Composition problem
• Any realistic program is composed by multiple,
separate parts – think of functions, classes, etc
• A good implementation should be based on
modularity
keep separate allow the access
things/concepts only through a
/abstractions well-specified
separate interface
Modularity

There is not a specific language feature to support


modularity, but it can be achieved through namespaces,
classes and functions.
3
Need for modularity
• Consider two libraries

// a library for shapes


class Shape { /* ... */ };
class Line : public Shape { /* ... */ };
class Poly_line: public Shape { /* ... */ };
class Text : public Shape { /* ... */ };

// a library for text


class Glyph { /* ... */ };
class Word { /* ... */ };
class Line { /* ... */ };
class Text { /* ... */ };
Modularity

• If a program uses both of them, it will not compile,


because Line and Text have multiple declarations
4
Namespace
• It represents a set of facilities that directly belongs
together (e.g., the code of a library) because of
• logical relationships
• the functionality these facilities provide
• Members of a namespace are all in scope
• Namespaces are open, i.e., it is possible to add members
to a namespace from multiple locations
• E.g., consider multiple classes in a namespace, defined in multiple
files
namespace TextLibrary {
// a library for text
Namespaces

class Line { /* ... */ };


class Text { /* ... */ };
}; // end of namespace TextLibrary
5
Access to namespace members
• Explicit qualification
TextLibrary::Line line_object {};

// ::GlobalMemberName can be used to access


// members from the global namespace, which
// are otherwise shadowed by local variables

• using declarations
using std::string;

string a_string {"hello"};


// instead of std::string a_string
Namespaces

6
Access to namespace members
• using directives

using namespace std;

string a_string {"hello"};


vector<string> vec {a_string}
// instead of std::string a_string
// and std::vector<std::string> vec

Use them with care


• They may lead to the same name clashes that namespaces were
introduced to avoid
Namespaces

• Don’t place them in the global scope of an header file (which could
be #included anywhere)

7
Access to namespace members
• with the argument-dependent lookup
• search for a function in the namespace of its
arguments
• it is particularly useful for operators
• the rules to find a function with the matching
signature are as follows:
• if the argument is a class member, first check the class,
then the namespace
• if the argument is a namespace member, first check the
namespace, then the (eventual) enclosing ones, up to
Namespaces

the global
• if the argument is built-in (int, char, bool, etc), there
are no associated namespaces 8
Modularization and interfaces
• A program is a combination of separate parts
• Each par needs access to the functionalities provided by
another part
• This can be done by defining interfaces
• They should be the only way to access the functionalities of a part
of code (a “module”)
• The implementation details should be hidden
• Data-hiding principle of the data abstraction programming
paradigm
• Interfaces can be defined with
• Namespaces (which could define libraries and modules)
Interfaces

• Classes and object-oriented programming

9
Example of modular codebase
TCP module

interacts with the


which hides the Congestion control
Congestion control interface
implementation

Retransmissions
Retransmissions interface
implementation

Header parsing
Header parsing interface
implementation
Interfaces

In this way, the TCP program can use – for


example – different congestion control
algorithms without changing the code 10

You might also like