Python might be the world’s most widely used programming language, but developers know how challenging it can be to maintain lengthy Python code.
In spite of being the primary programming language of over 8 million developers, Python has limitations. These deterrent qualities of Python are the main reason why it is still far behind in the race to be used as an enterprise-level language.
Writing complex databases and maintaining even a few thousand lines of code in Python can be a nightmare for enterprises unless adequate measures are taken.
There are many reasons for these limitations of Python, not the least of which is Python’s inbuilt dynamic nature of programming. When writing large programs in Python, it becomes excessively difficult for developers to find valid arguments for a function or the possible types of return values.
It makes understanding and readability one of the biggest hassles for developers. Since a significant number of developers and testers are involved in a large-scale project, it becomes an elementary quality for the code to be readable. And because large-scale projects are not the result of one developer, there must be some standards adopted at the enterprise level that keep a check on the code.
What makes Python challenging?
Python has been in the news recently for many reasons. Its valuable contribution to implementing machine learning models is undeniable. Apart from this, it is also quickly used for application development and data analytics.
In spite of these multitudinous characteristics of Python, it was the talk of the town because of its shortcomings. The cloud-based software platform Salesforce recently replaced Python in its analytics platform with Google’s Go.
While Google’s Go is relatively new to Python and designed for large-scale implementation, it’s readability is what catches attention.
Must Read: Can Nim be the Next Python?
Readability can also impact productivity severely in an organization. Since the lines of code only increase complexity by nature, it becomes challenging for developers to take cues from what’s happening at earlier stages in a program.
This factor directly impacts productivity. Employees thus spend a massive amount of time understanding the code that could have been otherwise utilized in building new modules and classes.
Another one of the shortcomings of Python is its dynamic nature. Unlike Google’s Go, which is a statically typed programming language, Python is more of a general-purpose language. It means that Python has design restrictions.
While other languages explicitly ask the developer to define return types along with other parameters when writing a function, Python doesn’t.
As a result, Python developers are familiar with more than a few errors that occur at runtime. On the other hand, applications built in Python require more rigorous testing.
Also Read: Performance Optimization Tips for Python
DropBox’s Approach to Large-Scale Development with Python
But, keeping these limitations aside, Python can be an excellent programming language for developers. Having said this, the cloud-based file hosting service Dropbox is a big fan of Python.
At Dropbox, it is easy to find developers who use Python for both backend services and desktop client applications. Let’s take a look at how Python’s dynamic nature couldn’t stop Dropbox from writing 4 million lines of code-
One of the benefits of using a statically typed programming language like Golang is that it reduces the chances of runtime errors and makes the code more readable. However, since Python is dynamically typed, there had to be another way to achieve successful implementation of large-scale projects. Dropbox used annotations for the task.
Hire Python developers to code right from the beginning. This helps define data types for different variables along with the respective types for function argument and return values. Although it requires some significant work from developers upfront, there are many benefits to it. For starters, it can be using a static type checker for the code.
Dropbox is one of the few companies in the world today that has adopted type checking at such a large scale for its projects. A type checker called mypy was used that helped stop a class of bugs that would otherwise have been difficult for developers and testers to spot.
Type checking is fundamental to large-scale code written by Python Programmers. While some developers enjoy Python’s dynamic typing, type checking prevents any errors from crawling into the program because of it.
A type checker like MyPy runs checks before the developer implements the code. This practice helps in verifying various operations, like whether the data being passed to and fro from various functions is of the correct type.
Python’s version 3.5 was the first to support Type Hints, that enabled developers to include annotations and be scrutinized by type checkers like MyPy.
Highlighting the use of type checkers from its experience, Dropbox mentioned that annotating the code makes it much easier for the developer to understand it. As a result, it drives productivity when building an extensive-scale application. Dropbox also regularly sent weekly reports to their teams.
These mentioned a check on the factors that their annotations were covering, along with suggestions to making them more valuable. Gradually, strictness for the code was also increased, and it resulted in the following:
Easier Refactoring
A type checker would instantly provide references to the code that needed to be changed. This made refactoring much more comfortable and saved time spent in analyzing deep stack traces.
Convenient Testing
Testing a broad-scale application takes more than a few seconds, sometimes even minutes. But mypy can quickly type-check millions of lines of code in a fraction of a second. For developers, it is a big win.
Verified Documentation
A type checker enforces a standard style of annotations. This provides more precise and verified documentation since the type checker allows the developer to enter the type and checks it with its implementation in the code.
Conclusion
It’s been a long journey for Dropbox to test things and find the best solutions to Python’s underlying large-scale hassles. Along the way, the company has standardized more than a couple of things that have resulted in better coding practices and a more natural understanding of the code.
These include a standardized ecosystem around type checking, IDE and editor support for type hints, multiple type checkers with different tradeoffs, and library support. However, new developers might find type-checking a lot more work than usual. But, with Dropbox’s experience, annotating and type checking might be the key to harnessing the benefits of Python.