15. 3.2. QUY T畉C V畛I HM
7. S畛 d畛ng tham s畛 c畛a hm
- Nhi畛u nh畉t n棚n l 2 tham s畛 畉u vo
- N畉u c坦 nhi畛u tham s畛 h董n 2 th狸 n棚n bao ch炭ng l畉i trong 1 class
Vd:
Circle makeCircle(double x, double y, double radius);
Circle makeCircle(Point center, double radius);
- 畛i v畛i tham s畛 畉u ra c畉n r探 rng, gi畉i th鱈ch nh動 tham s畛 畉u vo.
16. 3.2. QUY T畉C V畛I HM
8. Command Query Separation
- Hm n棚n lm vi畛c g狸 坦 ho畉c tr畉 l畛i gi 坦 nh動ng kh担ng n棚n lm c畉 2.
Vd ph畛 bi畉n:
public boolean set(String attribute, String value);
==> Hm ny set gi叩 tr畛 c畛a attribute l value. N畉u thnh c担ng th狸 tr畉 v畛 true.
==> N坦 s畉 d畉n 畉n c但u l畛nh sau:
if (set("username", "unclebob"))...
==> N畉u ta l reader. Ta s畉 t畛 h畛i. c叩i hm set ny n坦 lm nhi畛m v畛 g狸? 達 l set t畉i sao l畉i cho 動畛c trong if.
==> 畛 fix 動畛c ta c坦 th畛 畛i t棚n l: setAndCheckIfExists ==> C滴ng kh担ng m畉y kh畉 quan.
==> 畛 th畉t s畛 fix 動畛c ta n棚n chia r畉 nhi畛m v畛 c畛a ch炭ng ra nh動 sau:
if (attributeExists("username")) {
setAttribute("username", "unclebob");
...
}
17. 3.2. QUY T畉C V畛I HM
9. Prefer Exceptions to Returning Error Codes
- Tr畉 v畛 l畛i code t畛 hm l vi ph畉m s畛 tinh t畉 c畛a g畛i l畛nh t叩ch bi畛t.
if (deletePage(page) == E_OK)
- i畛u ny kh担ng g但y nh畉m l畉n hay d畉n 畉n x畛 l箪 c畉u tr炭c l畉p. Nh動ng khi tr畉 v畛 l畛i code, ta ph畉i gi畉i quy畉t
ngay n坦.
18. 3.2. QUY T畉C V畛I HM
- Thay v狸 ph畉i d湛ng nhi畛u if nh動 tr棚n ta c坦 th畛 d湛ng Exceptions 畛 x畛 l箪.
19. 3.2. QUY T畉C V畛I HM
- M畛t l畛i th畉 kh叩c c畛a Exceptions n畛a l:
public enum Error {
OK,
INVALID,
NO_SUCH,
LOCKED,
OUT_OF_RESOURCES,
WAITING_FOR_EVENT;
}
==> Khi ta c坦 c叩c l畛i code nh動 tr棚n. S畉 r畉t nhi畛u n董i ph畉i import enum vo. N畉u gi畛 Error c坦 th棚m l畛i. Ta
ph畉i thay 畛i enum.
==> S畉 d畉n 畉n vi畛c c叩c n董i kh叩c d湛ng Error s畉 ph畉i thay 畛i ==> Ta ph畉i recompile and rebuild l畉i.
==> Gi畛 n畉u ta d湛ng Exceptions
24. 3.3. QUY T畉C V畛I COMMENTS
1. Comment gi畉i th鱈ch 炭ng r探 m畛c 鱈ch c畛a n坦.
- Comment kh担ng trang i畛m cho bad code nh動ng n坦 n坦i l棚n r畉ng code c畛a b畉n kh担ng
clean.
- Tr動畛c khi comment, ta h達y ngh挑 t畉i sao ta l畉i ph畉i c畉n comment.
- N畉u ta th畉y o畉n code l畉ng nh畉ng, kh坦 hi畛u ph畉i c畉n comment. Th狸 ta n棚n ginh th畛i
gian 畛 clean o畉n code 坦 thay v狸 ng畛i comment n坦.
- Vd:
// Check to see if the employee is eligible for full benefits
if ((employee.flags & HOURLY_FLAG) && (employee.age > 65))
if (employee.isEligibleForFullBenefits())
- Thay v狸 ph畉i vi畉t comment v 1 畛ng logic nh動 tr棚n. Ta ch畛 c畉n t畛ng ch炭ng vo hm v
畉t t棚n d畛 hi畛u th狸 code clean h董n.
25. 3.3. QUY T畉C V畛I COMMENTS
2. Comment t畛t v h畛p l箪
Vd:
// Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the GNU General Public License version 2 or later.
- Khi publish d畛 叩n th狸 ta n棚n vi畉t comment th畉 no vo source 畛 n坦i quy畛n s畛 h畛u,
version...
26. 3.3. QUY T畉C V畛I COMMENTS
3. Bad Comments
- a ph畉n ch炭ng ta r董i vo 但y.
- C坦 nhi畛u l炭c ta kh担ng c畉n comment, nh動ng ta l畉i c畉m th畉y c畉n comment. v畉y l ta ginh th畛i gian 畛 vi畉t comment.
Vd:
==> Ta c坦 th畛 th畉y o畉n comment tr棚n c坦 th畛 c坦 箪 ngh挑a no 坦 v畛i ng動畛i vi畉t, nh動ng ch畉c ch畉n s畉 v担 ngh挑a v畛i reader.
- Ta c坦 th畛 hi畛u n畉u r董i vo catch th狸 s畉 kh担ng c坦 file no 動畛c load.
- Nh動ng n畉u file 動畛c load th狸 s畉 load 畛 但u, load l炭c no (t叩c gi畉 ch動a n坦i b棚n tr棚n)
27. 3.3. QUY T畉C V畛I COMMENTS
4. Redundant Comments
Vd:
- Ta th畉y comment tr棚n kia l s畛 l畉p l畉i th担ng tin c畛a hm, kh担ng nh畛ng th畉 n坦 c嘆n cho ta
bi畉t
it th担ng tin h董n c畛a hm. N畉u 畛c l畉n 畉u vo comment b畉n s畉 kh担ng hi畛u g狸.
28. 3.3. QUY T畉C V畛I COMMENTS
5. Noise Comments, Comments sai l棚ch
- Nhi畛u khi comments th畉t s畛 l noise. N坦 kh担ng th畉t s畛 cung c畉p cho ta th担ng tin m畛i g狸 c畉.
Vd:
/**
* @param title The title of the CD
* @param author The author of the CD
* @param tracks The number of tracks on the CD
* @param durationInMinutes The duration of the CD in minutes
*/
public void addCD(String title, String author, int tracks, int durationInMinutes) {
CD cd = new CD();
cd.title = title;
cd.author = author;
cd.tracks = tracks;
cd.duration = duration;
cdList.add(cd);
}
/**
* Default constructor.
*/
protected AnnualDateRule() {}
/** The day of the month. */
private int dayOfMonth;
/**
* Returns the day of the month.
*
* @return the day of the month.
*/
public int getDayOfMonth() {
return dayOfMonth;
}
29. 3.3. QUY T畉C V畛I COMMENTS
6. Commented-Out Code
- Kh担ng bao gi畛 n棚n lm i畛u ny.
- Ng動畛i sau 畛c s畉 th畉y kh坦 hi畛u, v kh担ng d叩m x坦a v狸 ng畛 n坦 quan tr畛ng.
- N坦 c嘆n g但y ra hi畛u l畉m. Do 坦 t畛t nh畉t n棚n x坦a i lu担n.
InputStreamResponse response = new InputStreamResponse();
response.setBody(formatter.getResultStream(), formatter.getByteCount());
// InputStream resultsStream = formatter.getResultStream();
// StreamReader reader = new StreamReader(resultsStream);
// response.setContent(reader.read(formatter.getByteCount()));