我有下面的代碼:如何優化這段代碼?
protected StoreDetailModel GetSelectedStore()
{
if (StoresWithDepartmentType != null && StoresWithDepartmentType.Any())
{
StoreDetailModel currentUserStore = WebsiteContext.GetCurrentUserStore();
if (currentUserStore != null && currentUserStore.Item != null)
{
StoreDetailModel store =
StoresWithDepartmentType.FirstOrDefault(x => x.Item.ID ==
currentUserStore.Item.ID);
if (store == null)
{
store = StoresWithDepartmentType.First();
}
return store;
}
}
return null;
}
條件太多了讓這段代碼幾乎沒有可讀性。我如何優化它並使其更清晰?
這是更多的代碼審查的問題。 – 2014-10-02 09:28:38
更多關於代碼審查的問題..但是我不會說代碼是不可讀的說實話。 – BenjaminPaul 2014-10-02 09:28:57
根據是否預期空引用,可以拋出異常並集中處理錯誤。 – 2014-10-02 09:30:07