diff --git a/.gitignore b/.gitignore index bd87454..cb36dd8 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,5 @@ docs/ # translation temp files po/*~ +# Mac OS X Finder +.DS_Store diff --git a/episodes/Enhancing-coding-efficiency.md b/episodes/Enhancing-coding-efficiency.md index f2dbe45..02827a0 100644 --- a/episodes/Enhancing-coding-efficiency.md +++ b/episodes/Enhancing-coding-efficiency.md @@ -1,32 +1,250 @@ --- -title: "Enhancing coding efficiency" -teaching: 0 -exercises: 0 +title: "Enhancing Coding Efficiency" +teaching: 25 +exercises: 15 --- :::::::::::::::::::::::::::::::::::::: questions -TODO +- 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? :::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::: objectives -- Outline the main AI coding assistants -- Set up Codeium as coding assistant for the lesson +- 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 :::::::::::::::::::::::::::::::::::::::::::::::: -TODO +## 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. + +### 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 -Challenge example +## 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. + +:::::::::::::::::::::::::::::::::::::::::::::::: + +## Documentation Generation + +Codeium displays code lenses, which are small, clickable text labels, above functions and classes. These lenses allow you to easily invoke Codeium’s AI capabilities for the specific item they reference. + +In the [previous episode](add link), we explored the `Refactor` code lens. Now, let’s focus on the `Docstring` lens. + +When you click on `Docstring`, Codeium will automatically generate a docstring above the function header. In Python for example, the docstring will be correctly placed directly beneath the function header. + +![](episodes/fig/jetbrains_docstrings.gif){alt='Docstring generation'} + +::::::::::::::::::::::::::::::::::::: 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. + +:::::::::::::::::::::::::::::::::::::::::::::::: + +::::::::::::::::::::::::::::::::::::: 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. :::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::: keypoints -TODO +- 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. :::::::::::::::::::::::::::::::::::::::::::::::: diff --git a/episodes/fig/acceleration.gif b/episodes/fig/acceleration.gif new file mode 100644 index 0000000..63d59a0 Binary files /dev/null and b/episodes/fig/acceleration.gif differ diff --git a/episodes/fig/fim_12.png b/episodes/fig/fim_12.png new file mode 100644 index 0000000..c0473f2 Binary files /dev/null and b/episodes/fig/fim_12.png differ diff --git a/episodes/fig/fim_13.png b/episodes/fig/fim_13.png new file mode 100644 index 0000000..178c679 Binary files /dev/null and b/episodes/fig/fim_13.png differ diff --git a/episodes/fig/jetbrains_docstrings.gif b/episodes/fig/jetbrains_docstrings.gif new file mode 100644 index 0000000..e6a9a4d Binary files /dev/null and b/episodes/fig/jetbrains_docstrings.gif differ diff --git a/episodes/fig/minimize_boilerplate.gif b/episodes/fig/minimize_boilerplate.gif new file mode 100644 index 0000000..b22de9a Binary files /dev/null and b/episodes/fig/minimize_boilerplate.gif differ diff --git a/episodes/fig/table_autocomplete.png b/episodes/fig/table_autocomplete.png new file mode 100644 index 0000000..698f738 Binary files /dev/null and b/episodes/fig/table_autocomplete.png differ diff --git a/learners/reference.md b/learners/reference.md index ba26b9f..56aa287 100644 --- a/learners/reference.md +++ b/learners/reference.md @@ -4,5 +4,8 @@ title: 'Reference' ## Glossary -This is a placeholder file. Please add content here. +Fill-in-the-middle (FIM): FIM is an advanced autocomplete feature in Codeium that provides code suggestions based on the context around the cursor. Unlike traditional methods, FIM analyzes both preceding and following lines to generate accurate and relevant completions, enhancing efficiency when inserting code into existing structures. +## Acknowledgements + +This material incorporates information from the official Codeium website. For more details, please visit [Codeium Help Center](https://help.codeium.com/).