Language: EN

que-son-las-propiedades-en-programacion

What is a property

Las Properties are a feature of certain object-oriented programming languages that allow defining getter and setter methods more concisely and legibly.

Managing access to and modification of the properties of a class is fundamental for data integrity. Traditionally, getter and setter methods are used for this, as we saw in the previous article.

Many modern programming languages offer a more intuitive and convenient alternative in the form of properties.

Properties allow users to access the private fields of a class as if they were public variables, while internally using methods to get or set the value.

In essence, they are “syntactic sugar” for creating and using getter and setter methods in a more convenient way for the programmer.

Practical case

Suppose we have a Person class that has a private variable name. We are going to convert this variable to a property.

Person
  • stringName

Details will vary from one language to another. At the end, I will provide examples in different languages. For example, in C# it would be done like this.

class Person {
	string Name { get; set; }
}

Beyond the way of defining it, which as I say in this case varies greatly from one language to another, the important thing is that:

  • We have created a Name property
  • Two getter and setter methods have been created for us
  • There is an internal private variable, which we do not even see

To use our property, we simply use it as if it were a public variable, with =.

Person person = new Person();
person.Name = "Luis";       // invokes the setter
var name = person.Name;     // invokes the getter

Internally, the appropriate getter and setter methods will be invoked, without us having to do anything more. So we see that we maintain the advantage of the methods, with the simplicity of using a variable.

Examples in different languages

Let’s see how the implementation of the Property concept is done in different programming languages.

In C#, properties are defined using the get and set keywords to allow access and modification of private fields of a class.

public class Person
{
    private string name;

    // Property
    public string Name
    {
        get { return name; }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                name = value;
            }
        }
    }
}

// Usage
Person person = new Person();
person.Name = "Juan";
Console.WriteLine(person.Name);

In C++, the concept of properties is not as integrated into the language as in other modern object-oriented languages. However, the Microsoft compiler (MSVC) provides a specific extension called __declspec(property) that allows simulating the behavior of properties similar to what we find in C#.

class Person {
private:
    std::string name;

public:
    void setName(const std::string& value) {
        if (!value.empty()) {
            name = value;
        } else {
            throw std::invalid_argument("Name cannot be empty");
        }
    }

    std::string getName() const {
        return name;
    }

    __declspec(property(get = getName, put = setName)) std::string Name;
};

int main() {
    Person person;
    person.Name = "Juan";
    std::cout << person.Name << std::endl; // "Juan"
    return 0;
}

In JavaScript, access methods (getters) and modification methods (setters) can be used to interact with private fields of an object.

class Person {
    constructor() {
        this._name = '';
    }

    // Property
    get name() {
        return this._name;
    }

    set name(value) {
        if (value) {
            this._name = value;
        }
    }
}

// Usage
const person = new Person();
person.name = "Juan";
console.log(person.name);

In TypeScript, properties can be defined using the get and set syntax

class Person {
    private _name: string;

    // Property
    get name(): string {
        return this._name;
    }

    set name(value: string) {
        if (value) {
            this._name = value;
        }
    }
}

// Usage
const person = new Person();
person.name = "Juan";
console.log(person.name);

In Python, decorators @property and @name.setter can be used to define properties with access and modification methods

class Person:
    def __init__(self):
        self._name = None

    # Property
    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        if value:
            self._name = value

# Usage
person = Person()
person.name = "Juan"
print(person.name)

Best practices Tips

As we can see, properties have many advantages. They allow us to have the benefits of getter and setter methods, with the ease of using a variable.

The only tips to keep in mind are the ones we already saw when we looked at getter and setter methods. That is, mainly the methods must be short and simple.

No heavy, blocking, or side-effect tasks should be performed within a property. The code within the set and get methods of a property must be kept to a minimum.

In most cases, only return or modify the value of the property. At most, do what is necessary to validate and maintain the integrity of the data in your object.

If you have to do something more “heavy”, you should create an independent method (for example, GetFromDatabase()) that clearly indicates the effect its execution will have.