3 Designing functions

 

This chapter covers

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

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 Copilot too big of 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. Functions help us organize our code and make it easier to think about and fix any bugs. 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) isn’t 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 The design cycle of functions with Copilot

3.3 Examples of creating good functions with Copilot

3.3.1 Dan’s stock pick

3.3.2 Leo’s password

3.3.3 Getting a strong password

3.3.4 Scrabble scoring

3.3.5 The best word

3.4 Benefits of functions

3.5 Roles of functions

3.6 What’s a reasonable task for a function?

3.6.1 Attributes of good functions

3.6.2 Examples of good (and bad) leaf functions

3.7 Exercises

Summary