Frameworks and libraries

H. S. Teoh hsteoh at qfbox.info
Thu Sep 14 17:32:19 UTC 2023


On Thu, Sep 14, 2023 at 04:28:24PM +0000, Adam D Ruppe via Digitalmars-d wrote:
> On Thursday, 14 September 2023 at 15:40:30 UTC, user548215 wrote:
[...]
> > based on the discussions I've read about the Garbage Collector in
> > the D programming language, I've decided to abstain from using that
> > feature.
> 
> This is a mistaken decision.

Yeah, in 90% of the use cases, the GC actually helps much more than it
hurts.  Two of the biggest advantages:

(1) It eliminates an entire class of bugs stemming from wrong management
of allocated memory: pointer bugs, buffer overflows, double frees,
memory leaks, all those delicious things that I spend hours, days,
sometimes even months to debug at my day job. Many of these bugs only
show up after the fact, in completely unrelated code that gives you no
clue as to where the real problem is.

(2) Your APIs become so much cleaner and more maintainable: instead of
spammy APIs like:

	OwnedPointer!MyReturnType myFunction(Allocator)(Allocator alloc,
		OwnedPointer!ArgTypeA arg1, ScopedPointer!ArgTypeB arg2,
		RCPointer!ArgTypeC arg3) { ... }

with the GC it becomes simply:

	MyReturnType myFunction(ArgTypeA arg1, ArgTypeB arg2, ArgTypeC
		arg3) { ... }

Your APIs also become so much more usable; instead of:

	auto arg1 = wrapInOwnedPointer(mydata1);
	auto arg2 = wrapInScopedPointer(mydata2);
	auto arg3 = wrapInRCPointer(mydata3);
	OwnedPointer!MyReturnType value = myFunction(allocator, arg1, arg2, arg3);
	doSomethingWithValue(value);
	if (!value.isNull) value.free();
	if (!arg1.isNull) arg1.free();
	if (!arg2.isNull) arg2.free();
	if (!arg3.isNull) arg3.decrement();

with the GC it becomes simply:

	MyReturnType value = myFunction(mydata1, mydata2, mydata3);


The result is that your code is easier to write, easier to debug, and
frees up your brain from having to constantly focus on memory management
paraphrenalia, and actually think about *making progress in your problem
domain* in the first place.

Manual memory management is error-prone, verbose, hard to read, harder
to understand, and not actually necessary 90% of the time.  Using it by
default is a net minus, especially when you don't know if it's actually
necessary.


T

-- 
We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare.  Now, thanks to the Internet, we know this is not true. -- Robert Wilensk


More information about the Digitalmars-d mailing list