Preliminary Software Engineering
Unit 1 - Programming Fundamentals
Unit 1 - Programming Fundamentals
  • 1 - Programming Fundamentals
    • Programming Fundamentals Content
  • 2 - Python
    • Expected Python Knowledge
    • GitHub
    • Learning Python
    • Data Structures and File Management
      • Data Structures
        • Lists
        • Arrays
          • Single and Multi-Dimensional Arrays
        • Lists vs Arrays
          • Activities
        • Tuples
        • Sets
        • Dictionaries
          • Activities
      • File Handling
        • Loops, Lists, Dictionaries
        • Activities
  • 3 - Theory Content
    • Theory Content Explained
      • NESA Directional Verbs
      • Responding to Directional Verbs
  • 4 - Software Development and Management
    • Approaches to Software Development
      • Waterfall Model
      • Agile Model
      • Summary: Waterfall vs Agile
      • Activities
    • Software Development Steps
      • Sample Exam Question
      • Requirements Definition
        • Understanding the Need
        • Key Questions to Ask
        • Examples
        • Activities
        • Sample Exam Question
      • Determining Specifications
        • Functional Specifications
        • Non-Functional Specifications
        • Activities
      • Design
        • Top-Down Design Approach
        • Bottom-Up Design Approach
      • Development
        • Optimising Code
      • Integration
        • Example: Payment Program
        • Activity
        • Application Programming Interface (API)
          • Example: OpenWeather API
          • Example: WeatherAPI
          • Activity: Prepare Spells
      • Testing and Debugging
        • Test Data
          • Activities
        • Testing the System
          • Activities
        • Debugging
          • Types of Errors
            • Activities
          • Python Debugger
            • Activities
          • VS Code Debugger
            • Activities
      • Installation
        • Activities
        • Sample Exam Question
      • Maintenance
  • Charts and Algorithms
    • Example: IPO Charts and Pseudocode
      • Activities
    • Algorithms, Flowcharts, Pseudocode
      • Pseudocode Activities
      • Flowchart Activities
      • Sample Exam Questions
    • Structure Charts
      • Activities
      • Sample Exam Questions
    • Data Flow Diagrams
      • Activities
    • Data Dictionaries
      • Activities
    • Decision Trees
      • Activities
      • Sample Exam Questions
    • Gantt Chart
    • Class Diagrams
      • Sample Exam Question
    • Storyboards
      • Sample Exam Question
  • Testing and Debugging
    • Test Data
      • Activities
    • Testing the System
      • Activities
    • Debugging
      • Types of Errors
        • Activities
      • Python Debugger
        • Activities
      • VS Code Debugger
        • Activities
  • Computational Thinking
    • Decomposition
    • Abstraction
    • Activities
  • Version Control
    • Git
    • GitHub
    • Activities
  • Number Systems
    • Binary Systems
    • Hexadecimal Numbers
    • Using Two's Complement
    • Activities
  • 5 - Assessment Task 1
    • Data Science Project
      • Before we Start
        • Setting up GitHub Repository
        • Setting Up Markdown Documentation
      • Examples of API Usage
        • Starter Code: NASA Scenario
        • Starter Code: Spell Book
        • Starter: Pokédex Explorer
        • Starter Code: Weather App
        • Example: OpenWeather API
        • Example: WeatherAPI
        • Example: Prepare Spells
    • Task Guide
      • Requirements Definition
      • Determining Specifications
        • Use Cases
      • Design
        • Gantt Chart
        • Structure Chart
        • Algorithms
        • Data Dictionary
      • Development
        • Comments vs DocStrings
        • UI - main.py
        • Create Python Module
          • Example: NASA Module
          • Example: WeatherFetch Module
          • Example: SpellBook Module
      • Integration
        • Example: Pokedex
      • Testing and Debugging
        • Commit Changes
      • Installation
      • Maintenance
    • Submitting Your Task
Powered by GitBook
On this page
  1. 5 - Assessment Task 1
  2. Task Guide
  3. Development

Comments vs DocStrings

Comments and docstrings are both ways to add explanatory text to your code, but they serve different purposes and have different formatting conventions. Here's a breakdown of their comparison and contrast:

Comments
  • Structure: Comments can be single-line (// in C++, # in Python) or multi-line (/* ... */ in C++) depending on the programming language. They are typically placed within the code block they are explaining.

  • Use: Comments are used to explain specific lines of code, algorithms, or logic choices made by the programmer. They can also be used for temporary notes or reminders to be removed later such as #TODO

DocStrings
  • Structure: Docstrings are multi-line strings (usually placed at the beginning of a class, function, or module) that follow specific formatting conventions depending on the language. They use special delimiters like triple quotes (""" ... """) in Python.

  • Use: Docstrings provide comprehensive explanations for classes, functions, or modules. They typically cover the purpose, usage, parameters, return values, and sometimes even examples. They are meant to be permanent documentation for anyone using the code.

We can also check what a function does, by asking it. This helps maintainability.

def my_function:
"""This is a docstring for MyClass"""
pass

# Print the docstring of MyClass

help(my_function)
print(my_function.__doc__)
Feature
Comments
Docstrings

Purpose

Explain specific code sections

Document classes, functions, or modules

Placement

Within the code block being explained

At the beginning of the definition

Formatting

Language-dependent (usually single or multi-line)

Language-specific formatting conventions (e.g., triple quotes in Python)

Permanence

Can be temporary (for notes/reminders)

Meant to be permanent documentation

Level of Detail

Brief explanations

Comprehensive descriptions


When Should I use a Comment or DocString?

  • Use comments to explain "why" you wrote a specific piece of code.

  • Use docstrings to document "what" a class, function, or module does and "how" to use it.

def create_historic_character(name, era):
    """Create a historical character with a name and era.

    Args:
        name (str): The character's name.
        era (str): The historical era.

    Returns:
        str: A formatted description of the character.
    """
    return f"{name} lived during the {era}."

# Example usage
print(create_historic_character("Leonardo da Vinci", "Renaissance"))

The docstring provides an explanation of the function and what it does:

  • Function Purpose: To create a historical character with a name and era.

  • Arguments: It defines a section titled "Args" that explains each argument taken by the function:

    • name: The name of the historical figure.

    • era: The historical era the figure belongs to.

  • Returns: Outlines the that a description of the character is returned from the function.

This docstring improves code readability and maintainability by providing clear explanations for anyone using the create_historic_character function.

PreviousDevelopmentNextUI - main.py

Last updated 3 months ago