ELI5 - "Handles" please

I have read a bunch online about what a handle is, but it makes no sense to me.
Currently, I am trying to figure out the jobs system, and schedule returns “JobHandle”

Why the term “handle”? I usually kind of understand why certain words are used in coding, but handle is making no sense to me.

A frying pan consists of two parts. The actual pan, and the handle. Why does it have a handle? Well because if you were to touch the actual pan part, you’d burn yourself.

In a similar fashion, an API might want to let you interact with an object, but it doesn’t want to give you the object directly (the pan) because that could be very dangerous. So it instead gives you a handle object which you can pass into APIs.

By this definition, the Entity datatype could also be considered a “handle”.

In this case, a JobHandle is a piece of metadata that I suspect can be interpreted to some node is the JobSystem’s DAG (not sure if that’s true as the JobSystem implementation is closed-source, but the analogy works).

4 Likes

Well, most words in programming don’t make sense to be honest. It’s just a mnemonic so when we talk about them, or talk to the computer, everyone knows what we’re talking about, a convention. Most of the time the reason why things are called that way is because 30 years ago someone called something vaguely similar (for example structs in c++ and structs in c#, which both hold values of various types, but apart from that have nothing else similar).

Likewise, the term “handle” is already commonly used, and is in use in some similar job/task-based systems. ScheduledJob could’ve been a better name, but it doesn’t make much sense as it’s not only one job but can be combined and stacked to mean “a whole bunch of jobs running in a sequence”. JobSequence, JobSchedule could’ve maybe been better names, maybe JobSchedule is just too confusing with the method job.Schedule();

Perhapse you are confused why a job returns a JobHandle? Note that a Job and JobHandle are 2 separate things:

  • Job is the definition of a job itself. You can assign values to it that will be passed to the job.
  • A JobHandle is a reference to the sequence of scheduled jobs

A Job does not run until it is scheduled. After a job runs, it needs to be completed so it’s resources can be used (this happens automatically with ECS system jobs). You can define a job and run it later.

// The job definition, assign values to it:
SomeJob job = new SomeJob { SomeValue = 1 };
// Now we can run it at some point:
JobHandle jobSchedule = job.Schedule();

// In practice however it's easier to define a job and schedule it in one line
JobHandle jobSchedule = new SomeJob { SomeValue = 1 }.Schedule();