Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a property "FullName" bad, when there are "FirstName" and "LastName"?

Tags:

c#

properties

I have an Employee class:

class Employee
{
    string FirstName { get; set; }
    string LastName { get; set; }
}

Sometimes I want to get the full name and it's tedious to write emp.FirstName + emp.LastName again and again and again.

Is it bad practice to add a FullName property (because it's the same data twice or something)?

class Employee
{
    string FirstName { get; set; }
    string LastName { get; set; }
    string FullName => $"{FirstName} {LastName}";
    }
}
like image 645
Michael Haddad Avatar asked Sep 15 '25 05:09

Michael Haddad


1 Answers

It's not the same data twice, you are actually cutting down on repeated code by adding this.

It would be bad if FullName wasn't calculated and it had to be maintained (kept in sync, set etc.) alongside FirstName/LastName.

like image 120
brent Avatar answered Sep 17 '25 19:09

brent