Writing Good Routines

The following list summarizes some of the rules you should follow to make routines as useful as possible:

  • Perform a single, well-defined task

  • Avoid side effects

  • Use descriptive names

  • Keep it short

  • Use comments

The following sections describe these concepts.

Perform a Single, Well-Defined Task

If a routine performs a single, well-defined task, it is easy for programmers to understand what it does and use it correctly. If a routine performs more than one task, programmers may be unable to perform one of the tasks without performing the other. They also may confuse the tasks and not use them properly.

If a piece of code performs multiple unrelated tasks, break it into separate routines, each of which will perform a single task. The code ...

Get Start Here!™ Fundamentals of Microsoft® .NET Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.