C#中的類是否有確定的推薦佈局?類的佈局
這裏是與佈局的問題的例子...
public class DinnerParty {
//>>>>should the const fields that are initiallised with a value be at the top?
private const int CostOfFoodPerPerson = 25;
//>>>>should the constructor always be placed near the top of the class?
public DinnerParty(int numberOfPeople, bool Health, bool fancyDecorations) {
NumberOfPeople = numberOfPeople;
this.fancyDecorations = fancyDecorations;
Health = healthyOption;
CalculateCostOfDecorations(fancyDecorations);
}
//>>>>backing private fields should always precede properties that use them?
private int numberOfPeople;
public int NumberOfPeople {
get {
return numberOfPeople;
}
set {
CalculateCostOfDecorations(fancyDecorations);
numberOfPeople = value;
}
}
//>>>>where do these fields go?
private bool fancyDecorations;
public decimal costOfBeveragePerPerson;
private bool healthyOption;
public bool HealthyOption{
set {
value = healthyOption;
if (healthyOption) {
costOfBeveragePerPerson = 5.0M;
} else {
costOfBeveragePerPerson = 20.0M;
}
}
}
//>>>>should methods be placed after the constructor and all properties?
public decimal costOfDecorations = 0;
void CalculateCostOfDecorations(bool fancy) {
if (fancy) {
costOfDecorations = (numberOfPeople * 15.0M) + 50.0M;
} else {
costOfDecorations = (numberOfPeople * 7.50M) + 30.0M;
}
}
public decimal CalculateCost(bool xfancy, bool xhealthyOption) {
decimal totalCost = (numberOfPeople * (CostOfFoodPerPerson + costOfBeveragePerPerson)) + costOfDecorations;
if (xhealthyOption) {
return totalCost * 0.95M;
} else {
return totalCost;
}
}
}
看起來像這樣可能會關閉;我懷疑是不夠具體 - 任何人有任何提示編輯的問題,使其更容易接受? (我覺得這個問題可能的答案是有用的) – whytheq
它屬於SO代碼審查或程序員沒有真正涵蓋的半代碼審查領域。我認爲這不是一個好問題,但它可能會成爲一個OK-IS社區維基。 –
@AdamHouldsworth如何將其切換到社區wiki? – whytheq