0% found this document useful (0 votes)
42 views

5 Lessons I've Learned On How To Structure Code - by Daan - Level Up Coding

The document discusses 5 lessons for structuring code: 1) Design before coding using UML diagrams, 2) Establish coding conventions, 3) Keep classes/functions small and single purpose, 4) Use design patterns appropriately, and 5) Write unit tests to enforce structure. Structuring code properly improves readability, maintainability, and enables unit testing. Key aspects include planning, conventions, separation of concerns, and testing.

Uploaded by

Mario Colosso V.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

5 Lessons I've Learned On How To Structure Code - by Daan - Level Up Coding

The document discusses 5 lessons for structuring code: 1) Design before coding using UML diagrams, 2) Establish coding conventions, 3) Keep classes/functions small and single purpose, 4) Use design patterns appropriately, and 5) Write unit tests to enforce structure. Structuring code properly improves readability, maintainability, and enables unit testing. Key aspects include planning, conventions, separation of concerns, and testing.

Uploaded by

Mario Colosso V.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

5 Lessons I’ve Learned on How to Structure Code | by Daan | Level Up Coding 18/8/20 1:30 p. m.

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.

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

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

Spend some time designing your application, it will pay oO.

. . .

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.

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

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

. . .

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.

Lesson 3: Guidelines for Classes and Functions


Level Up Coding
In order to keep your classes and functions readable and maintainable,
Coding tutorialsthere
and are a few guidelines you could follow:
news.

Follow 1. Keep classes and functions small

2. Let classes and functions follow the Single Responsibility Principle


(SRP)
1.1K

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.

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.

. . .

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.

Thanks for reading!

Stay updated with brand new articles

Email

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.

Coding Interview Questions | Skilled.dev


A full platform where I teach you everything you need to land your next job
and the techniques to…

skilled.dev

Thanks to Zack Shapiro.

Programming Web Development Software Development JavaScript Technology

Discover Medium Make Medium yours Become a member


Welcome to a place where words matter. Follow all the topics you care about, and Get unlimited access to the best stories on
On Medium, smart voices and original ideas we’ll deliver the best stories for you to your Medium — and support writers while you’re
take center stage - with no ads in sight. homepage and inbox. Explore at it. Just $5/month. Upgrade
Watch

About Help Legal

https://levelup.gitconnected.com/5-lessons-ive-learned-on-how-to-structure-code-6d662df0fd1f Página 8 de 8

You might also like