VOOZH about

URL: https://thenewstack.io/5-clean-code-tips-for-reducing-cognitive-complexity/

⇱ 5 Clean Code Tips for Reducing Complexity - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-07-03 08:00:50
5 Clean Code Tips for Reducing Complexity
sponsor-sonarsource,sponsored-post-contributed,
CI/CD / DevOps

5 Clean Code Tips for Reducing Complexity

Reducing cognitive complexity is key to helping you write secure, maintainable and reliable code that makes developers, including yourself, happier.
Jul 3rd, 2024 8:00am by John Clifton
👁 Featued image for: 5 Clean Code Tips for Reducing Complexity
Sonar sponsored this post. Insight Partners is an investor in Sonar and TNS.

You probably understand the frustration that comes with trying to understand someone else’s old code — or even your own. Time away has messed with your memory, and now you’re losing the thread of the code.

Creating code you and others can actually understand is imperative. Reducing cognitive complexity is key to helping you write secure, maintainable and reliable clean code that makes other developers, including yourself, happier in the long run. Here’s how you can take a disciplined approach.

1. Write Code Your Team Will Love You For

Software development is very much a team sport. It’s critical to understand how the code you write will fit into an overall project and be understood by the other developers who need to be able to read it.

Cyclomatic complexity was first brought onto the scene as a way to gauge the ease of testing and maintaining a module’s control flow. This formula can help with assessing how much testing is needed based on counting the number of branches in the code. It won’t give a good view of how hard it will be for you or your teammates to understand and maintain the code in the future.

2. Life Isn’t All Full-Speed Ahead

Linear code is your friend. If all code was a chain of commands with one following the next — no looping or meandering — you’d have no problem keeping everything straight in your head. Adding loops and branches to your code makes it increasingly difficult to understand and work on.

The cognitive complexity of the code increases incrementally every time you do that. The problem is that developers need to be able to write code that loops and branches, including code that uses if/else statements, to create software. What’s key here is mindfulness. Know what you’re using and be clear about whether or not your code is doing too much. If it is, consider refactoring. Understanding the cognitive complexity of your code can help you determine when and where you need to simplify.

3. Nesting Can Create Chaos Quickly

Nested code, for instance, loops inside loops, is difficult to understand. And the deeper you nest code, the more effort it’s going to take to straighten things out in your head and understand every piece of code you’re dealing with.

Take a look at your code and see which nested components are causing the greatest headache. Then, find a different way to write the code. Understanding the cognitive complexity each component adds will help guide you down the right path.

4. Useful Things Don’t Increase Complexity

Plenty of constructs exist that make code clearer and easier to understand. Switch statements are a good way to help eliminate a series of nested if or if/else statements that make code murky and muddled, and they don’t add to the cognitive complexity of the code. Continue or break statements that help you jump out of a loop can also help with writing clearer code, and once again won’t increase the complexity. These are only a few of the different kinds of constructs that can help reduce cognitive complexity.

5. Use the Right Tools and Write Clean Code

Tools like SonarLint, SonarQube, and SonarCloud have built-in cognitive complexity measurement features that can help you write code that is written not just to run well, but also to be understood and built upon. They can help you take a better, closer look at your code so you can understand where you’re making things more complicated than they need to be. Focus on writing code that is easy to understand and your team and your future self will thank you!

Developers should always focus on writing clean code — code that is secure, reliable, readable and maintainable, saving all who use it major migraines in their software projects. With the right mindset and tools in place, you can ensure that your code is a help and not a hindrance to the quality of your software.

Sonar is the industry standard for code verification and automated code review, trusted by 75% of the Fortune 100. Its SonarQube platform analyzes over 750 billion lines of code daily, helping to prevent outages, reduce risk, lower technical debt, and ensure compliance.
Learn More
The latest from Sonar
Hear more from our sponsor
TRENDING STORIES
John Clifton is a product manager at Sonar. Holding two master's degrees in mathematics and business enabled John to take on a variety of roles in software engineering and people, project and product management prior to joining Sonar. With more...
Read more from John Clifton
Sonar sponsored this post. Insight Partners is an investor in Sonar and TNS.
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Sonar.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.