A Comprehensive Guide to Modular Programming across Languages
Table of Contents
Module Systems Across Languages
CommonJS
What is the primary use case for CommonJS modules? drill module_systems
Back
CommonJS modules are primarily used in Node.js for server-side JavaScript development.
How do you export a module in CommonJS? drill module_systems
Back
In CommonJS, you export modules using `module.exports` or `exports`, e.g., `module.exports = { functionName: function() { … } }`.
ES6 Modules
What is a key difference between ES6 modules and CommonJS modules? drill module_systems
Back
ES6 modules use `import` and `export` statements, while CommonJS uses `require()` and `module.exports`.
How do you import a specific function from an ES6 module? drill module_systems
Back
You can use named imports in ES6, e.g., `import { specificFunction } from './module.js'`.
Python Modules
What is the purpose of the `_init_.py` file in Python packages? drill module_systems
Back
The `_init_.py` file marks a directory as a Python package and can be used to execute package initialization code.
How do you import a specific function from a Python module? drill module_systems
Back
You can use the `from` keyword, e.g., `from modulename import specificfunction`.
Java Modules
What Java version introduced the module system? drill module_systems
Back
The module system was introduced in Java 9.
What is the purpose of the `module-info.java` file? drill module_systems
Back
The `module-info.java` file defines a module's dependencies, exported packages, and services.
C++ Modules
What C++ standard introduced modules? drill module_systems
Back
C++20 introduced modules to the C++ language.
What are the main benefits of C++ modules over traditional header files? drill module_systems
Back
C++ modules offer faster compilation times, better encapsulation, and reduced header dependencies.
Ruby Modules
What is the primary purpose of modules in Ruby? drill module_systems
Back
Ruby modules are used for namespacing and mixins, allowing code to be shared across multiple classes.
How do you include a module in a Ruby class? drill module_systems
Back
You use the `include` keyword followed by the module name, e.g., `include ModuleName`.
Go Modules
What command initializes a new module in Go? drill module_systems
Back
The command `go mod init` initializes a new module in Go.
What file is created to manage dependencies in Go modules? drill module_systems
Back
The `go.mod` file is created to manage dependencies in Go modules.
Rust Modules
How do you declare a module in Rust? drill module_systems
Back
You declare a module in Rust using the `mod` keyword, e.g., `mod modulename;`.
What is the purpose of the `use` keyword in Rust? drill module_systems
Back
The `use` keyword in Rust is used to bring items from modules into scope, similar to `import` in other languages.
Swift Modules
What is the default access level for entities in Swift modules? drill module_systems
Back
The default access level for entities in Swift modules is `internal`.
How do you make a Swift class or function public? drill module_systems
Back
You add the `public` keyword before the class or function declaration, e.g., `public class ClassName { … }`.
PHP Autoloading
What is the purpose of autoloading in PHP? drill module_systems
Back
Autoloading in PHP automatically includes class files when they are used, eliminating the need for multiple `include` or `require` statements.
What PSR standard defines autoloading specifications in PHP? drill module_systems
Back
PSR-4 defines the autoloading specifications in PHP.
TypeScript Modules
How do TypeScript modules differ from ES6 modules? drill module_systems
Back
TypeScript modules are very similar to ES6 modules but add support for type information and additional features like `namespace`.
What is the purpose of the `export` keyword in TypeScript? drill module_systems
Back
The `export` keyword in TypeScript is used to make a declaration available for use in other modules.
Haskell Modules
How do you import a Haskell module? drill module_systems
Back
You import a Haskell module using the `import` keyword, e.g., `import ModuleName`.
What is the purpose of the `qualified` keyword when importing Haskell modules? drill module_systems
Back
The `qualified` keyword is used to import a module with a namespace, preventing name clashes, e.g., `import qualified Data.Map as Map`.
Scala Modules
What is the equivalent of Java packages in Scala? drill module_systems
Back
In Scala, packages serve a similar purpose to Java packages, organizing code into namespaces.
How do you create a singleton object in Scala? drill module_systems
Back
You create a singleton object in Scala using the `object` keyword instead of `class`, e.g., `object SingletonName { … }`.