Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions docs/coding-practices/DRY.md
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()
]);
}
}
````
114 changes: 114 additions & 0 deletions docs/coding-practices/formatingcode.md
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 {
Copy link
Collaborator Author

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

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();
}
}
}
````
118 changes: 118 additions & 0 deletions docs/coding-practices/variable.md
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;

//...
}
````