*********************** Coding Styles *********************** Python ********** Philosophy ----------- * Readable and Simple * Focuses on calirty and mininmal sytax * Uses include data science, AI and scripting backend Syntax -------- * Indentation defines code blocks * Python’s creator, Guido van Rossum, designed the language around the idea that “code should be as readable as plain English.” * Colon (:) signals and new block of indented code (sort of like a bracket) * Guido van Rossum worked on a teaching language called ABC at CWI (in the Netherlands) before inventing Python. ABC used colons to introduce blocks, just like Python. * No semicolons or braces/brackets * Naming: snake_case * # for comments .. code-block:: python for i in range(1,6): print(f"Number {i}") C++ ********** Philosophy ----------- * Power and Control * Lowlevel access * Manual memory management * Performance Focuses * Uses include systems, games, and high performace Software Syntax --------- * Braces define code blocks * Semicolons end statements * Type declaration required * Indentation optional but make it pretty so it is Readable * Naming: camelCase or PascalCase * // or /* for comments .. code-block:: c++ #include using namespace std; int main() { for (int i = 1; i <= 5; i++) { cout << "Number " << i << endl; } return 0; } Javascript ************* Philosophy ----------- * Dynamic and flexible * Designed for web interactivity * Loosely typed now supports modern modular patterns * Uses include web apps, front end and back end * Note that "ES6" is simply the modern version of javascript * ES6 added some features and changed some syntax * Now javascript is ES9 but everything from ES6 + is considered "modern javascript", before ES6 is considered an older langauge Syntax ----------- * Similar to c++ but more flexible * Template literals like Python f-strings * No type declarations * Semicolons to end statements are options but recomended * Indentation not required but make it pretty and uniform so it is readable * Naming: camelCase * // or /* for comments .. code-block:: javascript for (let i = 1; i <= 5; i++) { console.log(`Number ${i}`) } Object Declaration Comparison ********************************** .. list-table:: :header-rows: 1 :widths: 20 26 26 26 * - Feature - **Python** - **JavaScript** - **C++** * - Typing - Dynamic - Dynamic - Static * - Keyword - ``class`` - ``class`` / object literal ``{}`` - ``class`` / ``struct`` * - Constructors - ``__init__`` method - ``constructor()`` method - Class name as constructor * - Access Modifier Defaults - Public - Public - Private * - ``self`` / ``this`` - ``self`` explicitly passed - ``this`` implicitly available - ``this`` implicitly available * - Memory Management - Automatic (garbage collected) - Automatic (garbage collected) - Manual or via smart pointers * - Method Definition - Defined inside class - Defined inside class - Defined inside or outside class using ``::`` Python Example -------------- .. code-block:: python class Car: """A simple Car class.""" def __init__(self, make, model): self.make = make self.model = model def start(self): print(f"{self.make} {self.model} is starting.") # Object creation my_car = Car("Toyota", "Camry") my_car.start() JavaScript Example ------------------ .. code-block:: javascript /** * A simple Car class. */ class Car { constructor(make, model) { this.make = make; this.model = model; } start() { console.log(`${this.make} ${this.model} is starting.`); } } // Object creation const myCar = new Car("Toyota", "Camry"); myCar.start(); C++ Example ----------- .. code-block:: cpp #include #include using namespace std; /** * @brief A simple Car class. */ class Car { private: string make; string model; public: // Constructor Car(string m, string mod) { make = m; model = mod; } // Method void start() { cout << make << " " << model << " is starting." << endl; } }; // Object creation int main() { Car myCar("Toyota", "Camry"); myCar.start(); return 0; } Summary Table ------------- .. list-table:: :header-rows: 1 :widths: 20 26 26 26 * - Concept - **Python** - **JavaScript** - **C++** * - Define class - ``class ClassName:`` - ``class ClassName {}`` - ``class ClassName {}`` * - Constructor - ``def __init__(self, ...)`` - ``constructor(...)`` - ``ClassName(...) {}`` * - Create object - ``obj = ClassName()`` - ``const obj = new ClassName()`` - ``ClassName obj;`` * - Instance variable access - ``self.attr`` - ``this.attr`` - ``this->attr`` * - Access modifiers - Implicitly public - Implicitly public - Must specify (``public``, ``private``) * - Garbage collection - ✅ Yes - ✅ Yes - ❌ Manual / Smart pointers * - Documentation style - Docstring (``"""..."""``) - JSDoc (``/**...*/``) - Doxygen (``/**...*/``) Key Takeaways -------------- .. list-table:: :header-rows: 1 :widths: 20 80 * - Language - Object Philosophy * - **Python** - Objects are flexible, lightweight, and dynamically typed. Easy for beginners. * - **JavaScript** - Prototype-based; ES6 classes give a clean, modern OOP syntax. * - **C++** - Deep, type-safe OOP with control over memory and performance. Imports and Modules ******************************** .. list-table:: :widths: 20 25 25 25 :header-rows: 1 * - **Feature** - **Python** - **JavaScript (ES6)** - **C++** * - **Import keyword** - ``import module`` - ``import { name } from './file.js'`` - ``#include "file.h"`` * - **Export keyword** - Functions and classes automatically accessible from modules - ``export`` / ``export default`` - Not applicable — declarations in header files * - **Namespace** - Module namespace (each file acts as a module) - File or module-based namespace - ``namespace myNamespace { ... }`` * - **Encapsulation** - Managed by module imports - Managed by module exports - Controlled by header / source file separation * - **Dependency Management** - Handled via ``pip`` and virtual environments - Handled via npm or yarn - Handled manually or via build systems (CMake, Makefiles) ------------------------------- Memory and Resource Management ************************************************ .. list-table:: :widths: 20 25 25 25 :header-rows: 1 * - **Aspect** - **Python** - **JavaScript** - **C++** * - **Memory Management** - Automatic garbage collection using reference counting - Automatic garbage collection - Manual control (stack and heap allocation) * - **Object Lifetime** - Managed by interpreter and reference count - Managed by garbage collector - Determined by scope and destructors * - **Garbage Collection** - Yes (reference counting + cyclic GC) - Yes (mark-and-sweep GC) - No (must free manually or use smart pointers) * - **Pointers** - Not exposed to programmer - Not used - Directly used for memory access and manipulation * - **Resource Cleanup** - Automatic when objects go out of scope - Automatic by GC (not deterministic) - Controlled manually or via RAII (Resource Acquisition Is Initialization) * - **Common Issues** - Reference cycles can delay collection - Unpredictable GC timing - Memory leaks, dangling pointers if misused Who Wins? *********** * Readability belongs all to python * It basically reads as english and is short and sweet * Flexibility goes to Javascript * The syntax is flexible meaning there are many ways to achieve the same outcome * Semicolons are recomended but not required * Dynamic variable declaration with let * Performance and Control belongs to c++ * You can handle the memory yourself * It is extremly fast which is why it used for games and high end Software * It is so complex so that the coder can have more control over the program