ENG 1002 Lecture Notes - Code Reuse, Code Smell, Perfect Set

5 views2 pages

Document Summary

Consider the following simple function that simulates the throw of a six-sided die. This code can be completed in one line. We can test functions using a loop which works well for numerical inputs. A for loop and an if statement which prints out a statement when the dice role is outside of the acceptable range is an even more though approach. We can make the previous function more general by throwing more dice. Which simply allows the sum of a variable number of dice to be determined. Testing in this case would actually be a range of possible values. Functions can even use a function to simply generate and random number which keeps code neat. Refactoring: is the replacement of repeated code with a function call. One advantage of replacing repeated code with a function call is that we can easily improve the code for the function we are calling.