The surprising thing you can do in the D programming language

Nesting makes it easier for collaborators to understand a codebase. Here is an example of nesting in D to create clean, reusable code.
62 readers like this.

Software development can be a very complex process. As the code you write increases in size and complexity, it is important to keep it readable and easy to understand. In open source software development, many people collaborate on code with several interconnected and, quite often, frequently moving parts. This can make it hard for potential contributors to understand the codebases.

The effort required to wrap your head around a codebase may have a direct impact on contributions, as a lot of people have a limited window for contributing, many doing it in their free time. This makes it essential for a codebase to be easy to understand for both seasoned and new contributors.

The problem of complexity

Consider the example of a function that implements an algorithm to bake bread called bakeBread. Baking bread involves several steps, so it is helpful to break the code into multiple helper functions:

string bakeBread() 
{ 
	mixIngredients();
	kneadDough();
	riseDough();
	punchDough();
	riseDough();
	bakeInOven();
}


void mixIngredients() { /* code here */ }

void kneadDough() { /* code here */ }

void riseDough() { /* code here */ }

void punchDough() { /* code here */ }

void bakeInOven() { /* code here */ }

As you can see from the sample code above, baking bread is quite a process, and implementing it in multiple functions breaks the problem down into easy-to-understand steps. However, one downside to this approach is there are several functions outside bakeBread that are used only within that function.

Nesting to the rescue

Nesting helps alleviate some of this complexity in terms of how to structure code. It enables all the functions above to be declared within the scope of bakeBread where they are used:

string bakeBread()
{
        void mixIngredients() { /* code here */ }
        void kneadDough() { /* code here */ }
        void riseDough() { /* code here */ }
        void punchDough() { /* code here */ }
        void bakeInOven() { /* code here */ }

        mixIngredients();
        kneadDough();
        riseDough();
        punchDough();
        riseDough();
        bakeInOven();
}

Nesting helps clean up the code by providing a form of encapsulation. The functions are defined where they are used without cluttering the global scope. This also prevents name collisions with other functions that use the same name without resorting to strange function names to differentiate them. Nesting also helps collaborators easily identify where functions are defined, how they are used, and how to make improvements quickly.

Powerful nesting in D

Nesting in D goes beyond functions. You can nest classes within classes, structs within structs, structs within classes, and classes within structs, and even import modules locally within functions or any other scope. This concept is dubbed "turtles all the way down."

If the bakeBread example (or any of its now-nested functions) needs to import an external module, it can be imported within its scope where it's being used:

string bakeBread() 
{ 
	import std.stdio: writeln;

	void mixIngredients() { 
		import std.array: split;

		// more code here
	}
	
	/* the rest of code which may include 
	* functions, classes or structs goes here
	*/
}

This makes it easy to keep track of where things are coming from and to prevent name clashes with symbols (functions, classes, structs, etc.) from different modules with the same name. A function implemented like this can be moved around during refactoring without breaking code because it does not depend on any globally imported module. This minimizes the amount of work required to make major changes to a large codebase—which is a good thing for collaborative development.

Time to try D

D is a great language for development. It's easy to install a D compiler, write some example code, and run it to experience D for yourself. You can also run your code in the online D editor. The book Programming in D by Ali Çehreli provides a great introduction to D and offers a free online version.

What to read next
User profile image.
Lawrence is a freelance full stack engineer (React, Node.Js, D) and a 100% Linux user. He loves to design and code. Passionate about SaaS and the modern Web.

1 Comment

Hi Lawrence,

Nesting or other in-code solutions have one disadvantage. The fuction cannot be re-used directly.

Creating a library does.

For baking a cake you need the same functions.

Regards,

Jan Gerrit

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.