You are on page 1of 11

5 Lessons I’ve Learned on How to Structure Co…

Guardado en Dropbox • 5 jul. 2020 5:36 p. m.

Open in app

You have 2 free member-only stories left this


month.
Start your free trial

Photo by Fabian Grohs on Unsplash

5 Lessons I’ve Learned on


How to Structure Code
Bene=t from this list to improve the structure
of your code

Daan
Jun 2 · 4 min read

Structuring code is a hard, yet crucial part


of coding. Writing well-structured code
takes proper thinking, understanding of
design patterns, and experience.
However, these lessons are often learned
the hard way.

The importance of structuring code


shouldn’t be underestimated —
structuring code is very important from a
readability and maintainability
standpoint.

Here’s how you could improve the


structure of your code.

. . .
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).

Spend some time designing your


application, it will pay oO.

. . .

Lesson 2: Code Conventions


A big part of structuring your code has to
do with code conventions. Code
conventions are a must-have for every
project. Without coding conventions, your
code will become an unreadable mess in a
matter of time.

Make a list of coding conventions where


you document how variables should be
declared and some naming conventions,
etc. The number of rules you can add to
this list is unlimited, and the number of
rules can vary. Just do what works for you
and your team. Feel free to add new rules
to the list of conventions if the team feels
like it. The same goes for removing a
convention from the list.

Once you’ve got that list, stick to it!

. . .

Lesson 3: Guidelines for Classes


and Functions
In order to keep your classes and functions
readable and maintainable, there are a
few guidelines you could follow:

1. Keep classes and functions small

2. Let classes and functions follow the


Single Responsibility Principle (SRP)

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.

. . .

Lesson 4: Use Design Patterns


Learning about design patterns and how
they work is a great way to help structure
your code and write readable and
maintainable code.

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.

Be careful to not overuse design patterns


— which is the most common pitfall when
it comes to using design patterns.
Although you could implement a design
pattern in a certain situation, it doesn’t
mean you should. This will work against
you and you’ll get an overengineered
application that’s hard to grasp for other
developers.

. . .

Lesson 5: Write Unit Tests


Writing unit tests has the great side eOect
that it forces you to structure your code. In
order to be able to write unit tests for your
code, the code should at least be
structured properly.

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.

. . .
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.

Thanks for reading!


. . .

Coding Interview
Questions |…
Skilled.dev
A full platform where I
teach you everythin…
you need to land your
skilled.dev
next job and the
techniques to…

Thanks to Zack Shapiro.

Programming Web Development

Software Development JavaScript

Technology
1.1K
claps

WRITTEN BY
Daan Follow

Backend developer from The Netherlands.


Crypto enthusiast.

Level Up Coding Follow

Coding tutorials and news. The developer


homepage gitconnected.com

See responses (3)

About Help Legal

Get the Medium app

You might also like