Reputation: 197
I was wondering what the overhead of calling short methods were or if the code would get optimized either way and if it was different than the cost of getters?
I'll just give an example because it is hard to explain.
I have a ClaimsManager for a website that gets particular claims and returns them. The process for getting one claim from another differs only by a ClaimsType string.
public string GetClaimValueByType(string ClaimType)
{
return (from claim in _claimsIdentity.Claims
where claim.ClaimType == ClaimType
select claim.Value).SingleOrDefault();
}
/*Again would this be better or worse if I wanted to be able to choose if
I want the claim versus the value?
public Claim GetClaimByType(string ClaimType)
{
return (from claim in _claimsIdentity.Claims
where claim.ClaimType == ClaimType
select claim).SingleOrDefault();
}
public string GetClaimValueByType(string ClaimType)
{
return GetClaimByType(ClaimType).Value;
}
*/
public string GetEmail()
{
return GetClaimValueByType(ClaimTypes.Email);
}
/* Or should I use getters?...
public string Email
{
get
{
return return GetClaimValueByType(ClaimTypes.Email);
}
}
*/
So is this bad practice to have these short get methods? Should there be a large call overhead because it is so short or will this be optimized? Finally, does it make more sense to actually use getters here?..
Thanks
Upvotes: 2
Views: 724
Reputation: 19027
Performance wise there is no difference between a getter and a method. A getter is just syntactic sugar, and is converted to a method during compilation. There are some general guidelines as to when to use a getter and when to use a method. This msdn page advices to use a method instead of a property when:
Upvotes: 2
Reputation: 27441
In my opinion, what ever marginal overhead there may be with using setters and getters is outweighed by the clean and more easily maintainable code that would most likely be easier for any .NET developer off the street to pick up and run with.
But I guess it also depends on how huge your Claim object is. :)
Upvotes: 5
Reputation: 5903
I wouldn't use a getter for this, properties are intended to return a constant value. This means that that sequenced calls should return the same value. This is just a conceptual thing.
Upvotes: -2