What would be the "identity" of an object?

Asked

Viewed 482 times

12

Reading more about objects, trying to "detach" me from the concept that object would only be provided with a "class", in this answer, I saw the following sentence:

"Objects have identity. A variable does not."

What would be the identity of an object? Why variables do not have a?

2 answers

10


TL; DR

It is the way to determine what the object is and in some cases distinguish one object from the other. Where it is most used is to determine whether it is the same object or another. It’s how you know who is João and who is José, or is João da Silva, or is João da Silva with such affiliation (that’s why certain characteristics alone cannot be used to create unique identity).

Introducing

That’s a controversial term. I’m not going to say that a lot of people use the term the wrong way, as I did in another issue (there I was able to substantiate the difference better, so there I say it is wrong, here it is not).

There is a definition of what is object identity in the context of object orientation. I don’t like it, because it appropriates the term that can be applied otherwise universally and differently and gives another meaning. It’s no secret that I I consider OOP something very ill-defined and conceptually has less value than other disciplines, because what works almost always comes from other disciplines.

I will take the definition I consider most universal and it comes of the root wiki where there are good professionals trying to create quality knowledge about computing, and they usually do, and I won’t take of the wiki Nutella where some people help create some general knowledge, and it doesn’t always work.

So I advise reading both to understand each context. All knowledge is welcome, even when it causes confusion :) Learn to discern the wheat tares.

If you want to stick to just the definition of OOP you may not like the answer, but I think the question wasn’t about that, its origin was in question about the universal definition of object, not the one used in OOP. If it’s ID, then it’s worse :)

Unique identity X equivalent

One of the things I don’t agree on very much is the term identity itself. Because it is used to mean that it is a unique identity, and so the term should be this, composed. If you just use the term "identity" I think it might be something else. But it has a definition that says identity is unique. This sounds confusing, I’ll give you my version. I’m not saying it’s correct, make your judgment. If you disagree then just stick to the unique identity.

There is the unique identity and the equivalent identity. For me every object has an identity, whether the first or the second, it makes no sense not to have, I don’t even know how to call an object without identity.

To unique identity, that people only call identity, it’s easy to understand. You have some identification on the object that makes it unique. If you have objects with this equal (identical) feature in the background they are the same object (or entity in a certain context). It may be a memory address that determines this (such as reference), can be a id guaranteed unique (UUID for example, but can be something sequential), can even be a name, since unique in every solution, or a set of features that together become unique, can be even the whole object, and is more common than one imagines, mainly in immutable objects.

To equivalent identity is when the object possesses an undifferentiated identity, that is, you can have two completely different objects and you cannot tell who is who. The best example is a number 1 in a numerical object. You can have two objects with this value that have nothing to do with each other, it is just a coincidence that they are the same value, and have no additional information that distinguishes them. For me they have identity (it is the number 1) and it is equivalent in the two objects. For other people they have no identity (where I do not agree).

Therefore objects by reference have a natural identity if no other way exists to distinguish objects (in many cases others exist and are better). If you have the same reference in two different places they are certainly pointing to the same object.

In the second definition of C2 wiki (apart from the introduction) the author understands that it only matters the equivalent identity, but it may be the context he used.

If you continue reading there are other ways to define what identity is. I think this is good, even if it causes confusion. We don’t have a definite clear definition (unless someone shows me a).

Internal identity X external

Internal identity or logic is independent of the context in which the object is being used, and it depends on its values. It is common for only a few parts of the object to be considered to give a unique identity. Examples: a id of a person, the CNPJ of a company (this can be a little controversial, see Values that can be entered as primary key).

The external or physical identity depends on the context and where the object is from. This is the case of the reference mentioned above, so if duplicate this object happens to have two different objects, that is, it can have two identical "Joneses" (all the same, being the same person), which is generally problematic (some "modern" methodologies preach this and has caused several problems)is something that should be avoided, for that exists the normalization.

So you have to be careful with external identities because between one execution and another can change, and be the same entity. For most purposes, internal identity is what matters to us.

The internal identity ends up allowing even two different objects to be the same entity. I don’t like that, it creates problems, but a lot of people do. For example you may have a customer and a supplier who are different objects but they are the same entity, are the same company, have a unique information that is present in the two objects, a id for example, or the CNPJ, if you want to risk. For me system that does this wants confusion, and I talk about this in several responses here on the site.

Mutability

Mutable objects can have various characteristics that can be changed and the identity of the object does not change, for example the age, or even the name. Other characteristics cannot change. Changing a specific feature will change identity, and some people do not realize what they are. For example the person may think that if changing the CPF changes the identity, but it does not change, it is possible to change CPF for several reasons and be the same person, therefore it must be the same object.

Immutable objects cannot change any characteristic without changing the identity, even if the new identity is equivalent to the old one. So to me when you do

x = 1
x = 1

the second line, conceptually, is creating a new object with its identity, and is assigning to a variable that already had an object with an identity, and in this case the identities of the objects are equivalent.

The variable doesn’t matter any of that identity, it doesn’t. The most we could say is that it is identified by its name, but this is something else entirely, there is an identification, but it is not the same concept.

Understand more in What "immutable" really means?.

Variables X objects

My intention in that answer was to differentiate one thing from the other. How variable is a storage place doesn’t have because it has identity, its content changes, and if it changes its identity changes, then the variable can’t have its own identity, what’s in it is that it has, that’s all.

Another level of abstraction

At the risk of causing more confusion, we can even say that the variable has identity, but on another level of abstraction. Consider this a completely isolated explanation of the rest of the answer.

Understand here that the code is still a database. Related texts have some meaning. The code in this context is something that exists by itself and is not confused with the application you are creating, nothing to do with the executable that will run your application.

If you consider that variable is a code object (not to be confused with the application object) then it has identity, because every object has identity. This identity is given by attributes (already Linkei question on the subject early on). She has a name, she usually has type (in languages of static typing), scope, at least storage area, and perhaps other characteristics. It depends on the language and context, but changing only one of them is already another variable.

So it’s not that I’m saying that the variable has identity, but that at this level of abstraction that we’re looking at the variable is an object and every object has identity.

If you can’t understand the difference between what your application is and what your code base is, forget that part for a while.

Extra

Researching about, I found a question here that shows how many people do not understand well what is the identity of the object, and I’m not talking about anyone on the page, I’m talking about the creator of the Java API. He thinks a hashcode can demonstrate the identity of an object. Hashcodes can repeat themselves, can be confused with other things. If at least it was the type more the hashcode would help a little, but can still repeat.

2

So I’m going to be very brief and summarize.

Both Object and Variable have identities.

What is an identifier ?

Each variable has an identifier, which is a name given to her and so, every time we need to use it, just state your identifier.

What would be the identity of an object?

An object is the representation of a real world concept/entity, which can be physical (ball, car, tree, etc.) or conceptual (travel, inventory, purchase, etc.) and has a well-defined meaning for a particular software. For this concept/entity, a class should initially be defined from which later distinct objects will be instantiated.

Why variables do not have a?

Variables are basic elements in programming, because they are the ones that store the data that we need in our algorithm. Each data can be of a different type and so we need to show our algorithm right at the start what to expect to store.

Imagine, for example, that we need to separate clothes from clothing according to their type within a closet. This is who tidies up the mess, so we consider it our algorithm, as we see in Figure 1.

inserir a descrição da imagem aqui

Figure 1. How we store data

We understand with the Figure 1 that each drawer corresponds to a variable, which is a space inside the memory of the computer, that is, of our closet.

In the Drawer 1 we keep shirt-like clothes, in the Drawer 2 we keep clothes like pants and in Drawer 3 shorts. In Drawer 2 we know we won’t find shirts, because that’s the kind of clothes Drawer 1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.