Published originally on Medium
While helping and coaching aspiring developers during FreeCodeCamp meetups or Facebook Groups I picked up on a recurring question coming from the community;
“How can I improve my logical thinking?
After facing this question regularly I took my time to sum up my personal view on this in the following article. The contents if this article give advice on how to improve it but not all suggestions are relevant from day one when you get into programming. Some will be more applicable as you have gained more experience as a developer as you need a certain level of “comfort” with programming and solving problems.
# First things first
Some people have more affinity than others when it comes to critical thinking, but don’t let discourage you to learn it. Even for those that logical thinking comes more natural you need to keep improving. It’s a continuous journey, you get better over time, and you won’t become good at it over night. Persistence is key, it’s basically brain training, and it’s good to keep the brain sharp.
You don’t need a strong logical mindset when you start coding, but as you gain more experience and get better at it, it will help you to create better code and solve complex problems more elegantly.
As you might be coding from a day to day basis, your logical thinking will improve also, but giving it an extra push won’t hurt and is perfect for your personal development as a developer.
# So, what are the tricks?
Some people are more visual than others, but I believe that majority of developers benefit from visualising. There are various way to do this, like make an activity diagram, sequence diagram, or any other type UML diagram depending on what you are doing. There are many different diagram types out there, but especially for your own reasoning and understanding, don’t worry about the strict notation that comes with it.
When I’m sketching a diagram for myself, I don’t respect notation at all, it’s just a tool for myself to wrap my head around something. I might sketch a diagram from existing code, when I try to understand existing code, when I try to write new code I probably will start off with a diagram to reason about my problem and what a proper solution might be.
The benefit of first sketching your logic before actually writing it down, is that it is easier to reason and improve your logic even before writing the actual code. I’d encourage to just sketch your diagrams on a whiteboard or piece of paper, but if you want to go digital, check out the free service draw.io, but the “faster” the better. Don’t want to loose time on the perfect sketch.
We covered first the visualisation approach as this can be a very valuable tool aiding you in the upcoming topics.
It’s worth getting familiar code challenges, also known as “code katas”. They are great for brain exercising your logical mindset as they focus on a specific challenge that you should solve. There are several (social) platforms where you can find and create code challenges, some come with ranking and all that stuff. Often you can also review other people’s submissions, which is also great, as reading other’s code will help you improve you logical mindset.
Reviewing other’s submission gives almost the same benefit as pair programming where you basically benefit from observing how another person might solve the same challenge. I found a great article where they list some of these code challenge platforms.
Other sources for code challenges can be found if you google for coding interview questions from companies like Microsoft, Google and Amazon. Just try to solve them for yourself! Remember, visualise if you have problems understand the problem or someone’s solution.
Learn Algorithms And Data Structures
Rather than creating the solution to a problem, it’s helps to read and understand existing (battle) proven solutions where the pro’s and con’s are widely documented. When you search on the web or read books about algorithms and data structures, you will benefit from learning existing solutions and their complementary data structures. Like with code challenges, reading and understanding other people’s solutions will challenge you and advance your logical thinking.
To start, the sorting algorithms are the best to go with, my advice for getting the hang of a sorting algorithm is to use a deck of cards. Sorting algorithms are (mostly) about sorting a random set off numbers, just like a shuffled deck of cards. Use the deck of cards to mimic the sorting algorithm you try to understand. This will help to grasp the algorithm and to memorize it.
This falls rather under the same category as the visualise things tip. Clean Code itself does not necessarily translate in better logical thinking, but it does assist you on getting there. The idea here is, if you learn on making your code more elegant and clean to read, you will be able to understand the logic better. The easier your logic is to understand, the easier it is to find flaws in your logic or room for improvement.
When you have a method with 200 lines of codes and only variables named as single characters, you’ll have a hard time on reviewing your logic and find room for improving or find flaws.
The Clean Code name originates from Robin C. Martin’s book “Clean Code” so the book itself is a great resource, but as a starter, just googling “Clean Code” can return articles with good tips on writing cleaner code. When you’re up to it, consider reading the book.
We could argue that Design Patterns don’t immediately fit in this list but are worth the mention. Design patterns are basically suggested solutions to common everyday problems that arise in software development. These solutions are on a more abstract level than primary logical thinking, but they do assist in building a more, clear solution to a logical problem. Therefore, the mention of Design Patterns in this list.
The mainstream Design Patterns relate to Object Oriented programming and are not always applicable in all languages. To understand many of these patterns require some prior understand of Object Oriented design. For some languages you can create a derived version, for others they’re hard to implement, but yet, they can tune your logic mindset.
If you’re not working in an Object Oriented programming language, try to search for “Design patterns in programming language _” (fill in your language in the blank). Design Patterns do have a fair of criticism but it doesn’t hurt to read up on them so you can work on that logical mind of yours.
There are many resources like books and websites on these Design Patterns, one that I often use is SourceMaking.
We looked at some methods on improving your logical mindset, but also some tools that help you to understand and develop a better logic. The key is persistence. Read and understand existing solutions and try solving challenges that do the trick. This is a continuous process, you never suddenly “have a logical mindset”. Brain exercise yourself with logical challenges and you’ll only get better at it.