Coding Tips
Eight Puzzle Tips
Below are some guidelines to follow for the design, style, and development strategy of your project. Since we will be evaluating your project both for correctness and the quality of design and style, part of this score will be based on how well you followed the design principles summarized below.
-
Implement your functions/methods one by one–designing them on paper first, and then turning them into Python code. For each function, include a docstring that explains the data that the function accepts as parameters, what the function is “responsible for doing,” and what result it produces and returns. If a function is more than 10-12 lines of code, ask yourself if it makes sense to break parts of it into separate helper functions. (The answer may be no, but you should at least consider it.)
-
Use comments when there is something in your code that is not self-evident. You don’t need to have a comment for every line, but lines or blocks of code that do something that is not immediately obvious to the casual observer merit at least some documentation. You will find that it takes some experience to decide what is “not immediately obvious”.
-
Test each function/method as you write it. This is called unit testing. Make sure that each function behaves correctly before moving on to the next function. This will potentially save you much time when debugging.
-
Use descriptive function and variable names. A function name like
print_board
is more descriptive thanpb
. Similarly, a variable name likecolors
ornum_colors
is more descriptive thanc
orx
. -
Remove code duplication where possible. If you have the same code in more than one place, move that code into a function and call it from wherever you need it. Then, you have just one piece of code to test and one piece of code to change. This is known as procedural decomposition, and it is one of the most important software engineering principles.
-
When you are developing your program, make liberal use of print statements to help you catch errors. You should remove any debugging print statements before you submit your code.
-
Use the debugger! It will help you identify if your code does what you think it does.