10 Standards of coding and creating maintainable ML code

 

This chapter covers

  • Identifying ML code smells and how to correct them
  • Reducing code complexity in ML projects
  • Currying for cleaner and more understandable code
  • Applying proper exception handling in ML code bases
  • Understanding side effects and how they can create bugs
  • Simplifying nested logic to improve comprehension

In the preceding chapter, we covered the broad strokes of a code foundation. Focusing on breaking up complex structure by utilizing refactoring and basic software-engineering best practices was important to pave the way for further discussion of the more detailed aspects of software development for ML. Without laying the foundation of basic best-practices, the code architecture and design elements that follow simply don’t matter.

Early in anyone’s career in software development (ML or otherwise), the ability to identify potential issues with an implementation is effectively nonexistent. This is understandable, as the wisdom of knowing what works and what doesn’t comes directly from experience. Everyone who works in developing software eventually learns that just because you can do something doesn’t mean that you should do it in code. These lessons are typically gained by messing things up a great deal.

10.1 ML code smells

10.2 Naming, structure, and code architecture

10.2.1 Naming conventions and structure

10.2.2 Trying to be too clever

10.2.3 Code architecture

10.3 Tuple unpacking and maintainable alternatives

10.3.1 Tuple unpacking example

10.3.2 A solid alternative to tuple unpacking

10.4 Blind to issues: Eating exceptions and other bad practices

10.4.1 Try/catch with the precision of a shotgun

10.4.2 Exception handling with laser precision

10.4.3 Handling errors the right way

10.5 Use of global mutable objects