I recently spoke with Kevin Karplus a professor at UCSC about what constitutes excellent code writing. What Karplus stated was that, “clean decomposition into specific subproblems” was essential for excellent code.
No-one has completely summed it up so perfectly for me before. It seems so clear. When you make an engine for a car for instance, you don’t want the fuel tank to double as a radiator. Everything has been atomized into its specific components. So when making a program why would you have a main function that attempts to do fifty things? By breaking everything into functions and modules where appropriate… one can atomize their program and write more efficiently. Whats really important is that this style of writing can create a a stream of clarity.
Below I have included some of the code I use in Kevins BME 205 class. This function employs clear breakdown of subproblems in the main loop, where the use of a generator function maintains clearly legible code.