7 - Object-Oriented Programming and AI-Assisted Coding Workflow — Topics & Learning Outcomes
Module Topics
Foundations of Object-Oriented Programming
Introduces the fundamental concepts of OOP, explaining why it is a powerful paradigm for structuring engineering software. Covers the building blocks of classes, objects, attributes, and methods.
- What Is Object-Oriented Programming? — Object-oriented programming (OOP) is a programming paradigm that organizes software around objects rather than functions and logic alone. It allows engineers to model real-world entities in code in an intuitive and reusable way.
- Classes and Instances — A class is a blueprint that defines the structure and behavior of a type of object, while an instance is a specific object created from that blueprint. Understanding the distinction between a class definition and an instantiated object is foundational to OOP.
- Attributes — Attributes are variables that belong to an object and store its state or properties. In engineering contexts, attributes might represent physical properties such as a beam's length, material, or cross-sectional area.
- Methods — Methods are functions defined inside a class that describe the behaviors or operations an object can perform. They allow objects to act on their own data and interact with other objects.
- The Constructor Method __init__ — The __init__ method is a special method automatically called when a new instance is created. It initializes the object's attributes, ensuring every instance starts in a valid, well-defined state.
Modeling Engineering Objects with Classes
Demonstrates how OOP concepts are applied to represent real-world engineering entities such as structural beams and sensors as Python classes. Emphasizes practical class design for engineering problem-solving.
- Designing an Engineering Class — Good class design begins by identifying the properties and behaviors of the engineering object being modeled. A systematic design process leads to classes that are intuitive, reusable, and aligned with engineering domain knowledge.
- Implementing a Beam Class — A structural beam can be modeled as a Python class with attributes for geometry and material, and methods that compute engineering quantities such as moment of inertia or deflection. This example ties OOP syntax directly to familiar engineering concepts.
- Implementing a Sensor Class — Sensors in an engineering system can be modeled as objects with attributes describing their type, location, and measurement range, and methods for reading or validating data. Modeling sensors as classes supports scalable, maintainable monitoring system code.
- Using Objects Together in Engineering Systems — Real engineering programs often involve multiple interacting objects, such as sensors attached to beams or loads applied to structural members. Designing classes to work together models the relationships present in actual engineering systems.
- String Representation with __str__ and __repr__ — Implementing __str__ and __repr__ methods gives objects meaningful text representations, making debugging and logging significantly more informative. For engineering objects, these methods can display key attribute values in a readable format.
Inheritance and Class Hierarchies
Explains how inheritance allows new classes to build upon and extend existing ones, reducing duplication and reflecting natural hierarchies in engineering domains. Covers single inheritance, method overriding, and the use of super().
- The Concept of Inheritance — Inheritance allows a child class to automatically receive the attributes and methods of a parent class, then extend or modify them as needed. This mechanism captures the 'is-a' relationship between engineering object types.
- Defining Subclasses — A subclass is defined by passing the parent class as an argument in the class declaration. The subclass can introduce new attributes and methods while inheriting everything already defined in the parent.
- Calling the Parent Constructor with super() — The super() function allows a subclass to call the parent class's __init__ and other methods, ensuring parent initialization logic runs before the subclass adds its own setup. This prevents duplication and maintains proper object state.
- Method Overriding — Method overriding allows a subclass to provide its own implementation of a method already defined in the parent class. This is how different beam types can each calculate moment of inertia using geometry-specific formulas.
- When to Use Inheritance Versus Composition — Choosing between inheritance and composition is an important design decision. Inheritance suits 'is-a' relationships, while composition suits 'has-a' relationships, and selecting the right approach keeps code maintainable and logically sound.
- Practical Inheritance Example: Sensor Hierarchy — A sensor hierarchy illustrates inheritance in action: a base Sensor class defines common attributes and a read_value() method, while subclasses such as TemperatureSensor and StrainGaugeSensor override read_value() with type-specific logic.
Introduction to AI-Assisted Coding Tools
Introduces GitHub Copilot and ChatGPT as AI coding assistants and explains how they integrate into the engineering programming workflow. Establishes the capabilities and limitations of each tool.
- What Is GitHub Copilot? — GitHub Copilot is an AI-powered code completion tool integrated directly into code editors such as VS Code. It suggests code in real time as the programmer types, based on context from the current file and comments.
- What Is ChatGPT for Coding? — ChatGPT is a conversational AI that can generate code, explain concepts, suggest fixes, and answer programming questions in natural language. Unlike Copilot, it is used in a separate chat interface rather than within the editor.
- Capabilities and Limitations of AI Coding Assistants — AI coding assistants are powerful productivity tools but have well-documented limitations including generating plausible but incorrect code, using outdated library versions, and lacking domain-specific engineering judgment.
- Setting Up the AI-Assisted Coding Environment — Integrating AI tools into a development environment requires installing the appropriate extensions and configuring access credentials. A properly set-up environment allows seamless use of Copilot within the editor alongside ChatGPT in a browser or API.
AI-Assisted Code Generation Workflow
Establishes a structured workflow for using AI tools to generate, refine, and integrate code for engineering programming tasks. Focuses on effective prompting strategies and iterative development practices.
- Writing Effective Prompts for Code Generation — The quality of AI-generated code is directly tied to the quality of the prompt provided. Effective prompts are specific, include relevant context, state constraints, and describe expected inputs and outputs clearly.
- Using Comments and Docstrings to Guide Copilot — GitHub Copilot uses inline comments and docstrings as high-quality contextual signals to generate relevant code suggestions. Writing descriptive comments before a function or method body is one of the most effective Copilot usage patterns.
- Iterative Refinement of AI-Generated Code — AI-generated code rarely meets all requirements on the first attempt and should be treated as a draft to be iteratively refined. Reviewing, testing, and re-prompting are essential steps in the workflow.
- Generating Boilerplate and Class Scaffolding — AI tools are especially effective at generating repetitive boilerplate code such as class definitions, __init__ methods, property accessors, and __str__ implementations. This accelerates the setup phase of engineering class design.
- Using AI for Test Case Generation — AI tools can generate unit test cases for engineering classes, helping engineers verify that methods return correct results across a range of inputs. Prompt the tool to create tests covering typical values, edge cases, and boundary conditions.
AI-Assisted Debugging
Covers how to use AI tools to interpret error messages, identify bugs, and reason through incorrect program behavior. Establishes a systematic debugging approach that combines AI assistance with engineer-led analysis.
- Reading and Interpreting Error Messages — Python error messages contain precise information about the type of error, the file, the line number, and a description of the problem. Learning to read tracebacks systematically is a prerequisite for effective debugging with or without AI assistance.
- Prompting AI Tools to Explain Errors — Pasting an error message and the relevant code into ChatGPT and asking for an explanation is an effective first debugging step. AI tools can quickly identify common error patterns and suggest likely causes.
- Common OOP Errors in Engineering Code — Certain error types arise frequently when working with classes, including AttributeError from misspelled attribute names, TypeError from incorrect method signatures, and errors from failing to call super() in subclass constructors.
- Systematic Debugging Strategy — Effective debugging follows a systematic process: reproduce the error reliably, isolate the failing component, form a hypothesis about the cause, test the hypothesis, and apply and verify a fix. AI tools support but do not replace this process.
Responsible Use and Verification of AI-Generated Code
Establishes the ethical responsibilities and practical verification practices engineers must apply when using AI-generated code. Emphasizes that engineering judgment and accountability cannot be delegated to AI tools.
- Why Verification Is Non-Negotiable in Engineering — Engineering software underpins decisions about physical systems where errors can have safety, financial, or environmental consequences. AI-generated code must be rigorously verified because the tools have no awareness of engineering standards, safety factors, or physical constraints.
- Hand Calculation Verification — The most direct verification method is computing expected outputs by hand and comparing them against the program's results. Hand calculations catch numerical errors, wrong formulas, and incorrect unit handling in AI-generated engineering code.
- Code Review Practices for AI-Generated Code — Code review involves systematically reading through generated code line by line to understand what it does, checking for logical errors, inappropriate assumptions, and missing edge case handling. This is a critical quality gate before any code is integrated.
- Testing Strategies for Engineering Classes — Structured testing using Python's unittest or pytest frameworks provides systematic and repeatable verification of class behavior. Writing tests that cover typical values, boundary conditions, and known failure modes builds confidence in the code.
- Academic and Professional Integrity in AI-Assisted Coding — Using AI tools responsibly includes understanding and following institutional policies on academic integrity and professional standards on attribution. Representing AI-generated code as entirely one's own original work without disclosure may violate ethical obligations.
- Building a Responsible AI-Assisted Workflow Summary — A responsible AI-assisted engineering coding workflow integrates AI tools as productivity aids within a framework of human judgment, systematic verification, and professional accountability. The engineer remains the decision-maker throughout.
Student Learning Outcomes
By the end of this module, students will be able to:
MO1
Construct Python classes that model engineering objects — such as structural beams or sensors — by defining attributes, methods, and a constructor using the __init__ method
Level: ApplyType: CognitiveCourse mapping: CO1
MO2
Implement class hierarchies using inheritance, method overriding, and super() to represent 'is-a' relationships among engineering object types
Level: ApplyType: CognitiveCourse mapping: CO1
MO3
Formulate effective prompts for AI coding assistants to generate class scaffolding and test cases for engineering programming tasks
Level: ApplyType: BehavioralCourse mapping: CO2
MO4
Evaluate AI-generated engineering code by comparing program outputs against hand calculations and applying structured code review to identify logical errors or invalid assumptions
Level: EvaluateType: CognitiveCourse mapping: CO3
MO5
Apply a systematic debugging strategy — including interpreting Python tracebacks and using AI tools to explain common OOP errors — to diagnose and correct faults in class-based engineering programs
Level: ApplyType: BehavioralCourse mapping: CO3
Course Outcomes (reference)
CO1Use variables, arrays and object structures appropriate to solve engineering problems.
CO2Write computer functions to meet specific calculation needs.
CO3Write computer programs to solve mathematical expressions and engineering problems