Property x Attribute

Asked

Viewed 8,360 times

37

One propriedade of a class is not the same thing as a atributo?

What’s the real difference between them?

Or propriedade is a synonym for atributo or vice versa?

Or varies according to the programming language?

  • 4

    Attribute is the UML term for what we implement as class (or object) properties in our code.

  • 1

    There are the attributes of C# -> [DllImport("user32.dll")] -> Attributes

  • 1

    Yes, for defining metadata of class elements or class metadata. Note that Microsoft itself uses the term "attribute" when talking about designing classes using UML: https://msdn.microsoft.com/en-us/library/dd323861.aspx. It’s nice to choose a coherent language in your project, but it’s not wrong when someone switches the terms "attribute" and "property" to refer to the same thing.

  • @Meuchapeu Just to be clear, are you talking about UML or programming languages in general? Did I understand what you wanted? Or what you want is something else?

  • 2

    @bigown, I’m not talking about UML but programming languages in general.

  • 1

    @Meuchapeu Don’t worry about UML, it’s just one of the indicators that terms are interchangeable. I added an answer with enough evidence of this, without ever mentioning the word UML ;-) Good reading.

Show 1 more comment

3 answers

33


Heed! This started with just the context of C#, but now after research I conclude that what is written in this answer applies to any programming language, perhaps except Smalltalk. I’m sorry, but the other answer is full of thoughtless things, repeated without analysis and criticism, producing fallacies. I may be wrong, but challenge me with facts, with information that makes sense. Repeating the mistakes of others does not help.

Introducing

Has a network question that speaks in a generic way but cites an example in the IT area.

For use in software development, especially in object orientation as specified, this may depend on the language you are using.

I searched the terms for object orientation and nothing reliable was found. I found some places, again, unreliable, where they exchanged the two terms as if they were the same thing, besides the confusion already indicated previously. This is canonical and reliable?

C#

In C# clearly the property (Property) is a member of a class that provides information about the object/class. In fact the property itself is a method. Often it accesses the state that is effectively in a field (field) (sometimes also called class variable or instance), is what is called standard access/modifier method design (getter and Setter).

Attribute (attribute) It has nothing to do with this. It is used to mark or modify classes and methods by adding relevant information that can be used by the compiler, utilities or even the application at runtime.

If you exchange the terms property and attribute, you will confuse the interlocutor and you will have to give unnecessary explanations. This when it does not cause any major problem.

Note that the language has several attributes ready and that are part of its syntax as specified. In addition to these attributes there is a mechanism where you can create and use your own attributes which are those that are used with brackets to disambiguate with other names.

Java

In Java the property is the same as in C#, I believe we can say that it applies to all object-oriented languages. But there is a difference in implementation since other languages do not have the property as a feature of the language itself. It can be emulated by creating pair of access/modifier methods.

In Java there is a attribute which is used similarly to C# but does not have this clear in the language specification. The attribute exists, only it is not clarified, after all a public is a language attribute in Java and C#. A static also, when you use the type of a field you are using a type attribute for that field, the same for the field name, yes, this is an attribute.

If class variables are attributes because a class of reflection has a method called getFields() and doesn’t have a getAttributes()? Has a getAnnotations() which is what Java calls the attributes.

The programmer’s custom attribute mechanism is called annotation.

Wrong term

Attribute is usually used to indicate the field, but it is a wrong term. The use of the term attribute is pertinent in other contexts but not in classes. In Java it is very common for people to speak attribute to refer to the field, but to oracle documentation does not use this term (at least not used, may be in in new parts, especially written by the community may use the term, but it is a mistake, and it is terrible that the document teaches wrong, so take care, nor in the documentation can trust, can but only in the part that was written by careful professionals.

Probably the term was disseminated by books and courses made by those who do not understand or do not care about the quality of the content, who do not want to teach the correct way, only the way that solves.

Of course, in some context the term may be correct. As UML quoted in the comments, but it is not the case than most programming languages mainstream use. I believe this is the reason they use it wrong, if in UML it makes sense to use the term then I think that in a generic way it makes sense too, but it is inconsistent with what the languages have adopted. Since in general people work on languages all the time and do not treat in a generic way I think they should change the term in other contexts, or stop using a tool like UML (many people are leaving).

Other languages

Other languages probably use the terms in their own way. C++, for example, uses the term property similarly to Java, and attributes are existing modifiers in the language, but you couldn’t create one, now you can.

In Python a property is a special form of attribute to access state, more or less like in other languages. The attribute is treated more formally as the member guarding the state.

In some places I have seen the term property used for the fields directly. There are languages that can use both terms interchangeably. So your conclusion is correct, depends on language yes.

Summary

The term property is the form that accesses a characteristic of the object and attribute is the wrong way to refer to the instance variable where the state is effectively saved, the field (I speak in more detail in another question). But this varies from language to language. Get used to what the specific community uses, but learn the correct term. The most important thing is to communicate well with the people who use the tool you use. So get used to using the term within each context.

Member refers to any element within a class (actually any type, since some languages have types that are not classes). Some members are fields, often confused with attributes in some communities. These fields are variables, that may be class or of instance. Special methods for accessing fields (with own syntax or not) are called estates.

References

A interesting article when choosing one or the other in C#. Which shows how confusing it can be if you use the wrong terms.

Wikipedia:

Contestation

The other answer talks about another issue unrelated to the question. She’s right, mostly. I do not agree that terms are interchangeable even at the conceptual level, and if they were there would not need to be two, much less do I think that attribute is the same as field, since it would make attribute, field and property the same thing. It’s insane to say that. I understand that in practice the 3 are used as one thing, precisely because people do not know what it is about, but it is wrong to say that it is the same thing. And those who do not understand the correct terms end up producing incorrect solutions in a subtle way.

To understand the conceptual go on What is the difference between attribute and field in the classes?.

12

Definition

Modern, high-level programming languages serve nothing but to express a model, express domain rules, so a conceptual dialogue is important for those who want to improve themselves as a developer.

In Object Orientation, "attribute" and "property" are terms that can be interchangeable.

This is because "attribute", conceptually, is a characteristic that an object carries, and in various object-oriented languages (Java, C#, Ruby, Smalltalk), property is the implementation of this attribute, its controlled exposure to the outside world to the object.

This concept that "property" is a way to implement an "attribute" in a class or object has brought us the practice of saying that attribute is an internal field of the class. And that’s not wrong.

Evidence - Attribute as class or object field:

See this paper by Martin Fowler: Dealing With Properties (or "Dealing with properties"). In this article he refers to "attribute" as being a class field, something that will be consumed externally as a property.

See also these two articles on Smalltalk: Smalltalk Object Model and Smalltalk Basics - Definitions, Nomenclature and Concepts. They also treat attributes as internal class or object fields.

See also the way to declare properties in Ruby, the keyword is prefixed with attr (of attribute). Examples:

def Pessoa
    attr_accessor :nome # propriedade leitura e escrita
    attr_reader :cpf # propriedade somente leitura
end;

Evidence - Attribute as term interchangeable with property:

When we define our object model, it is natural to only talk about the characteristics and behaviors of an object, that is, we do not talk about its internal data that is of interest only to code within the class.

This is because the internal data of the object are implementation details and are not of interest for example of business experts.

In his book Domain-Driven Design, Eric Evans uses the term "attribute" to talk about the characteristics of business objects. Examples:

  • "Object Value: An object that describes some features or attributes, but does not carry the concept of identity."

  • "When you change any one of attributes in the Route Specification, we will delete the old Itinerary."

In his legendary book Design Patterns: Elements of Reusable Object-Oriented Software, the "gang of four" alternates the use of the terms "property" and "attribute" to refer to the same thing, for example:

  • "Equipment declares operations that return the attributes of a part of the equipment, such as its energy consumption and cost."

In his book The Pragmatic Programmer, Dave Thomas also alternates the terms "attribute" and "property" to refer to the characteristics of an object. For example:

  • "Let’s say our analysis reveals that, among others attributes, a truck has a type, a license number, and a driver."

Attribute or property in programming languages

If we develop a solution in Ruby, let’s set the accessors for our Attributes (the word is recognized by the language).

If we develop in VB.Net or Delphi, let’s create properties (the word is recognized by the language).

Java and C#, in turn, use neither the term "property" nor the term "attribute" in the language to refer to what we are dealing with here (these terms are conceptual and are not present in the language).

Completion:

At the conceptual level, it is common to say that attributes are the characteristics of the class or object, the data that the object carries.

Historically we have been doing this, and not only bad tools or bad people do this, but also tools and well-known people, old or modern, as shown in the references of this answer.

At the programming level, we implement attributes such as properties, controlling their exposure to the outside world.

Finally, one has to go up to the conceptual level to say what is attribute and what is property, because each language has particularities as to the use of these terms (this when they refer to these terms, because some of them do not use them in the actual language).

This answer sought to demonstrate that both are the same thing, mainly at the conceptual level of our objects and that, by descending to the level of language at which the domain is implemented, particularities may arise as to the use of the terms, but that these particularities should not overshadow the understanding of what the expression "object and its attributes" means in an object-oriented solution.

  • 3

    If I understand correctly, you basically argue that "properties" are variables within any program, that is, a mechanism to implement the concept of object orientation attribute. Right? If my understanding is correct, I liked the answer and agree with it. I also argue that attribute and property are synonymous because, from a conceptual point of view, an attribute/property is something that a class/object has, and a method/Handler/function is something that a class/object ago.

  • 4

    This metaphor is useful to understand that no matter the accessibility (public, private, protected) of an attribute/property or method/function, the concepts are unique. Each language has the freedom to choose how best to facilitate the life of the programmer, and if any choose to use, for example, property to define an attribute that you already have getters and setters, better for the programmer. But it doesn’t change the concept.

-1

In C# and Visualbasic the word "attribute" has another meaning:

Attribute (attribute)

Attributes provide a powerful method of associating metadata or declarative information with code (assemblies, types, methods, properties and so on).

After an attribute is associated with a program entity, the attribute can be consulted at runtime using a technique called Reflection.

Attributes add metadata to your program. Metadata is information about the types defined in a program.

All assemblies . NET contain a set of metadata describing types and their members. You can add custom attributes to specify any additional information required.

You can apply one or more attributes to small program assemblies, modules or elements such as classes and properties.

Field () <maybe an attribute in other programming languages>

A field is a variable of any type that is declared directly in a class or struct. In general, you should only use fields for variables that have particular or protected accessibility. (It cannot be accessed externally)

Property (Property)

A property is a member that offers a flexible mechanism to read, write or calculate the value of a particular field. Properties can be used as if they were members of public data, but in fact they are really special methods called accessors. This allows data to be easily accessed and even helps promote the security and flexibility of methods. (Can be accessed externally)

See practical examples:

Attribute (attribute)

[System.Serializable]

Field ():

string _nomeUsuario;

Ownership (Property):

public string NomeUsuario { get; set; }

Browser other questions tagged

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