A Comprehensive Guide to Modular Programming across Languages
Table of Contents
1. Module Systems Across Languages
1.1. CommonJS
1.1.1. What is the primary use case for CommonJS modules? drill module_systems
1.1.1.1. Back
CommonJS modules are primarily used in Node.js for server-side JavaScript development.
1.1.2. How do you export a module in CommonJS? drill module_systems
1.1.2.1. Back
In CommonJS, you export modules using `module.exports` or `exports`, e.g., `module.exports = { functionName: function() { … } }`.
1.2. ES6 Modules
1.2.1. What is a key difference between ES6 modules and CommonJS modules? drill module_systems
1.2.1.1. Back
ES6 modules use `import` and `export` statements, while CommonJS uses `require()` and `module.exports`.
1.2.2. How do you import a specific function from an ES6 module? drill module_systems
1.2.2.1. Back
You can use named imports in ES6, e.g., `import { specificFunction } from './module.js'`.
1.3. Python Modules
1.3.1. What is the purpose of the `__init__.py` file in Python packages? drill module_systems
1.3.1.1. Back
The `__init__.py` file marks a directory as a Python package and can be used to execute package initialization code.
1.3.2. How do you import a specific function from a Python module? drill module_systems
1.3.2.1. Back
You can use the `from` keyword, e.g., `from module_name import specific_function`.
1.4. Java Modules
1.4.1. What Java version introduced the module system? drill module_systems
1.4.1.1. Back
The module system was introduced in Java 9.
1.4.2. What is the purpose of the `module-info.java` file? drill module_systems
1.4.2.1. Back
The `module-info.java` file defines a module's dependencies, exported packages, and services.
1.5. C++ Modules
1.5.1. What C++ standard introduced modules? drill module_systems
1.5.1.1. Back
C++20 introduced modules to the C++ language.
1.5.2. What are the main benefits of C++ modules over traditional header files? drill module_systems
1.5.2.1. Back
C++ modules offer faster compilation times, better encapsulation, and reduced header dependencies.
1.6. Ruby Modules
1.6.1. What is the primary purpose of modules in Ruby? drill module_systems
1.6.1.1. Back
Ruby modules are used for namespacing and mixins, allowing code to be shared across multiple classes.
1.6.2. How do you include a module in a Ruby class? drill module_systems
1.6.2.1. Back
You use the `include` keyword followed by the module name, e.g., `include ModuleName`.
1.7. Go Modules
1.7.1. What command initializes a new module in Go? drill module_systems
1.7.1.1. Back
The command `go mod init` initializes a new module in Go.
1.7.2. What file is created to manage dependencies in Go modules? drill module_systems
1.7.2.1. Back
The `go.mod` file is created to manage dependencies in Go modules.
1.8. Rust Modules
1.8.1. How do you declare a module in Rust? drill module_systems
1.8.1.1. Back
You declare a module in Rust using the `mod` keyword, e.g., `mod module_name;`.
1.8.2. What is the purpose of the `use` keyword in Rust? drill module_systems
1.8.2.1. Back
The `use` keyword in Rust is used to bring items from modules into scope, similar to `import` in other languages.
1.9. Swift Modules
1.9.1. What is the default access level for entities in Swift modules? drill module_systems
1.9.1.1. Back
The default access level for entities in Swift modules is `internal`.
1.9.2. How do you make a Swift class or function public? drill module_systems
1.9.2.1. Back
You add the `public` keyword before the class or function declaration, e.g., `public class ClassName { … }`.
1.10. PHP Autoloading
1.10.1. What is the purpose of autoloading in PHP? drill module_systems
1.10.1.1. Back
Autoloading in PHP automatically includes class files when they are used, eliminating the need for multiple `include` or `require` statements.
1.10.2. What PSR standard defines autoloading specifications in PHP? drill module_systems
1.10.2.1. Back
PSR-4 defines the autoloading specifications in PHP.
1.11. TypeScript Modules
1.11.1. How do TypeScript modules differ from ES6 modules? drill module_systems
1.11.1.1. Back
TypeScript modules are very similar to ES6 modules but add support for type information and additional features like `namespace`.
1.11.2. What is the purpose of the `export` keyword in TypeScript? drill module_systems
1.11.2.1. Back
The `export` keyword in TypeScript is used to make a declaration available for use in other modules.
1.12. Haskell Modules
1.12.1. How do you import a Haskell module? drill module_systems
1.12.1.1. Back
You import a Haskell module using the `import` keyword, e.g., `import ModuleName`.
1.12.2. What is the purpose of the `qualified` keyword when importing Haskell modules? drill module_systems
1.12.2.1. Back
The `qualified` keyword is used to import a module with a namespace, preventing name clashes, e.g., `import qualified Data.Map as Map`.
1.13. Scala Modules
1.13.1. What is the equivalent of Java packages in Scala? drill module_systems
1.13.1.1. Back
In Scala, packages serve a similar purpose to Java packages, organizing code into namespaces.
1.13.2. How do you create a singleton object in Scala? drill module_systems
1.13.2.1. Back
You create a singleton object in Scala using the `object` keyword instead of `class`, e.g., `object SingletonName { … }`.