2037 - 1002 - DOC - LAB - Exploring Advanced Features of AI Coding
2037 - 1002 - DOC - LAB - Exploring Advanced Features of AI Coding
Student Activity
Student Activity: Exploring Advanced Features of AI Coding
Assistants
Welcome to this hands-on activity session where you'll get to explore the advanced features of
AI coding assistants. This activity is designed for beginners and will guide you through practical
examples to help you understand and apply these features effectively. Let's dive in!
Python Code:
def greet(name):
return f"Hello, {name}!"
print(greet("World"))
Translate to JavaScript:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("World"));
Translate to C++:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
JavaScript Code:
function add(a, b) {
return a + b;
}
console.log(add(5, 3));
Translate to Python:
print(add(5, 3))
Activity: Use an AI coding assistant to translate these examples and verify the translations by
running the code in the respective languages.
2. Automatic Documentation Generation Using AI: Code
Comments and External Documentation
In this section, you'll learn how AI can help generate documentation for your code.
Python Code:
def calculate_area(radius):
# Calculate the area of a circle
return 3.14 * radius * radius
AI-Generated Comment:
def calculate_area(radius):
# Calculate the area of a circle using the formula: π * r^2
return 3.14 * radius * radius
JavaScript Code:
function multiply(a, b) {
return a * b;
}
AI-Generated Documentation:
Function: multiply
Description: Multiplies two numbers and returns the result.
Parameters:
- a: The first number.
- b: The second number.
Returns: The product of the two numbers.
Example 3: Python Function with Docstring
Python Code:
def greet_user(username):
return f"Welcome, {username}!"
AI-Generated Docstring:
def greet_user(username):
"""
Greets the user with a welcome message.
Parameters:
username (str): The name of the user.
Returns:
str: A welcome message for the user.
"""
return f"Welcome, {username}!"
Activity: Use an AI coding assistant to generate documentation for these examples and
compare it with the provided documentation.
Code Snippet:
AI Review Suggestion:
Check for division by zero to prevent runtime errors.
Example 2: Automated Testing
Code Snippet:
function isEven(num) {
return num % 2 === 0;
}
Code Snippet:
AI Optimization Suggestion:
Use enhanced for-loop for better readability: for (int number : numbers)
Activity: Use an AI coding assistant to review and test these examples, and implement any
suggested improvements.
Conclusion
By completing these activities, you have gained hands-on experience with the advanced
features of AI coding assistants. These tools can help you translate code, generate
documentation, and automate parts of the CI/CD process, making you a more efficient and
effective developer. Keep practicing and exploring these features to enhance your coding skills
further!