Skip to content

Commit

Permalink
merge lesson 2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
gcroci2 committed Oct 26, 2024
1 parent bf95f7d commit 8a92c23
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 253 deletions.
3 changes: 1 addition & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ contact: '[email protected]' # FIXME
episodes:
- 1-introduction-ai-coding.md
- 2-code-generation-optimization.md
- 3-enhancing-coding-efficiency.md
- 4-ethical-and-security-considerations.md
- 3-ethical-and-security-considerations.md

# Information for Learners
learners:
Expand Down
228 changes: 227 additions & 1 deletion episodes/2-code-generation-optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ exercises: 20
- How to refactor and improve the structure of existing code using Codeium as AI-assistant?
- What are the best ways to use Codeium for identifying and fixing bugs in a code?
- How does Codeium’s context-awareness improve code suggestions during optimization and refactoring?
- How can the Autocomplete function in Codeium help improve your coding speed?
- What types of repetitive coding tasks can be automated?
- What are some key components of a clear and effective prompt when using the Autocomplete function?
- In what ways can Codeium assist in generating and improving code documentation?

::::::::::::::::::::::::::::::::::::::::::::::::

Expand All @@ -21,6 +25,10 @@ exercises: 20
- Refactor and improve the structure and quality of existing code using Codeium as AI-assistant
- Identify and fix bugs using Codeium as AI-assistant
- Learn about context-awareness in AI-assisted coding
- Use an Autocomplete function of Codeium to improve coding speed
- Automate repetitive coding tasks using the Autocomplete function of Codeium
- Compose clear and effective prompts that capture and facilitate the expected outcomes using the Autocomplete function
- Generate and improve documentation using Codeium

::::::::::::::::::::::::::::::::::::::::::::::::

Expand All @@ -46,10 +54,84 @@ Codeium’s Command feature enhances your coding workflow by offering specific f

- **Refactor Functionality**: Clicking the Refactor label triggers Codeium’s refactoring capabilities, providing a dropdown of pre-filled options or allowing you to write a custom instruction. This is particularly useful for improving performance or restructuring code. You can also highlight a block of code and use Command (`⌘+I` or `Ctrl+I`) to perform more targeted refactoring.

- **Docstring Generation**: For generating documentation, clicking the Docstring label automatically creates a docstring above your function header (or under the function header in Python). This AI-generated documentation will describe what the function does, helping you maintain well-documented, readable code.
- **Docstring Generation**: For generating documentation, clicking the Docstring label automatically creates a docstring above your function header (or under the function header in Python). This AI-generated documentation will describe what the function does, helping you maintain well-documented, readable code. In Python for example, the docstring will be correctly placed directly beneath the function header.

![](episodes/fig/command.webp){alt='Command'}

![](episodes/fig/jetbrains_docstrings.gif){alt='Docstring generation'}

::::::::::::::::::::::::::::::::::::: challenge

## Generate Docstrings (5 min)

Modify the `analyze_co2_trends()` function you created earlier to add a detailed docstring using Codeium's `Docstring` lens. The docstring should:

- Describe the purpose of the function.
- Document the function’s arguments and expected data types.
- Explain what the function returns.
- Optionally, provide a usage example.

**Note**: Although you could manually write the docstring, this task is meant to demonstrate the `Docstring` functionality in Codeium, so ensure that you use it to streamline the documentation process. Although using Python is not mandatory, the solution will be provided in Python.

1. How did Codeium’s Docstring feature improve the documentation process?
2. In what situations might you need to adjust the generated docstring manually?
3. What are the benefits of keeping well-documented functions in your codebase?

::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::: solution

## Solution

Here’s an example of how the `analyze_co2_trends()` function might look with a generated docstring:

```python
def analyze_co2_trends(df):
"""
Processes weekly CO2 data to calculate monthly average CO2 concentrations
and visualizes the trend over time.
Args:
df (DataFrame): A pandas DataFrame containing the CO2 data with a 'date' column
in 'yyyy/mm/dd' format and a 'CO2 ppm' column representing CO2 concentrations.
Returns:
DataFrame: A DataFrame containing the monthly average CO2 concentrations.
Example:
>>> df = pd.read_csv('weekly_in_situ_co2_mlo.csv')
>>> monthly_avg = analyze_co2_trends(df)
"""
# Convert 'date' column to datetime
df['date'] = pd.to_datetime(df['date'])

# Set 'date' as index
df.set_index('date', inplace=True)

# Drop rows with NaN values
df.dropna(inplace=True)

# Resample data to get monthly averages
monthly_avg = df.resample('M').mean()

# Plotting
plt.figure(figsize=(10, 5))
plt.plot(monthly_avg.index, monthly_avg['CO2 ppm'], marker='o')
plt.title('Monthly Average CO2 Concentrations')
plt.xlabel('Date')
plt.ylabel('CO2 Concentration (ppm)')
plt.grid()
plt.show()

return monthly_avg
```

1. Codeium’s `Docstring` feature improves the documentation process by quickly generating structured and consistent docstrings, saving time and effort.
2. You might need to adjust the generated docstring if the function has complex logic or if the generated docstring lacks specific details about edge cases or exceptions.
3. Well-documented functions improve code readability, make it easier for others (or yourself) to understand and use the function, and reduce onboarding time for new developers.

::::::::::::::::::::::::::::::::::::::::::::::::

### Best Practices ###

Here are a few things to remember when using Command function of Codeium:
Expand Down Expand Up @@ -441,6 +523,142 @@ After applying the corrections suggested by Codeium, your code should look somet
**Changes:**


::::::::::::::::::::::::::::::::::::::::::::::::

## Autocomplete Function

A key feature of Codeium is its Autocomplete function: with every keystroke, Codeium actively attempts to predict and complete what you're typing. By analyzing the current file, previous edits, and contextual snippets from your codebase, it offers relevant suggestions as "ghost text".

![](episodes/fig/acceleration.gif){alt='Autocomplete Function'}

This feature can be particularly useful when you're writing boilerplate code (which refers to repetitive code that often serves as a standard template), as it can save you time and reduce the likelihood of errors. By leveraging Codeium's Autocomplete function, you can speed up your coding process and focus on the more creative and challenging aspects of your work.

### Boilerplate, Formatting, and More

As already mentioned above, one of the most powerful ways to use Codeium is for automating repetitive coding tasks. Whether you’re writing boilerplate code, standard functions, or common design patterns, Codeium's AI assistant can help speed up the process and reduce the manual effort required.

By recognizing patterns in your codebase, Codeium can predict and suggest repetitive structures such as:

- **Boilerplate code**: Generate routine code structures like class definitions, function signatures, or common initialization blocks with minimal effort.
- **Repetitive functions**: Quickly replicate commonly used functions or methods that follow a similar pattern, reducing the need for retyping or copy-pasting.
- **Code formatting and styling**: Maintain consistency in your code format by allowing Codeium to suggest and automate repetitive formatting tasks, saving you from manual corrections.

For example, when writing a set of functions that handle similar operations, Codeium can recognize the pattern after a few examples and suggest the next method based on prior code. This not only speeds up your workflow but also minimizes the risk of errors from copying or manually creating repetitive code.

::::::::::::::::::::::::::::::::::::: callout

## Autocomplete Feature for Docstrings

Note that Codeium's Autocomplete feature may also suggest docstrings as you type, further assisting in creating well-documented functions without needing to manually write them. This can be particularly useful when you need to create specific wordings and have the freedom to customize documents interactively.

::::::::::::::::::::::::::::::::::::::::::::::::

### Fill-in-the-middle (FIM)

Codeium's Autocomplete model also includes Fill-in-the-Middle (FIM) capabilities. This is crucial because, more often than not, you're inserting code within an existing file, rather than just appending new lines. With FIM, Autocomplete analyzes the code both above and below the current line to provide more accurate suggestions.

Let's compare the FIM-enabled model to the standard model.

Codeium Autocomplete (with FIM):

![](episodes/fig/fim_12.png){alt='FIM'}

Competitor Autocomplete (without FIM):

![](episodes/fig/fim_13.png){alt='No FIM'}

Note that the non-FIM model suggests a generic docstring based on only the preceding line with the function signature. In contrast, the FIM model provides a more contextually relevant suggestion based on the entire function definition.

### Inline Comments

You can guide the Autocomplete feature by using comments within your code. Codeium interprets these comments and generates code suggestions to implement what the comment describes.

![](episodes/fig/minimize_boilerplate.gif){alt='Inline Comments'}

### Shortcuts

The following shortcuts can be used to speed up your workflow:

- `Tab` = accept suggestion
- `Esc` = clear suggestions

#### MacOS

- `Cmd` + `Left Arrow` = accept next word in suggestion
- `Option` + `]` = next suggestion

#### Windows/Linux

- `Ctrl` + `Left Allow` = accept next word in suggestion
- `Alt` + `]` = next suggestion


### Best Practices

- Avoid manually triggering Autocomplete; instead, let it naturally enhance your workflow. Writing prompts as comments is not recommended, but decorating your code with quality comments and information variable/function names yields the best results.
- To achieve the best results with Autocomplete, it's important to enhance your code with clear, *declarative* (not instructive) code, descriptive function names, and good comments with examples of the desired inputs and outputs. See the table below for examples:

![](episodes/fig/table_autocomplete.png){alt='Best Practices for Autocomplete'}

- If needed, you can temporarily snooze Autocomplete. This feature is available in VS Code version 1.8.66. Simply click the Codeium button in the bottom right to access it.

::::::::::::::::::::::::::::::::::::: challenge

## Autocomplete Exploration (10 min)

Familiarize yourself with Codeium's Autocomplete feature by practicing coding tasks. Create the function `analyze_co2_trends()` that processes the CO2 data in `weekly_in_situ_co2_mlo.csv` to calculate monthly average CO2 concentrations and visualizes the trend over time. The function should:

- Accept the dataframe as an argument.
- Convert the `date` column to a datetime format.
- Discard any rows with missing values.
- Resample the data to calculate monthly averages for CO2 concentrations.
- Visualize the trends over time in a well-formatted plot.
- Return the monthly averages.

**Note**: While this task can be solved without using Autocomplete, it’s important to use Codeium in this exercise to demonstrate how Autocomplete can assist with coding. Although using Python is not mandatory, the solution will be provided in Python.

1. What are the benefits of using Codeium’s Autocomplete feature in this function?
2. What are the potential drawbacks of relying on Autocomplete?
3. How can you improve the accuracy of suggestions using Autocomplete?

::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::: solution

## Solution

Here’s a sample outline of what the `analyze_co2_trends()` function might look like:

```python
def analyze_co2_trends(df):
# Convert 'date' column to datetime
df['date'] = pd.to_datetime(df['date'])

# Set 'date' as index
df.set_index('date', inplace=True)

# Drop rows with NaN values
df.dropna(inplace=True)

# Resample data to get monthly averages
monthly_avg = df.resample('M').mean()

# Plotting
plt.figure(figsize=(10, 5))
plt.plot(monthly_avg.index, monthly_avg['CO2 ppm'], marker='o')
plt.title('Monthly Average CO2 Concentrations')
plt.xlabel('Date')
plt.ylabel('CO2 Concentration (ppm)')
plt.grid()
plt.show()

return monthly_avg
```

1. Autocomplete saves time by suggesting common functions, reducing the need for memorization, preventing typos, and recognizing patterns to speed up repetitive tasks.
2. Be cautious! You might miss out on understanding the code if you accept suggestions blindly. Autocomplete can also suggest incorrect functions if the context is unclear.
3. Improve suggestion accuracy by writing clear comments and using meaningful variable/function names, which help Codeium understand the context better.

::::::::::::::::::::::::::::::::::::::::::::::::

## Context Awareness
Expand All @@ -467,4 +685,12 @@ For instance, if you're working on a function and ask Codeium to help refactor i

- With Context Awareness, Codeium retrieves relevant context from open files and your entire codebase, ensuring tailored, high-quality suggestions that align with your current coding task.

- Codeium's Autocomplete boosts coding speed by predicting and completing code based on context, past edits, and current files.

- Automating repetitive tasks like boilerplate code, repetitive functions, and formatting reduces errors and saves time.

- Fill-in-the-Middle (FIM) improves suggestions by analyzing the code above and below the insertion point.

- Docstring generation via code lenses automates the creation of accurate docstrings in the correct location.

::::::::::::::::::::::::::::::::::::::::::::::::
Loading

0 comments on commit 8a92c23

Please sign in to comment.