-
Notifications
You must be signed in to change notification settings - Fork 1
week6 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hapo-tuanlh
wants to merge
1
commit into
master
Choose a base branch
from
week6
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
week6 #8
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| --- | ||
| sidebar_position: 3 | ||
| --- | ||
|
|
||
| ## DRY | ||
|
|
||
| ````php | ||
| //bad | ||
|
|
||
| <?php | ||
| function showDeveloperList(array $developers): void | ||
| { | ||
| foreach ($developers as $developer) { | ||
| $expectedSalary = $developer->calculateExpectedSalary(); | ||
| $experience = $developer->getExperience(); | ||
| $githubLink = $developer->getGithubLink(); | ||
| $data = [ | ||
| $expectedSalary, | ||
| $experience, | ||
| $githubLink | ||
| ]; | ||
|
|
||
| render($data); | ||
| } | ||
| } | ||
|
|
||
| function showManagerList(array $managers): void | ||
| { | ||
| foreach ($managers as $manager) { | ||
| $expectedSalary = $manager->calculateExpectedSalary(); | ||
| $experience = $manager->getExperience(); | ||
| $githubLink = $manager->getGithubLink(); | ||
| $data = [ | ||
| $expectedSalary, | ||
| $experience, | ||
| $githubLink | ||
| ]; | ||
|
|
||
| render($data); | ||
| } | ||
| } | ||
|
|
||
| //good | ||
| function showList(array $employees): void | ||
| { | ||
| foreach ($employees as $employee) { | ||
| render([ | ||
| $employee->calculateExpectedSalary(), | ||
| $employee->getExperience(), | ||
| $employee->getGithubLink() | ||
| ]); | ||
| } | ||
| } | ||
| ```` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| --- | ||
| sidebar_position: 3 | ||
| --- | ||
|
|
||
| ## Định dạng theo chiều dọc | ||
|
|
||
| - Code của bạn không nên quá dày đặc theo chiều dọc, khai báo biến có thể viết liền lại với nhau và không nên có comment hoặc ngăn cách chúng bằng 1 dòng trống, điều đó phá vỡ sự liên kết.. | ||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| public class ReporterConfig { | ||
| /** | ||
| * The class name of the reporter listener | ||
| */ | ||
| private String m_className; | ||
| /** | ||
| * The properties of the reporter listener | ||
| */ | ||
| private List<Property> m_properties = new ArrayList<Property>(); | ||
| public void addProperty(Property property) { | ||
| m_properties.add(property); | ||
| } | ||
|
|
||
| //good | ||
| <?php | ||
|
|
||
| public class ReporterConfig { | ||
| private String m_className; | ||
| private List<Property> m_properties = new ArrayList<Property>(); | ||
|
|
||
| public void addProperty(Property property) { | ||
| m_properties.add(property); | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| # Khoảng cách giữa các phần | ||
| - Gần như tất cả code của bạn được đọc từ trái qua phải, từ trên xuống dưới. Mỗi dòng đại diện cho một biểu thức hoặc một mệnh đề, và mỗi nhóm dòng đại diện cho một mạch logic hoàn chỉnh. Mỗi dòng trống là một dấu hiệu để người đọc xác định một khái niệm mới và riêng biệt | ||
| ````php | ||
| <?php | ||
| //good | ||
| const [visible, setVisible] = React.useState(false); | ||
|
|
||
| const [index, setIndex] = React.useState(0); | ||
| ```` | ||
|
|
||
| # Khai báo biến | ||
| - Các biến được khai báo càng gần với nơi sử dụng chúng càng tốt. Bởi hàm sử dụng chúng rất ngắn, các biến cục bộ phải đước khai báo ở những dòng đầu tiên của mỗi hàm. | ||
| ````php | ||
| <?php | ||
| const getProperties = (item: ItemProps) => { | ||
| if (item.prices != null && item.prices.properties.length > 0) { | ||
| let property = "" | ||
| item.prices.properties.forEach((it: ItemPropertyProps) => { | ||
| property = `${property}${it.name}, ` | ||
| }) | ||
| return property.substring(0, property.length - 2) | ||
| } | ||
| return null | ||
| } | ||
| ```` | ||
|
|
||
| # Các hàm phụ thuộc nhau. | ||
| - Nếu có một hàm gọi một hàm khác, chúng nên đặt gần nhau. Nếu có thể, hàm gọi nên ở trên hàm được goi, tạo một dòng chảy mã nguồn từ mức cao đến mức thấp. Cũng đừng thu nhỏ font chữ để tăng số lượng ký tự trên 1 dòng màn hình. | ||
| ````php | ||
| <?php | ||
| ngOnInit() { | ||
| this.loadData(); | ||
| } | ||
|
|
||
| loadData() { | ||
| this.loadLanguage(); | ||
|
|
||
| this.currentMemberId = await this.storage.get('currentMemberId'); | ||
| } | ||
|
|
||
| loadLanguage() { | ||
| // Load language here | ||
| } | ||
| ```` | ||
|
|
||
| ## Định dạng theo chiều ngang | ||
| # Việc lùi đầu dòng | ||
| - Một file code nên giống một hệ thống hoàn chỉnh hơn là một bản phác thảo sơ sài. Sẽ có thông tin về toàn bộ file, thông tin về các class riêng lẻ trong file, thông tin về các khối lệnh của hàm và các khối lệnh con trong hàm. Mỗi cấp độ của hệ thống này tạo ra một phạm vi mà ở đó bạn có thể khai báo các biến, trong đó các khai báo và câu lệnh được thể hiện một cách rõ ràng. | ||
| ````php | ||
| //bad | ||
| <?php | ||
| public class FitNesseServer implements SocketServer { private FitNesseContext context; public FitNesseServer(FitNesseContext context) { this.context = context; } public void serve(Socket s) { serve(s, 10000); } public void serve(Socket s, long requestTimeout) { try { FitNesseExpediter sender = new FitNesseExpediter(s, context); sender.setRequestParsingTimeLimit(requestTimeout); sender.start(); } catch(Exception e) { e.printStackTrace(); } } } | ||
|
|
||
| //good | ||
| <?php | ||
| public class FitNesseServer implements SocketServer { | ||
| private FitNesseContext context; | ||
|
|
||
| public FitNesseServer(FitNesseContext context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| public void serve(Socket s) { | ||
| serve(s, 10000); | ||
| } | ||
|
|
||
| public void serve(Socket s, long requestTimeout) { | ||
| try { | ||
| FitNesseExpediter sender = new FitNesseExpediter(s, context); | ||
| sender.setRequestParsingTimeLimit(requestTimeout); sender.start(); | ||
| } | ||
| catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| ```` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| --- | ||
| sidebar_position: 3 | ||
| --- | ||
|
|
||
| ## Đặt tên biến dễ hiểu | ||
| - Tức là đặt tên biến sao cho đọc vô là hiểu nó là gì và nó dùng để làm gì. Không cần phải suy nghĩ, suy diễn. | ||
|
|
||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| $ymdstr = $moment->format('y-m-d'); | ||
| //good | ||
| <?php | ||
|
|
||
| $currentDate = $moment->format('y-m-d'); | ||
| ```` | ||
|
|
||
| ## tránh lồng quá nhiều và nên return sớm | ||
| - Quá nhiều if else lồng nhau sẽ khiến code tăng độ phức tạp, khó debug. Giảm sự phức tạp bằng cách giảm số if else lồng nhau xuống ít nhất có thể. Return sớm chính là một cách giảm số lần lồng nhau. | ||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| function isShopOpen($day): bool | ||
| { | ||
| if ($day) { | ||
| if (is_string($day)) { | ||
| $day = strtolower($day); | ||
| if ($day === 'friday') { | ||
| return true; | ||
| } elseif ($day === 'saturday') { | ||
| return true; | ||
| } elseif ($day === 'sunday') { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } else { | ||
| return false; | ||
| } | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| //good | ||
| <?php | ||
| function isShopOpen(string $day): bool | ||
| { | ||
| if (empty($day)) { | ||
| return false; | ||
| } | ||
|
|
||
| $openingDays = [ | ||
| 'friday', 'saturday', 'sunday' | ||
| ]; | ||
|
|
||
| return in_array(strtolower($day), $openingDays, true); | ||
| } | ||
| ```` | ||
|
|
||
| ## tránh hack não người đọc | ||
| - Đừng khiến người đọc code phải khó khăn để hiểu ý nghĩa của biến. Tên biến càng rõ ràng càng tốt. | ||
|
|
||
| ````php | ||
| //bad | ||
| <?php | ||
| $l = ['Austin', 'New York', 'San Francisco']; | ||
|
|
||
| for ($i = 0; $i < count($l); $i++) { | ||
| $li = $l[$i]; | ||
| doStuff(); | ||
| doSomeOtherStuff(); | ||
| // ... | ||
| // ... | ||
| // ... | ||
| dispatch($li); | ||
| } | ||
| //good | ||
| <?php | ||
| $locations = ['Austin', 'New York', 'San Francisco']; | ||
|
|
||
| foreach ($locations as $location) { | ||
| doStuff(); | ||
| doSomeOtherStuff(); | ||
| // ... | ||
| // ... | ||
| // ... | ||
| dispatch($location); | ||
| } | ||
| ```` | ||
|
|
||
| ## Đừng thêm những nội dung không cần thiết | ||
| - Nếu tên của class/object đã rõ ràng, không nên lặp lại chúng trong tên biến. | ||
|
|
||
| ````php | ||
| //bad | ||
| <?php | ||
| class Car | ||
| { | ||
| public $carMake; | ||
| public $carModel; | ||
| public $carColor; | ||
|
|
||
| //... | ||
| } | ||
| //good | ||
| <?php | ||
| class Car | ||
| { | ||
| public $make; | ||
| public $model; | ||
| public $color; | ||
|
|
||
| //... | ||
| } | ||
| ```` | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chuyen vi du sang code php