Professional Documents
Culture Documents
https://heartbeat.fritz.ai/how-great-data-scientists-can-stop-writing-bad-code-9b054eb62b75 1/13
09/09/2019 How great data scientists can stop writing bad code
https://xkcd.com/1513/
I’m a software engineer by trade. But I seem to keep dipping my toes into
the machine learning and data science communities. There are some
awesome libraries out there, but I admit I’ve had a difficult time wrapping
my head around many of them. Surprisingly, it’s not the math. It’s actually
the code itself that I find myself getting stuck on. It’s understandable.
Tensor algebra is hard enough without needing to worry about style guides
and code reviews.
Your code is the first impression others have of your hard work. A good first
impression will pique interest and encourage engagement with the tools you
build.
The other day I was reading some code from Apple that infers shapes from
Core ML neural network files. It’s great to see Apple getting their hands
dirty in the open source community. But, I got stuck trying to understand
this one function and have decided to document my processes of figuring it
out.
. . .
Can you read this and understand what’s happening? If so, you are smarter
than me. This code doesn’t really provide much context for anything. Here
were my thoughts reading through this function the first few times:
By the very end I was able to figure out what this function is trying to do:
compute and update the output shape of a crop layer. To solve the mystery, I
dusted off my deductive reasoning skills and put them to work.
I read code nearby to see how and where this function is used. I had to
search for similarly named variables in other code to give myself more
context to what they mean. Ultimately, I had to comprehend way more than
just this function to figure out these 12 lines of code. This experience is very
common. To understand even a small part of the pie, you need to know how
the whole pie was baked.
So, what does this code really do? This wonderful snippet in the Core ML
documentation gives us a clue:
https://heartbeat.fritz.ai/how-great-data-scientists-can-stop-writing-bad-code-9b054eb62b75 5/13
09/09/2019 How great data scientists can stop writing bad code
Aha! There are two functional modes with different behaviors for each
mode. The _crop function is beginning to come into clearer view. Let’s
rewrite the function in a way that is much more declarative about the world
it lives in:
Reading this, hopefully the intent of the function is obvious. It’s definitely
not perfect, and we can spend hours going back and forth on its
https://heartbeat.fritz.ai/how-great-data-scientists-can-stop-writing-bad-code-9b054eb62b75 7/13
09/09/2019 How great data scientists can stop writing bad code
. . .
https://heartbeat.fritz.ai/how-great-data-scientists-can-stop-writing-bad-code-9b054eb62b75 8/13
09/09/2019 How great data scientists can stop writing bad code
Variable names should state what they represent, not how or why. Single
letter names can not possible convey what they represent.
https://heartbeat.fritz.ai/how-great-data-scientists-can-stop-writing-bad-code-9b054eb62b75 9/13
09/09/2019 How great data scientists can stop writing bad code
Name your function in a way that conveys what it does without having to the
read its contents.
https://heartbeat.fritz.ai/how-great-data-scientists-can-stop-writing-bad-code-9b054eb62b75 10/13
09/09/2019 How great data scientists can stop writing bad code
Readable code makes bugs easier to spot. Readable code makes it easier for
others to get involved. Readable code lets us spend our precious time
solving interesting problems.
The code we are writing is ultimately not for ourselves. It is used and improved
by many other people to solve problems in new ways.
As the data science community grows and machine learning is used more to
create fresh experiences, comprehensible code is necessary to increase the
velocity of the community.
https://heartbeat.fritz.ai/how-great-data-scientists-can-stop-writing-bad-code-9b054eb62b75 11/13
09/09/2019 How great data scientists can stop writing bad code
. . .
Editor’s Note: Join Heartbeat on Slack and follow us on Twitter and LinkedIn
for the all the latest content, news, and more in machine learning, mobile
development, and where the two intersect.
Sign up
I agree to leave Heartbeat.fritz.ai and submit this informa on, which will be
collected and used according to Upscribe's privacy policy.
https://heartbeat.fritz.ai/how-great-data-scientists-can-stop-writing-bad-code-9b054eb62b75 12/13
09/09/2019 How great data scientists can stop writing bad code
Machine Learning Arti cial Intelligence Data Science Heartbeat Data Science For Ml
https://heartbeat.fritz.ai/how-great-data-scientists-can-stop-writing-bad-code-9b054eb62b75 13/13