From the course: Agile Software Development: Clean Coding Practices

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Making good use of loops

Making good use of loops

From the course: Agile Software Development: Clean Coding Practices

Start my 1-month free trial

Making good use of loops

- [Instructor] Some of the best advice I've ever heard about creating clean programs can be distilled as a short little rhyme, "Three or more? "Use a for!" Which is Charles Petzold's restating of Don Roberts's Rule of Three. The gist of the Rule of Three is that it's okay to tolerate repeating yourself once, but instead of repeating yourself again find a way to automate the problem. What does that mean? Well let's work through an example. Say you have a line of Python code that prints a blank line to the screen, and then you realize that you need another blank line. It's pretty easy to just duplicate that line so now the code looks like this. But what if you need to add a third one? Again, we could just copy and paste the line again, but there is a better way. Instead of that third copy, let's switch to using a for loop. It's now much easier to control the number of times the blank line is printed to the screen.…

Contents