reading-notes

reading notes for code fellows


Project maintained by dLeigh01 Hosted on GitHub Pages — Theme by mattgraham

Testing and Modules

Testing and recursion are going to be important matters to go over moving forward, and it’s good to having a grip on them ahead of time. Recursion can be kind of tricky, so going through it more carefully and taking notes is a good way to learn a bit more and have it be less daunting, while we haven’t generally written our own tests yet, so its good to see some of the reasoning behind them and a bit of the way to create them for the future.

TDD With Python

Unit tests are pieces of code designed to test the input, output and behavior or your code. Test Driven Development (TDD) is writing the tests before the code.

When writing tests, your test name should be as descriptive as possible (it is okay to have longer names for these) and the test file name should follow the module name, module.py vs test_module.py. I deally you would separate the production code from the tests in different folders.

Arrange Act and Assert (AAA) is a widely used convention for the structure of tests. You arrange the necessary data, have the program act on the code being tested, and assert that result is the expected output. The cycle of AAA is writing a unit test that fails since there is no code, writing the feature and making it pass, then refactoring the code.

If Name Equals Main

Before executing code, Python will run through the source file and define some special variables, one of which being __name__. If the source file is being run as the main program, then __name__ will be set to __main__, otherwise it will be set to whatever the name of the module is that it is being imported from. You can use this to set up blocks of code that will only run when it is being used as an import or not. We can set things to work when we know it is being used in a standalone format and plan accordingly.

Recursion

Recursion is the process in which a function calls itself directly or indirectly, and a function that invokes recursion is called a recursive function. A recursive function solves a problem by calling a cpoy of itself to solve smaller bits of the original problem and is very useful to reduce the length of code and make it easier to read and write. A problem where recursion will be helpful must perform the same operation multiple times with different inputs, have a smaller input in every step, and have a base condition to stop an infinite loop from occurring.

Recursion uses more memory than other methods, as it adds to the stack with each call and operates under LIFO in the same way as stack structures. Because of this, if there is not a defined base case or if it isn’t reached, it will cause a stack overflow.

The base case in recursion is what will be returned first when it hits the end of the calls.

If a function calls the exact same function, then it is known as a direct recursive, if it calls a new function and said function calls it back directly or indirectly, it is called an indirect recursive. If the recursive call is the last thing executed in the function, it is known as tailed recursion; if the opposite is true, then it is non-tailed recursion.

While both recursive and iterative programs have the same ability to solve problems, the recursive program has greater space and time requirements and more care needs to be taken when writing it since it can be more difficult to understand. On the other hand, recursion is clean and simple and some programs are inherently recursive.

Things I Want to Know More About

I think it will be interesting to get a bit more in depth with how useful it is to have certain things only run inside vs outside of modules and have some more insight into cases where that would be useful.

Discussion

We’ve already seen testing a few times before, between the tests run by netlify and the testing within data-structures-and-algorithms, and in fact having the tests written first and creating code to meet them halfway is exactly what we were doing in the code challenges, only we weren’t writing the tests. In recursion, it is a lot like many different math problems, such as the fibonacci sequence or factorials, and its very useful in that way, especially for problems that rely specifically on recursiveness inherently.

[< table of contents]

[< home]