跳转到内容

“XOOPS 的整洁代码原则”

干净的代码是易于阅读、理解和维护的代码。本指南涵盖了专门应用于 XOOPS 模区块开发的干净代码原则。

mindmap
root((Clean Code))
Readability
Meaningful Names
Small Functions
Comments When Needed
Simplicity
Single Responsibility
DRY Principle
KISS Principle
Maintainability
Consistent Style
Error Handling
Testing
// Bad
$d = new DateTime();
$u = $memberHandler->getUser($id);
$arr = [];
// Good
$createdDate = new DateTime();
$currentUser = $memberHandler->getUser($userId);
$publishedArticles = [];
// Bad
function process($data) { ... }
function handle($item) { ... }
function doStuff($x, $y) { ... }
// Good
function publishArticle(Article $article): void { ... }
function calculateTotalPrice(array $items): float { ... }
function sendNotificationEmail(User $user, string $subject): bool { ... }
// Bad
class Manager { ... }
class Helper { ... }
class Utils { ... }
// Good
class ArticleRepository { ... }
class NotificationService { ... }
class PermissionChecker { ... }
// Bad - does too many things
function processArticle($data) {
// Validate
if (empty($data['title'])) {
throw new Exception('Title required');
}
// Save
$article = new Article();
$article->setTitle($data['title']);
$this->repository->save($article);
// Notify
$this->mailer->send($article->getAuthor(), 'Article published');
// Log
$this->logger->info('Article created');
return $article;
}
// Good - each function does one thing
function validateArticleData(array $data): void
{
if (empty($data['title'])) {
throw new ValidationException('Title required');
}
}
function createArticle(array $data): Article
{
$this->validateArticleData($data);
return Article::create($data['title'], $data['content']);
}
function publishArticle(Article $article): void
{
$this->repository->save($article);
$this->notifyAuthor($article);
$this->logArticleCreation($article);
}

保持函数简短 - 最好在 20 行以下:

// Good - focused function
public function getPublishedArticles(int $limit = 10): array
{
$criteria = new CriteriaCompo();
$criteria->add(new Criteria('status', 'published'));
$criteria->setSort('published_at');
$criteria->setOrder('DESC');
$criteria->setLimit($limit);
return $this->repository->getObjects($criteria);
}
// Bad - repeated code
function getActiveUsers() {
$criteria = new CriteriaCompo();
$criteria->add(new Criteria('level', 0, '>'));
$criteria->setSort('uname');
return $this->userHandler->getObjects($criteria);
}
function getActiveAdmins() {
$criteria = new CriteriaCompo();
$criteria->add(new Criteria('level', 0, '>'));
$criteria->add(new Criteria('is_admin', 1));
$criteria->setSort('uname');
return $this->userHandler->getObjects($criteria);
}
// Good - shared logic extracted
function getUsers(CriteriaCompo $criteria): array
{
$criteria->add(new Criteria('level', 0, '>'));
$criteria->setSort('uname');
return $this->userHandler->getObjects($criteria);
}
function getActiveUsers(): array
{
return $this->getUsers(new CriteriaCompo());
}
function getActiveAdmins(): array
{
$criteria = new CriteriaCompo();
$criteria->add(new Criteria('is_admin', 1));
return $this->getUsers($criteria);
}
// Bad - generic exceptions
throw new Exception('Error');
// Good - specific exceptions
throw new ArticleNotFoundException($articleId);
throw new PermissionDeniedException('Cannot edit article');
throw new ValidationException(['title' => 'Title is required']);
public function findArticle(string $id): ?Article
{
try {
return $this->repository->findById($id);
} catch (DatabaseException $e) {
$this->logger->error('Database error finding article', [
'id' => $id,
'error' => $e->getMessage()
]);
throw new ServiceException('Unable to retrieve article', 0, $e);
}
}
// Bad - obvious comment
// Increment counter
$counter++;
// Good - explains why, not what
// Cache for 1 hour to reduce database load during peak traffic
$cache->set($key, $data, 3600);
// Good - documents complex algorithm
/**
* Calculate article relevance score using TF-IDF algorithm.
* Higher scores indicate better match with search terms.
*/
function calculateRelevanceScore(Article $article, array $terms): float
{
// ...
}
class ArticleService
{
// 1. Constants
private const MAX_TITLE_LENGTH = 255;
// 2. Properties
private ArticleRepository $repository;
private EventDispatcher $events;
// 3. Constructor
public function __construct(
ArticleRepository $repository,
EventDispatcher $events
) {
$this->repository = $repository;
$this->events = $events;
}
// 4. Public methods
public function publish(Article $article): void { ... }
public function archive(Article $article): void { ... }
// 5. Private methods
private function validateForPublication(Article $article): void { ... }
}
  • 名字有意义且发音清晰
  • 函数只做一件事
  • 函数很小(< 20 行)
  • 没有重复的代码
  • 正确处理特定异常的错误
  • 评论解释“为什么”,而不是“什么”
  • 一致的格式和风格
  • 没有幻数或字符串
  • 依赖项是注入的,而不是创建的
  • 代码组织
  • 错误处理
  • 测试最佳实践
  • PHP标准