Property X attribute

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

What is the real difference between them?

Is

Or propriedade a synonym for atributo or vice versa?

Or does it vary by programming language?

Author: Maniero, 2015-07-17

3 answers

Attention! this started by considering only 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 poorly thought out things, repeated without analysis and criticality, producing fallacies. I may be wrong, but challenge me with facts, with information that makes sense. Repeat the mistakes of others do not help.

Introduction

Has a question on the network that speaks generically 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 for the terms within the scope of object orientation and nothing reliable was found. I found some places, again, unreliable, where they interchange the two terms as if they were the same thing, in addition to the confusion already indicated earlier. is this 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 a class or instance variable), is what is called a project pattern of accessor / modifier methods (getter and setter).

Attribute (attribute ) has no relation to this. It is used to mark up 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 keep giving unnecessary explanations. It when it does not cause some major problem.

Note that the language has several ready-made attributes 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 that 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 holds for all object-oriented languages. But there is an implementation difference since other languages do not have the property as a feature of the language itself. It can be emulated by creating accessor / modifier method pair.

In Java there is a attribute that 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 an attribute of the language in Java and in C#. A static also, when using the type of a field is using a type attribute for that field, the same for the field name, yes, this is an attribute.

If the class variables are attributes because the reflection class has a method called getFields() and does not have a getAttributes()? It has a getAnnotations() which is how Java calls attributes.

The attribute engine customized by the programmer is called annotation.

Wrong term

Attribute is often used to indicate the field, but it is a wrong term. The use of the term attribute is relevant in other contexts but not in classes. In Java it is very common for people to speak attribute to refer to the field, but the Oracle documentation does not use this term (at least did not use, it may be in new parts, especially written by the community can use the term, but it is a mistake, and it is terrible that the documentation teaches wrong, so be careful, nor in the documentation can trust, careful.

Probably the term was spread 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 right 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 of what most mainstream programming languages use. I believe this is the reason for using them wrong, if in UML it makes sense to use the term so he thinks that generically it makes sense too, but it's inconsistent with what the languages have adopted. Since in general people work on top of languages all the time and do not treat it 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 of form analogous to Java, and attributes are existing modifiers in the language, but you could not 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 more formally treated as the member guarding the state.

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

Summary

The term property is the form that accesses a feature of the object and attribute is the wrong way to refer to the instance variable where the state is effectively saved, the field (I talk more in detail in another question). But this varies from language to language. Get used to using what the specific community uses, but learn the correct term. The most important thing is to communicate well with 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 , which can be of Class or of instance. Special methods for accessing fields (with own syntax or not) properties are called.

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:

Objection

The other answer talks about another subject not related to the question. She's right, for the most part. I don't agree that the terms are interchangeable even at the conceptual level, and if they were there would not have to be two, let alone 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 you use the 3 as if they were 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 such a way as subtle.

To understand the concept go to What is the difference between attribute and field, in classes?.

 30
Author: Maniero, 2020-09-25 16:31:39

Definition

Modern, high-level programming languages serve nothing but to express a model, express domain rules, so a conceptual dialogue is important for anyone who wants to improve 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 oriented languages a Object (Java, C#, Ruby, Smalltalk), property is the implementation of this attribute, its controlled exposure to the world outside 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 inner field of the Class. And that's not wrong.

Evidence-attribute as class field or object:

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

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

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

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

Evidence-attribute as interchangeable term with property:

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

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

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

  • "value object: an object that describes some characteristics or attributes , but does not carry identity concept."

  • "when you change any of the attributes in the Route specification, we will delete the old Itnerary."

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 piece of equipment, such as its consumption of energy 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 other attributes , a truck has a type, a license number, and a driver."

Attribute or property in programming languages

If we are to develop a solution in Ruby , let's define the accessors for our attributes (the word is recognized by the language).

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

Java and C#, in turn, do not even use either the term "property" or 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).

Conclusion:

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

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

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

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

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

 13
Author: Caffé, 2015-07-20 19:48:55

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

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 queried at runtime using a technique called reflection.

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

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

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

Field perhaps 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. (Cannot be accessed externally)

Property

A property is a member that provides a flexible mechanism for reading, writing, or calculating the value of a particular field. To 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 accessed easily and still helps to promote the security and flexibility of methods. (Can be accessed externally)

See practical examples:

Attribute

[System.Serializable]

Field:

string _nomeUsuario;

Property:

public string NomeUsuario { get; set; }
 -2
Author: Guilherme Alencar JW, 2017-07-27 23:32:33