23 lines
1.2 KiB
Markdown
23 lines
1.2 KiB
Markdown
---
|
|
title: Avoiding nesting
|
|
date: 2024-04-07
|
|
permalink: daily/2024/04/07/avoiding-nesting
|
|
tags:
|
|
- software-development
|
|
- clean-code
|
|
cta: ~
|
|
snippet: |
|
|
How many levels of nesting do you have in your code? It's something I try to keep to a minimum.
|
|
---
|
|
One of my goals when coding is to reduce the amount of nesting in the code I write.
|
|
|
|
I mean both in my PHP code where if conditions and foreach loops can be nested within each other, and CSS and Sass files, which support nesting CSS rules.
|
|
|
|
My aim is to have a maximum of two or three levels of indentation, though sometimes this isn't possible.
|
|
|
|
Doing so where I can, though, makes my code easier to read and understand and encourages other clean code approaches, such as having small and well-named functions.
|
|
|
|
In CSS or Sass, avoiding nesting makes it easier to find a rule I'm looking for instead of having to find how rules have been nested or names have been concatenated - making it hard to search or grep for a string.
|
|
|
|
This approach is part of "object callisthenics", which was introduced by Jeff Bay and includes other approaches that I like to follow, such as not using the `else` keyword and other good practices that I like to try and implement when possible.--
|