3 Designing functions

 

This chapter covers

  • Functions in Python and their role in designing software
  • Reasonable tasks for Copilot to solve
  • The standard workflow when interacting with Copilot
  • Examples of writing good functions using Copilot

One of the hardest challenges for programming novices is to know what a reasonable task is to give to Copilot so that it finds a good solution. If you give it too big a task, it will often fail in spectacular ways that can be extremely difficult to fix. What, then, is a reasonable task?

This question is important for our use of Copilot but goes far beyond it. Human programmers struggle with complexity, too. If experienced software engineers try to write code to solve a problem that’s too complex without breaking it down into smaller, more solvable subparts, they often have trouble as well. The solution for humans has been to use something called a function, whose job is to perform one task. There are various rules of thumb for how to write a reasonable function in terms of number of lines of code, but fundamentally these rules try to capture writing something that (1) performs a single task and (2) is not so complex that it’s hard to get right.

3.1 Functions

3.1.1 The components of a function

3.1.2 Using a function

3.2 Benefits of functions

3.3 Roles of functions

3.4 What’s a reasonable task for a function?

3.4.1 Attributes of good functions

3.4.2 Examples of good (and bad) leaf functions

3.5 The cycle of design of functions with Copilot

3.6 Examples of creating good functions with Copilot

3.6.1 Dan’s stock pick

3.6.2 Leo’s password

3.6.3 Getting a strong password

3.6.4 Scrabble scoring

3.6.5 The best word

Summary