day1homework's People
day1homework's Issues
homework completed.
建議調整項目
- 命名調整:在 C# 中命名通常採 Pascal/Camel, 也就是全名,無底線,單字之間透過字母大寫來區分,整個宣告首字母大寫為 Pascal, 小寫為 Camel。越重要的,外面看得到的,都是 Pascal, 只有自己看得到的,存活時間短的,採 Camel。(例如 argument, variable, field)。這邊該修改的有 Prod, cal,
- 命名調整:通常 data 這個字都是贅字,原因是什麼都是 data, 如果有兩份不一樣的 data 時,命名就會造成困擾、修改成本,且在語意上混淆不清。
- production code 寫法調整:
List<T> List = CalList.Skip((pagecnt - 1) * RowCnt).Take(RowCnt).ToList();
for (int j = 0; j < List.Count(); j++)
{
int iSum = 0;
if(int.TryParse(List[j].GetType().GetProperty(CalColName).GetValue(List[j], null).ToString(), out iSum))
Sum += iSum;
}
後面那段 for loop 可以直接改成 上面那一行 .Skip().Take().Sum(x=>GetValue(column)); 就結束了。GetValue(column) 的內容就是 reflection 取 property value 的那一段。
- property 的部分使用 string 傳進來,是很標準使用 reflection 的方式,好處是不管未來新增什麼 property, API都不用變。而可能衍生的不方便是:第一,當未來針對 property rename 時,傳入的 string 參數不會跟著變,就會出現問題。第二,對呼叫端來說,需要對 Product 的 property name 很清楚,且字串不能打錯字,例如大小寫,而且 string 無法享用 intellisense 的好處。
參考
可以參考一下我的 sample code: https://gist.github.com/hatelove/f5a4c2591293828b637f#file-getsumbypagesize_v2-cs
重點 focus 在下列幾點:
- 呼叫端怎麼呼叫最好用, 語意清楚, 參數夠少, intellisense輔助。
- API 在需求異動時,能越穩定越好,代表支援有足夠的彈性。在面對重構時,也不會影響到呼叫端的使用,例如 property rename。
- 測試案例要怎麼呈現,最像口語化的描述著呼叫端使用情境。包含怎麼寫語意最清楚,寫起來最快。
這個 homework 基本上有四種方式來實作,也考驗著對 C# 的熟悉程度。
- 使用 enum, production code 以 if/else 來分辨
- 使用 string + reflection 來動態決定使用哪一個 property 加總
- 使用 delegate/lambda 針對 Product 型別來處理
- 使用 generic 支援泛型,使用 extension method 針對所有集合都提供這樣的功能
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.