- 더러운 코드
private void InitDefaultItems() // 줄여서 Init으로 쓴 이유가 무엇일까
{
if (!ExistDefaultItems(Data.Income)) { // 뜻은 통하지만 Is나 Has로 시작하는 개선의 여지가 보이는 작명
LoadIncome();
var lastOtherIncomeItems = ConvertStringToOtherItems(otherIncomeItems); //String은 파라미터 형식으로 유추가능
Income.AddRange(lastOtherIncomeItems);
}
Income.EnsureMinimumItems(() => new OtherItem(), MinItemCount);
if (!ExistDefaultItems(Data.Expense)) {
LoadExpense();
var lastOtherExpense = ConvertStringToOtherItems(otherExpenseItems);
if (lastOtherExpense.Count > 0) {
Expense.AddRange(lastOtherExpense);
}
}
Expense.EnsureMinimumItems(() => new OtherItem(), MinItemCount);
}
// Income과 Expense을 다른 구문으로 나눠서 적용해도 좋을 듯
- 깨끗한 코드
private void AccountBtnClick(AccountInfo account, bool isIncome = true, bool isNew = false)
{ // Button이 길어서 Btn으로 줄여쓴걸까.. 하는 역할에 따라 이름을 적절히 변경 가능해 보인다.
var window = Cursors.Wait.ShowCursor(() => AccountWindow.Load(account.Name, account.Id, new OwnerlessWindowPosition(this)));
bool isExistDuplicated = account.GetDuplicatedCount(isIncome ? Income.Select(x => x.Account) : Expense.Select(x => x.Account)) > 1;
string originalAccountName = account.Name;
window.ShowOwnerDialog(() => {
if (isNew && isExistDuplicated && MessageDialog.Show(Messages.Get("Do you apply account to all?"), window, MessageDialogType.Question, "Apply Once;Apply to all") == "Apply to all") {
ApplyAccountsId(originalAccountName, window.AccountName, window.AccountId); // 왜 Accounts?
}
else if (isIncome) {
Income[idcIncome.FocusedItemIndex].Account.Name = window.AccountName;
Income[idcIncome.FocusedItemIndex].Account.Id = window.AccountId;
}
else {
Expense[idcExpense.FocusedItemIndex].Account.Name = window.AccountName;
Expense[idcExpense.FocusedItemIndex].Account.Id = window.AccountId;
}
});
}
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[1] is TimeUnitType timeType && values[2] is Laytime laytime && values[3] is bool naLaytimeCalculator) {
if (naLaytimeCalculator) {
return null;
}
return laytime.IsOperation ? GetLaytimePortDaysString(laytime.Operation.Actual.Total.RemainedDay, laytime.Operation.Actual.LaytimeAmount) : GetLaytimePortDaysString(laytime.Estimation.Laytime, laytime.Estimation.DemDes);
}
return null;
string GetLaytimePortDaysString(double remainDay, double demdesPrice)
{
string days = timeType == TimeUnitType.Days ? "days" : "hours";
remainDay *= (timeType == TimeUnitType.Days ? 1 : 24);
string saveLost = remainDay > 0 ? "Laytime Saved" : "Laytime Lost";
string demDes = remainDay > 0 ? "Despatch" : "Demurrage";
return $"{saveLost} {Math.Abs(remainDay).ToString("N6")} {days} {demDes} {Math.Abs(demdesPrice).ToString("N2")}";
}
}