Namespaces is an actively used design pattern.

?Years Old
  • Namespaces does not currently rank in our top 50% of entities I track
  • I have 11 facts about Namespaces. what would you like to know? email me and let me know how I can help.

Languages with Namespaces include cpp, csharp, f-sharp, typescript

Example from cpp:

#include <iostream>
using namespace std;

// Variable created inside namespace
namespace first
{
  int val = 500;
}
 
// Global variable
int val = 100;
// Ways to do it: https://en.cppreference.com/w/cpp/language/namespace
namespace ns_name { declarations }
inline namespace ns_name { declarations }
namespace { declarations }
ns_name::name
using namespace ns_name;
using ns_name::name;
namespace name = qualified-namespace ;
namespace ns_name::inline(since C++20)(optional) name { declarations } 

Example from csharp:

namespace MyNamespace
{
    class MyClass
    {
        public void MyMethod()
        {
            System.Console.WriteLine("Creating my namespace");
    }
  }
}

Example from f-sharp:

namespace Widgets
type MyWidget1 =
   member this.WidgetName = "Widget1"
module WidgetsModule =
   let widgetName = "Widget2"

Example from typescript:

// Typescript even supports splitting namespaces across multiple files:
// Validation.ts
namespace Validation {
    export interface StringValidator {
        isAcceptable(s: string): boolean;
    }
}
// LettersOnlyValidator.ts
/// <reference path="Validation.ts" />
namespace Validation {
    const lettersRegexp = /^[A-Za-z]+$/;
    export class LettersOnlyValidator implements StringValidator {
        isAcceptable(s: string) {
            return lettersRegexp.test(s);
        }
    }
}

Last updated September 6th, 2019