Hackernoon logoPython[3] is great for projects with large code base by@Alir3z4

Python[3] is great for projects with large code base

Alireza Savand Hacker Noon profile picture

Alireza Savand

Software Architect

People always suggest Java, C++, Go or any other kind of language that are statically typed over Python for projects with large code base due to their statically typed nature.

When the code grows or becomes old, no matter how much you apply clean code or readability principles, it becomes annoying and harder to understand when you’re going to modify or fix some bugs later, refactoring becomes risky, code review time increases and well, you might start develop a grudge against it. Modifying another lazy person code that doesn’t have any clue about clean code or documentation becomes insane, you might start developing the same grudge against humanity as well.

Python is great for fast development, taking an idea from zero to production but having a dynamic type nature has its own disadvantages as well.

Good news is, this is not true anymore.

Python 3.6 + Mypy comes with type checking now, you can check out PEP 526 and its previous PEPs regarding to type checking.

What is mypy?

Mypy is an optional static type checker for Python. You can add type hints to your Python programs using the standard for type annotations introduced in Python 3.5 (PEP 484), and use mypy to type check them statically. Find bugs in your programs without even running them!

PyCharm already supports type hinting and even type annotation syntax, we have project written in Python 3.5 on production with the code base fully written with these new feature, the code has amazing readabilities.

“It has made the code review and maintenance much much easier.”

When I say large code base, I’m mostly talking about projects with at least +100k lines.

Right now I’m working with multiple projects, each with at least around ~250k lines of code, and they’re growing in matter of code base.

I’m not saying it’s impossible, but it’s harder when types are not mentioned and hints are not there.

When I review code, I constantly find myself reading through the function body only to find out the arguments types and the return type of the function. It’s like constantly running the code in my brain to get a clear understanding of it.

I write DocString a lot, Sphinx style or Python style or any other kind of way to let my development tools such as PyCharm to understand the types.

For instance, do you know this fib method takes and return ?

def fib(n):
a, b = 0, 1
while a < n:
yield a
a, b = b, a+b

I’m sure you know, since you just read the body of function to figure that out, but with new features you don’t need to do that anymore:

def fib(n: int) -> Iterator[int]:
a, b = 0, 1
while a < n:
yield a
a, b = b, a+b

So much better and cleaner.

This is a very simple example to show case the problem and how it can be solved.

You can’t bring up this excuse anymore, to say Python is for small sized projects”, just because of dynamic typing nature.

Some PEPs to read:

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising &sponsorship opportunities.
To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.