Why I no longer use newspaper code structure - a podcast by Kent C. Dodds

from 2021-04-06T15:03:50

:: ::

Hey there friends. So a while ago, I wrote well actually several years ago.
I wrote a blog post called a newspaper code structure where I described theway that I like to structure my code to basically make it. I don't know
easier for me to parse as I come into a new file and the idea is thatanytime I have some sort of block of code that returns some value or
whether that be a function or a module where your exporting values andstuff.
I always,Like to do the the thing that it returns or the exports at thevery top of the file and the reason for this is in a newspaper article and
they make it they write it so that they add just kind of a high level firstand then they slowly fill in with details and the reason they do that is so
or at least maybe they used to do that this way is so that you could clipthe article at any point to size it for where it needs to be and it would
still be coherent to make sense.And in the same way I like to make my code so that it makes sense.Reading
from the top down and if you need additional details then you can dive intothe function there. And so what it ended up looking like is you'd have a
function and like one of the first couple lines would be a return statementand then all of the guts of the function would be in a other functions that
are below the return statement.This is actually like a thing that you can do. You can define functions
below a return statement and they will get hoisted the declaration and thedefinition will get hoisted above the return statement. So it technically
actually works. So I did.The first for a long time but I eventually stoppeddoing this and the reason is because I I was bothered that I couldn't put
the exports and the return statement at the very very first at the veryvery top because any time like so the the hoisting functionality I'm
talking about that works for function declarations, but it will not workfor arrow functions, it won't work for variable declarations.
So, I'd have to just put everything in enough function if I needed to use
any variables and so what ended up happening.Was I'd have a couple of
variables that I define at the top and then I never returned statement and
then I'd have all the other functions or in a module I'd have you have to
put all the import statements at the top.Well, you don't have to those get hoisted too but it just looks funny if
you don't. So I put all those at the top and then I have some variable
declarations and stuff and then my exports and so what I ended up with wasa return statement and the exports that were just like in the middle of all
the code.So now I just put everything at the bottom and it's easier to find. So,
that's that's why I made that change. This is an answer to somebody in
Discord. I hope you're having a great day. Bye.

Further episodes of 3 Minutes with Kent

Further podcasts by Kent C. Dodds

Website of Kent C. Dodds