CSC/ECE 517 Fall 2010/ch2 4d RB

From Expertiza_Wiki
Jump to navigation Jump to search

Introduction

Namespaces are used in Computer Science to group similar items into different logical units, so that one item can be un-ambigously differentiated from another item. Trivial it may seem, but creating namespaces is one of the most basic activities of modern day programming. It makes a program more organized and less prone to errors.

Why namespaces

The Problem

In general programming languages like C++, variable names, function names, structure names, class names, union names and enumerations fall under one general category called names. While writing big programs involving several programmers, the situation is likely to go out of hand if proper control is not exercised on the visibility of these names. For example, lets consider the following:


//library1.h
char hello();
void print();
class EmpSal
{
  char* ename;
  int esal;
}
// library2.h
char hi();
void print();
class EmpSal
{
  char* nm;
  float tax;
}

In the above examples, we have two header files, library1.h and library2.h which have the function void print() and class EmpSal. Now if both the header files are included in a program (see below) and a reference to the class is made, or the function is called, then this situation becomes ambiguous.

//prog.c
#include "library1.h"
#include "library2.h"
void main()
{
 print();   // ambiguous call
 EmpSal es = new EmpSal(); // ambiguous reference EmpSal a class
}

We can see in the above code that a call to the method print() and creation of the object EmpSal es is ambigous. Since both the header files contain this method and this class, the compiler won't know which library is being referred here. In such a case, the compiler flashes an error message of name clash.

Possible solutions to this problem

The first solution can be to modify the name of the method and the class in one of the libraries. This will prevent a name clash and the code will compile correctly. But this is not a feasible solution. It might be a case, where the library is being referred in other parts of the program which we don't have access to. So making changes in the library will cause problems in other parts of the program. Secondly, the libraries may be "read-only" and we may not be allowed to change any part of the library. Thirdly, even if are able to make changes in the library and make our code run, this is not the best approach as this requires a lot of re-work and it may lead to potential bugs in the program.

The second, more appropriate approach can be to use the concept of namespaces and the libraries can be placed into specific namespaces. C++ by default provides a global namespace where all the class objects and methods are placed. The global namespaces can be split into further manageable pieces using the namespaces features in C++, Java, Python etc.

Lets take up an example of namespaces in C++ to understand this concept.

 //library1.h
namespace Library1
char hello();
void print();
class EmpSal
{
  char* ename;
  int esal;
}
// library2.h
namespace Library2
char hi();
void print();
class EmpSal
{
  char* nm;
  float tax;
}
using namespace std;
void main()
{
 Library1::print();
 Library2::printf();
 
 Library1::EmpSal es1;
 Library2::EmpSal es2;
}

In the above example, both the libraries are placed into different namespaces. This removes the ambiguity in the function calls. Each function can be referred uniquely using its namespace preceding it. Now it becomes to use the same names in different namespaces without any conflict. As long as the names are in different namespaces, each name will be unique because of the addition of the namespace identifier.

Namespaces in different languages

C++

Namespaces are using in C++ using the keyword namespace. This keyword can be used to specify a new namespace as well as can be used to access a namespace.

Specifying a new namespace:

namespace abc;
void display()
{
 cout<<"Hello Namespaces";
}

Accessing the function using the namespace:

using namespace abc;
display();

Java

The concept of namespaces is implemented in Java using modules. Java has a system of packages and sub-packages and each package contains properties and methods. In order to access the contents of a package, the keyword import is used. Lets look at an example to make things clear.

Creating a new module:

module java.Parentpack.childpack;
void display1();
void display2();

Accessing this module:

import java.Parentpack.childpack;
display1();
display2();

Ruby

Ruby has the concept of modules which can contain various methods. The modules can then be accessed using the . operator. Lets look at an example to look how modules are implemented in Ruby.

Creating a new module:

module module1
def display1
{
  puts "Hello modules"
}
end

Accessing the module:

require module1
display1

Facts about namespaces

Here are some facts about Namespace relevant to C++:

1. The syntax of namespace is similar to that of the class except for the semicolon beyond the closing brace.

2. Declarations that are not present in any namespace are be default in the global namespace.

3. A definition of namespace can span over multiple header files. Refer the example below for more details:

// header1.h
namespace mynmspace
{
 void print();
}
//header2.h
namespace mynmspace
{
 void display();
}

This style of namespace spanning is called extension namespace definition.