Jump to ratings and reviews
Rate this book

Robust Python: Write Clean and Maintainable Code

Rate this book
Does it seem like your Python projects are getting bigger and bigger? Are you feeling the pain as your codebase expands and gets tougher to debug and maintain? Python is an easy language to learn and use, but that also means systems can quickly grow beyond comprehension. Thankfully, Python has features to help developers overcome maintainability woes. In this practical book, author Patrick Viafore shows you how to use Python's type system to the max. You'll look at user-defined types, such as classes and enums, and Python's type hinting system. You'll also learn how to make Python extensible and how to use a comprehensive testing strategy as a safety net. With these tips and techniques, you'll write clearer and more maintainable code.

378 pages, Paperback

Published August 17, 2021

69 people are currently reading
244 people want to read

About the author

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
53 (53%)
4 stars
30 (30%)
3 stars
13 (13%)
2 stars
2 (2%)
1 star
2 (2%)
Displaying 1 - 15 of 15 reviews
Profile Image for John.
317 reviews30 followers
March 19, 2023
There was a clamor at my workplace around Robust Python; it being the inaugural book of a developer bookclub and one of two books recommended by a leader as informing their work. It seemed impossible that a book with such an apparently arcane focus would live up to the hype, but I'm pleased to report this reputation is deserved.

The factor that the author would like our practices to be robust to is change or volatility, putting this book in the same category as Righting Software. Instead of architecture and project plans, it's the code itself which we're looking to make robust to change, through
* undertaking every form of declaring and checking intent to communicate effectively over time;
* allowing many patterns of extensibility including composition, protocols, plug-ins, and event-handling; and
* deploying a variety of testing and static analysis strategies to provide a safety net,

in all cases using tools to back up and extend the advantages of these choices.

What's revelatory about Robust Python is how it rethinks the basics. A lot of Python books make the assumption: hey, we're starting with object-oriented design, let's get started by defining some classes and figure out their relationships. Not so with Robust Python: it doesn't talk about classes until chapter 10, after having covered every simpler form of declaring meaning through basic type annotations, data structure selection, aliasing, defining existing types as different names, enumerations, data classes, etc. In this formulation, classes are reserved only for where invariants are maintained, which necessitate information hiding beyond mere packaging, radically reducing and clarifying their role. Later, the way it prefers protocols to inheritance under most conditions is another example of using modern features to reduce the heaviness of the past.

The end section on testing also hit solidly above its weight. Today, testing is not thought of as only a safety strategy, but also a design strategy. Writing tests are something we do first to say what our intent is and avoids over-complication by constraining our activities to the scope of those tests. In addition to covering testing and static analysis in themselves, this book also looks to the strategies of
* acceptance testing: writing additional tests at the level of the product in scenarios close to common language
* property testing: testing what properties should hold between a range of inputs and outputs, instead of a particular case
* mutation testing: testing to see that changes in code actually break tests, and thus that the coverage of tests are complete and the implementation is minimal
These three strategies allow us to state and assure even more of what we intend to mean.

In all of these, Robust Python gives a lot of thought to tooling to support the factors it introduces, which is entirely appropriate given how much thinking developers now apply to tooling. We're now in a different era of tooling: from thinking about the development environment with rich IDEs supporting refactoring, repository access, debugging, test suites, documentation generation, and the like, to where linters, checkers, peer review, acceptance testing, and the like, are also social affairs. The environments for configuring and running type-checkers, complexity analyzers, and all the rest are now arbitrated by the team instead of personal decisions. They are now also brought front-and-center into the design, rather than supported ambiently with editing tools.

The one area where I think the book missed on testing was to put testing at the end rather than have it all the way though. Yet, I can't begrudge even this choice, as this book doesn't proceed like "Architecture Patterns with Python" (https://www.goodreads.com/review/show...), which proceeds from a single system outward, but instead develops its ideas gradually from a naive position, which is the only way it can be sure to reach its goal of having rethought the basics.

Overall, I thought Robust Python would be about filling in the gaps, and would be a book to get to after having read many others. Now, I think it's an excellent place to start, perhaps the first book to read after having come to terms with the basic syntax and structure.
Profile Image for Ethan Swan.
64 reviews
December 23, 2021
An okay book, with some useful and some not-so-useful examples. Specifically, I felt a lot of the architectural patterns didn’t come across as very practical, which may be partly because they aren’t and partly because of the toy examples (always dealing with imaginary systems that make soups and sandwiches). However, the coverage of more advanced typing features (e.g. protocols, literals) and various approaches to testing (property-based, mutation) is quite helpful.
Profile Image for Matthew Balshaw.
155 reviews2 followers
December 6, 2022
The book is well-written and easy to read, with clear examples that helped me understand the concepts being discussed.

One of the things that I appreciated the most about this book is how it made it clear that using type hints is the way to go. The author provides a compelling case for why type hints are important and shows how to use them effectively in Python. I'd been using type hints a little bit before but now I'm convinced that they are worth it for larger projects.

Another thing that stood out to me about this book is that it introduced me to some new forms of testing. In addition to covering the basics of unit testing and integration testing, Viafore also discusses more advanced techniques like mutation testing. This was really interesting to me, and I appreciated how the book provided practical advice for how to implement these tests in Python by sharing some specific libraries.

Overall, I would highly recommend Robust Python to anyone who is looking to improve their Python skills. Whether you are a beginner or an experienced programmer, this book has some great tips.
Profile Image for Serhii Kushchenko.
107 reviews18 followers
October 22, 2022
This book is not just good but great. It is probably the best Python book I've read this year. I have recently re-read it again. It is the most reliable proof of its high usefulness. Even if you already have many years of experience in Python, you will still learn many practical things.

This text will serve you the best if you already have experience participating in big real-life projects. Probably you have already encountered problems caused by poor code quality. If yes, you will immediately agree that the book is worthwhile.

What I liked the most (probably your list will be different):
- Chapter on mypy customization.
- NewType annotation.
- Enum flag.
- The idea of using classes to control invariants.
- Information about protocols which are a relatively new feature of Python.

In the next edition, I suggest adding more details about the SOLID principles and design patterns. This information is in the book, but I would still add some more.
Profile Image for Necot.
18 reviews
December 13, 2023
The book contains some very valuable principles about code robustness and how they can be applied by Python developers.

Unfortunately not all chapters deliver as the author intended. In particular, I found that Part III could have used more examples to describe the various design choices presented by the author.

I found very useful the fact that the author lists several tools which can be used to make the code more robust, especially in Part IV, even though the list is by no means exhaustive.

Also Part I is a good read for developers who need an introduction to the type annotation system of Python, even though more advanced use of annotations would have added additional value to the book

Overall, it is a 7/10 and developers who read it will most likely find some useful information across the several chapters of the book.
Profile Image for Nickolai.
875 reviews8 followers
April 7, 2024
В целом, книга неплохая и содержит достаточно много полезной теоретической информации и интересных примеров. Недостатка, на мой взгляд, два. Во-первых, слишком затянутый и излишне подробный раздел про типизацию. И во-вторых, немного странная структура остальных разделов. Часто вызывало удивление, как автор от одной темы переходит в следующей главе к совершенно с ней не связанной другой теме, а потом через несколько глав снова приближается к первой. Такое мельтешение несколько напрягало.
1 review
May 27, 2024
This book is well-written and easy to read, featuring clear and concise examples. I've gained valuable strategies to strengthen my codebases, including broader considerations for types and type hints, as well as advanced testing techniques beyond pytest. As an academic researcher aiming to write sustainable and maintainable code for future researchers, this book has provided me with numerous methods to achieve this goal.
Profile Image for Raymond Lewis.
173 reviews
July 19, 2024
A robust book. I expected it to cover mostly typing, which it did, but it also covered testing strategies in surprising detail. I have to give this book the highest honor I have: it made me think differently. I will likely need to revisit the topics again before I can master them, but I will undoubtedly adopt many of the recommendations from this book.
3 reviews
May 23, 2023
best programming book I ever read, primarily because it talks about how to think when developing software, and how do different pieces relate to one another. It’s not about syntax or any library, but the mindset of how to approcah solid software development in Python, and I got a lot out of it
Profile Image for Suphan Fayong.
99 reviews1 follower
August 20, 2022
The book explains how to write more robust Python code using recent Python features and modern 3rd-party tools.

[I've finished only chapter 1 - 16.]
Profile Image for Mohammad Forouhesh.
14 reviews18 followers
February 8, 2023
If you lead a team of python developers, this might come in handy with the onboarding process, yet there are better alternatives out there such as fluent python.
Profile Image for AbduLlah.
17 reviews
October 5, 2024
Вообще не понравилась книга, никакой практической пользы, какие-то общие слова и тонны воды. Гораздо больше зашла книжка High Performance Python.
Profile Image for Steven.
44 reviews
April 11, 2022
Great overview of clean python

I’m in a bit of a weird bucket of developer: I’ve written a bunch of python, but only ever as side projects no-one ever viewed. I’ve written a lot of code, but not really read books on clean code (or, at least, started and never finished them). I really enjoyed this book as a result: short, clear, practical lessons, light on the theory, and aimed at making code better.

I’m not convinced that reading this e.g. gave me a coherent and complete philosophy around code that I expect some other books would, but the fact that I finished this one, and it’ll almost certainly make my python better, means it was more useful.
Profile Image for Raphael Villela.
3 reviews1 follower
December 9, 2020
Too early into the book, only 5 chapters produced, at this point, I can summarize the book as "use types", that said, it's an easy read.
Displaying 1 - 15 of 15 reviews

Can't find what you're looking for?

Get help and learn more about the design.