C
C#13mo ago
triplemocha

❔ Is this good practice with public accessors?

I come from C++ which used get/sets to access private members. In C#, there seems to be several ways of doing this. I learned the latest one. It seems like less time accessing these probbaly due to less function call overhead. I'm creating a CustomDateTime class that's specific toward a game I'm working on. Is below good practice of using these public accessors? I assume the rule is if it fits in one line (member variable or expression), it's good practice?
// Accessors
//
// Hour
public int Hour => (m_hour24 > 12) ? m_hour24 - 12 : m_hour24;
public int Hour24 => m_hour24;
public string AMPM => (m_hour24 >= 12) ? "PM" : "AM";
// Minute
public int Minute => m_minute;
// Second
public int Second => m_second;
// Year
public int Year => m_year;
public string ShortYear => m_year.ToString().Substring(m_year.ToString().Length - 2, 2);
// Month
public int Month => m_month;
public int MonthCount => m_monthNames.Count();
public string MonthName => m_monthNames[m_month - 1];
public string MonthNameAbbr => m_monthNamesAbbr[m_month - 1];
// Day
public int Day => m_dayOfMonth;
public int DaysPerMonth => m_daysPerMonth;
public int DayOfYear => ((m_month - 1) * m_daysPerMonth) + m_dayOfMonth;
public string WeekdayName => m_weekdayNames[(m_dayOfMonth - 1) % 7];
// Format
public string LetterDate => String.Format("{0} {1}, {2}", m_weekdayNames[(m_dayOfMonth - 1) % 7], m_dayOfMonth, Year);
public string ShortDate => String.Format("{0}/{1}/{2}", m_month, m_dayOfMonth, ShortYear);
public string Time => String.Format("{0}:{1:00} {2}", Hour, m_minute, AMPM);
public string Time24 => String.Format("{0}:{1:00}", m_hour24, m_minute);
// Accessors
//
// Hour
public int Hour => (m_hour24 > 12) ? m_hour24 - 12 : m_hour24;
public int Hour24 => m_hour24;
public string AMPM => (m_hour24 >= 12) ? "PM" : "AM";
// Minute
public int Minute => m_minute;
// Second
public int Second => m_second;
// Year
public int Year => m_year;
public string ShortYear => m_year.ToString().Substring(m_year.ToString().Length - 2, 2);
// Month
public int Month => m_month;
public int MonthCount => m_monthNames.Count();
public string MonthName => m_monthNames[m_month - 1];
public string MonthNameAbbr => m_monthNamesAbbr[m_month - 1];
// Day
public int Day => m_dayOfMonth;
public int DaysPerMonth => m_daysPerMonth;
public int DayOfYear => ((m_month - 1) * m_daysPerMonth) + m_dayOfMonth;
public string WeekdayName => m_weekdayNames[(m_dayOfMonth - 1) % 7];
// Format
public string LetterDate => String.Format("{0} {1}, {2}", m_weekdayNames[(m_dayOfMonth - 1) % 7], m_dayOfMonth, Year);
public string ShortDate => String.Format("{0}/{1}/{2}", m_month, m_dayOfMonth, ShortYear);
public string Time => String.Format("{0}:{1:00} {2}", Hour, m_minute, AMPM);
public string Time24 => String.Format("{0}:{1:00}", m_hour24, m_minute);
21 Replies
Angius
Angius13mo ago
$getsetdevolve
MODiX
MODiX13mo ago
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
can be shortened to
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
can be shortened to
class Foo
{
public int Bar { get; set; }
}
class Foo
{
public int Bar { get; set; }
}
Angius
Angius13mo ago
Generally speaking, there's no need for backing fields most of the time And I see plenty of fiends in your code Prefixed with m_ for some reason
Jimmacle
Jimmacle13mo ago
i think that's a C++-ism i prefix private fields with _ personally
Angius
Angius13mo ago
Same
Jimmacle
Jimmacle13mo ago
iirc you can't/shouldn't do that in C++ because _ and __ prefixes are special
triplemocha
triplemocha13mo ago
The question isn't on how to declare variables, as we all have our preferences and hopefully stay consistent with them, but am curious on these expression-bodied members. To me, it seems kind of confusing with what's a method and what's a variable with these accessors. And there's no way to error check them when they're used like variables but is doing a single-lined expression internally.
Jimmacle
Jimmacle13mo ago
a method has () technically property getters and setters are also methods, but abstracted away to have normal field access semantics
Angius
Angius13mo ago
public int Foo => 69; // property
public int Foo() => 69; // method
public int Foo => 69; // property
public int Foo() => 69; // method
Equivalents to
public int Foo { get { return 69; } }
public int Foo()
{
return 69;
}
public int Foo { get { return 69; } }
public int Foo()
{
return 69;
}
respectively
triplemocha
triplemocha13mo ago
I think I might stick to Get() and Set().
Angius
Angius13mo ago
RWwhy mean, sure, go ahead, I'm not gonna kinkshame people for a little bit of masochism
triplemocha
triplemocha13mo ago
At work, we used Get*() and Set*() for VB.Net back in the day. I'm just used to it. But for games, which is what I'm doing, I think it would be good to see which is faster. I know some developers said to stick to Get() and Set() for performance.
Angius
Angius13mo ago
Properties are compiled to methods There's zero performance gain You just waste space writing 4 lines of code instead of 1
triplemocha
triplemocha13mo ago
But what if you want to add error checking and other checks later? It's rewriting it to those 4+ lines. So now you have properties and Get/Set methods. It's not consistent to me.
Angius
Angius13mo ago
No, just use refactoring tools and turn that get-only prop into a full prop And add the code you want
triplemocha
triplemocha13mo ago
I see.
Angius
Angius13mo ago
The refactoring tools will easily turn the property into a property with a backing field And then you just need to add the code, say
public int Foo {
get {
DoSomeStuff(Bar);
return Bar < 0 ? 0 : Bar;
}
}
public int Foo {
get {
DoSomeStuff(Bar);
return Bar < 0 ? 0 : Bar;
}
}
or some such
triplemocha
triplemocha13mo ago
Yeah, that is helpful. Still, is this all good practice with the code above using these to access variables and single-lined expressions?
Angius
Angius13mo ago
Yeah, that's pretty much the reason to use get-only properties
triplemocha
triplemocha13mo ago
Cool, I'll try it out for this project.
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts