Ground the teaching of style and code clarity through the experience of reading unfamiliar code to provide meaningful motivation for students.

  • Instead of giving a definition of elegant or clear code, help students refine their practice.
    • A definition of elegance or clarity won’t be as clear as concrete examples.
  • Use an iterative process to help students understand what it means to have readable code.
  • Emphasize that there is no such thing as universally readable code. Someone’s experiences determine what types of things may or may not be readable to them.
    • It is better to overshoot your point and tell students there is no such thing as readable code.
    • Even if students think they understand a concept like readability, overshooting your point helps students correct and refine their understanding resulting in code that is, ideally, readable to more people.
  • Strategies:
    • Regularly make mistakes while coding in front of the class that students can correct to scaffold their critical thinking.
    • Refactor or modify code as a class to demonstrate iterative refinements of clarity and elegance.
      • You can call out specific strategies that you used:
        • changing a variable’s name
        • cutting away unnecessary code
    • Contrast facial expressions when reading clear and unclear code.
      • If you give someone unclear code, they’ll likely furrow their brow.
      • If you give someone clear code, they’ll likely nod.
    • Create situations where students won’t understand each others’ code to discuss what makes it readable and discuss how important readable code is.
    • Have students rewrite code written by other students so that it is more clear to them.
    • Take advantage of heterogeneous classroom to contrast sophisticated, efficient, and readable code
      • Have students all write code.
        • You’ll likely get a wide variety in the code they write.
      • Have students share their code.
      • As the teacher, point out strengths and weaknesses of each piece of code to the class.

More about this tip

External Source

Interview with Gregory Nelson.