Understanding that DomainObjectCollection (and others) is a "live container" is helpful to understand the flow of Gradle

Today I finally realized why something about Gradle was somewhat confusing to me, and how to clarify it a bit in my mind. I just wanted to mention this, in case other people have had the same problem. If I’m stating this incorrectly, please correct me.

The thing is, generally when we as developers see code, we sort of expect it to execute linearly. When code is explicit about being asynchronous, we can also understand that.

What can be confusing is when code looks like it is specifying a linear process, but really describes something that happens asynchronously or dynamically. What helps is if we’re made aware of (at least) one level of context below what we see. For instance, it can sometimes make it easier to understand Java code when we learn about how bytecode works.

For an example in Gradle, if our projects use a plugin with an “configurations” block, the code in that block looks like it’s executed once, but it’s obviously executed for every configuration that gets created, even after this block of code is “loaded”. I think it’s hard for some people to wrap their head around that. I know it bothered me a bit, and I wasn’t even sure why.

What I think helps is to understand that the “configurations” list is a “live” collection, as it’s an implementation of the DomainObjectCollection interface. It’s sort of like saying this block defines a callback that is executed whenever you create a new configuration.

The FileCollection interface also has similar behavior.