GitHub Copilot Certification
GitHub Copilot Basics
Demo Inline Chat
Explore how GitHub Copilot’s inline chat accelerates code explanation, refactoring, generation, and testing directly within your editor—without context switching.
Note
Ensure you have the GitHub Copilot extension installed and enabled in your IDE to use inline chat.
Opening Inline Chat
Use one of the following methods to launch the inline chat panel:
Platform | Shortcut | Alternative |
---|---|---|
macOS | ⌘ + I | Right-click in editor → Copilot Editor Inline Chat |
Windows/Linux | Ctrl + I | Right-click in editor → Copilot Editor Inline Chat |
Explaining Code Inline
Highlight any function or block, type explain
(or /explain
), and Copilot injects an inline comment detailing its behavior. For example:
class Car:
def update(self, light_state, cars_ahead):
"""Update the car's position.
- Check if the car is approaching the intersection's stop line.
- Stop if the light is red or yellow and the car is not in the intersection.
- Maintain safe distance from the car ahead.
"""
move = True # assume car can move
# Determine when to stop at the light for northbound traffic
if self.direction == 'north':
front = self.rect.bottom
if front + CAR_SPEED >= STOP_LINE['north'] and light_state != 'green':
move = False
# Enforce safe gap from cars ahead
for other in cars_ahead:
if other.rect.top <= self.rect.bottom:
continue
if other.rect.top < self.rect.bottom + SAFE_GAP:
move = False
break
# Move the car if permitted
if move:
self.rect.y += CAR_SPEED
If you prefer the full chat experience, click View in chat to see this explanation in the traditional chat window.
Refactoring Hard-Coded Values
Use inline chat to replace literals with external configuration.
Original:
LIGHT_COLORS = {
'green': (0, 255, 0),
'yellow': (255, 255, 0),
'red': (255, 0, 0)
}
Refactored using JSON:
import json
def load_config(path):
with open(path) as f:
return json.load(f)
config = load_config('config.json')
LIGHT_COLORS = config['light_colors']
Toggle the “before” and “after” diff in inline chat to review changes.
Consolidating Configuration Constants
At the top of your file, organize all constants for clarity and easy maintenance:
import pygame
import json
# ---------------- Configuration Constants ----------------
WINDOW_WIDTH, WINDOW_HEIGHT = 800, 800
ROAD_WIDTH = 100
CENTER_X = WINDOW_WIDTH // 2
CENTER_Y = WINDOW_HEIGHT // 2
VERT_ROAD_RECT = pygame.Rect(CENTER_X - ROAD_WIDTH/2, 0, ROAD_WIDTH, WINDOW_HEIGHT)
HORZ_ROAD_RECT = pygame.Rect(0, CENTER_Y - ROAD_WIDTH/2, WINDOW_WIDTH, ROAD_WIDTH)
STOP_LINE = {
'north': CENTER_Y - ROAD_WIDTH/2,
'south': CENTER_Y + ROAD_WIDTH/2,
'east': CENTER_X + ROAD_WIDTH/2,
'west': CENTER_X - ROAD_WIDTH/2
}
CAR_SPEED = 3 # pixels per frame
CAR_SIZE = {'vertical': (20, 40), 'horizontal': (40, 20)}
SAFE_GAP = 10 # pixels between cars
SPAWN_INTERVAL = 60 # frames between spawns
LIGHT_OFFSET = 30
LIGHT_RADIUS = 8
Generating New Code with Inline Chat
Prompt Copilot to scaffold helper functions. For instance, generate get_light_state
logic:
def get_light_state(direction, cycle_timer):
"""
Determine traffic light state based on timer.
- NS group: 'north'/'south'
- EW group: 'east'/'west'
"""
t = cycle_timer % CYCLE_LENGTH
if direction in ('north', 'south'):
if t < NS_GREEN_DURATION:
return 'green'
if t < NS_GREEN_DURATION + NS_YELLOW_DURATION:
return 'yellow'
return 'red'
else:
if t < NS_GREEN_DURATION + NS_YELLOW_DURATION:
return 'red'
if t < CYCLE_LENGTH:
return 'green'
return 'yellow'
Creating Unit Tests with Pytest
Copilot can also generate test scaffolding. For example:
import pytest
from main2 import get_light_state, CYCLE_LENGTH, NS_GREEN_DURATION, NS_YELLOW_DURATION
@pytest.mark.parametrize("t", range(NS_GREEN_DURATION))
def test_ns_green_light(t):
assert get_light_state('north', t) == 'green'
assert get_light_state('south', t) == 'green'
@pytest.mark.parametrize("t", range(NS_GREEN_DURATION, NS_GREEN_DURATION + NS_YELLOW_DURATION))
def test_ns_yellow_light(t):
assert get_light_state('north', t) == 'yellow'
assert get_light_state('south', t) == 'yellow'
Warning
Always install and pin your test dependencies to avoid mismatched versions:
pip install pytest
References
Watch Video
Watch video content