5 Lessons I've Learned On How To Structure Code - by Daan - Level Up Coding
5 Lessons I've Learned On How To Structure Code - by Daan - Level Up Coding
You have 1 free story left this month. Upgrade for unlimited access.
https://levelup.gitconnected.com/5-lessons-ive-learned-on-how-to-structure-code-6d662df0fd1f Página 1 de 8
5 Lessons I’ve Learned on How to Structure Code | by Daan | Level Up Coding 18/8/20 1:30 p. m.
Daan Follow
Jun 2 · 4 min read
https://levelup.gitconnected.com/5-lessons-ive-learned-on-how-to-structure-code-6d662df0fd1f Página 2 de 8
5 Lessons I’ve Learned on How to Structure Code | by Daan | Level Up Coding 18/8/20 1:30 p. m.
. . .
Lesson 1: Designing
Before you jump straight into coding it might be a good idea to give some
thought to how you’re going to design the application you’re about to build.
A great way you can do this is by using UML diagrams.
Having a plan before you start writing code keeps you focused. Thinking
about the structure of your code and creating some UML diagrams you Hnd
helpful will iron out the most obvious Iaws. On top of that, it can make you
more aware of the fact there’s a lot to think about before writing code.
The UML diagrams you’ve created allow you to not let your thoughts drift
away or add some unnecessary features you think might be useful in the
future.
Not spending time designing your application might give you a head start,
but will eventually come back to bite you. It will result in having to refactor
large parts of code which costs lots of time (and motivation).
. . .
https://levelup.gitconnected.com/5-lessons-ive-learned-on-how-to-structure-code-6d662df0fd1f Página 3 de 8
5 Lessons I’ve Learned on How to Structure Code | by Daan | Level Up Coding 18/8/20 1:30 p. m.
. . .
https://levelup.gitconnected.com/5-lessons-ive-learned-on-how-to-structure-code-6d662df0fd1f Página 4 de 8
5 Lessons I’ve Learned on How to Structure Code | by Daan | Level Up Coding 18/8/20 1:30 p. m.
3 Keeping classes and functions as small as possible helps to make code easier
to understand. As a rule of thumb split larger classes and function into
smaller specialized ones.
Follow the Single Responsibility Principle which means every class and
function should do one thing. And one thing only. This helps you keep your
functions and classes small. Keep this within reasonable limits. Too many
tiny classes are much worse than a few larger classes most of the time.
Big functions that fetch, handle and store data are a no go when following
the SRP. Alternatively, you have to split this function up into three smaller
ones: one for fetching, one for handling, and another one for storing the
data.
. . .
https://levelup.gitconnected.com/5-lessons-ive-learned-on-how-to-structure-code-6d662df0fd1f Página 5 de 8
5 Lessons I’ve Learned on How to Structure Code | by Daan | Level Up Coding 18/8/20 1:30 p. m.
Knowing what design patterns you could use in certain situations gives you
the advantage of not having to come up with a half-decent solution
yourself. By simply following the design principle your code will be in good
shape most certainly.
. . .
You’ve probably heard before about untestable code before — or you might
have written it yourself. If you don’t know how to write a unit test for a
piece of code it’s probably because it does way too many things or it’s
designed poorly.
Either way, it’s safe to say dealing with untestable code has one cause: badly
structured code. You’ll Hnd yourself refactoring most of the time whenever
you’re running into untestable code.
Unit tests can be used as a big stick to force you into structuring your code.
. . .
https://levelup.gitconnected.com/5-lessons-ive-learned-on-how-to-structure-code-6d662df0fd1f Página 6 de 8
5 Lessons I’ve Learned on How to Structure Code | by Daan | Level Up Coding 18/8/20 1:30 p. m.
Wrapping it up
There are a few things you could do in order to structure your code. It all
begins before you start typing your Hrst letter of code. Thinking about the
design of your application. Creating UML diagrams help you iron out the
most obvious Iaws.
Whenever you’re ready to code, make sure you have a list of code
conventions you stick to. This helps to make your code readable and
maintainable. Learning about design patterns and implementing them can
help you accomplish this. Keep classes and functions small and make them
do one thing only.
Last, but not least, start writing unit tests. Unit tests force you to write
structured code because if you don’t you’ll end up with a bunch of
untestable code.
Sign up
If you're ok with us sending you email updates, please tick the box.
Unsubscribe whenever you want!
. . .
https://levelup.gitconnected.com/5-lessons-ive-learned-on-how-to-structure-code-6d662df0fd1f Página 7 de 8
5 Lessons I’ve Learned on How to Structure Code | by Daan | Level Up Coding 18/8/20 1:30 p. m.
Level Up Coding
Thanks for being a part of our community! Subscribe to our YouTube
channel or join the Skilled.dev coding interview course.
skilled.dev
https://levelup.gitconnected.com/5-lessons-ive-learned-on-how-to-structure-code-6d662df0fd1f Página 8 de 8