Expression-bodied is recommended? Does it have performance differences?

4

To what extent is it recommended, or even good practice, to use expression-bodied ?

I know that expression-bodied allow properties, methods, operators, and other function members to have bodies defined using lambda expressions ( => ) rather than instruction blocks, which helps reduce the amount of code and gives a clearer view of the expressions.

Does this practice impact performance or is it only "visual", leaving the code leaner?

Example below:

No expression-bodied :

public class Item
{
    public int Quantidade { get; set; }
    public double Preco { get; set; }
    public double Total
    {
        get
        {
            return Quantidade * Preco;
        }
    }
}

With expression-bodied :

public class Item
{
    public int Quantidade { get; set; }
    public double Preco { get; set; }
    public double Total => Quantidade * Preco;
}
    
asked by anonymous 20.08.2018 / 20:33

2 answers

4

It was created, so it is recommended. In general there are no contraindications in the use that it was designed, which is something that only has a simple expression and already returns it.

What I would like people to understand is that language puts these things because it wants to be less verbose or want to reduce the number of lines. And that way people would think that other things they do that increase the number of lines and is not as fitting as they think, because they learned that it was "good practice" (which I always critique here on the site and "> palestrei about this and I will post the slides until next week).

Is not the same as a var for example, that has cases that there are difficulties in its use . This only does not use who does not want, and taste is not discussed, only regrets:)

Performance changes nothing. Semantics also, or any other criterion, is only more readable in the second form, unless the person does not know how to program. But the solution to this is to learn.

See the ShapLab as is the form

20.08.2018 / 20:40