会议与评审规范
团队协作规范
团队协作规范是确保多人协同开发时保持代码一致性和可维护性的关键。前端项目通常涉及多个开发者同时工作,缺乏统一规范会导致代码混乱、冲突频发。
代码风格统一
使用ESLint和Prettier强制统一代码风格。配置示例:
// .eslintrc.js
module.exports = {
extends: ['airbnb', 'prettier'],
rules: {
'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx'] }],
'import/no-unresolved': 'off'
}
};
// .prettierrc
{
"semi": true,
"singleQuote": true,
"tabWidth": 2
}
Git工作流
采用Git Flow分支模型:
main
:生产环境代码develop
:集成开发分支feature/xxx
:功能开发分支hotfix/xxx
:紧急修复分支
提交消息规范:
feat: 添加用户登录功能
fix: 修复按钮点击无效问题
docs: 更新README文档
组件开发规范
-
原子设计原则:
- Atoms:按钮、输入框等基础组件
- Molecules:表单、导航条等组合组件
- Organisms:完整的功能区块
-
组件示例:
// Button.jsx
import PropTypes from 'prop-types';
import styles from './Button.module.scss';
const Button = ({ children, variant = 'primary', ...props }) => (
<button className={`${styles.button} ${styles[variant]}`} {...props}>
{children}
</button>
);
Button.propTypes = {
children: PropTypes.node.isRequired,
variant: PropTypes.oneOf(['primary', 'secondary', 'danger'])
};
会议与评审规范
有效的会议和代码评审能显著提升代码质量和团队协作效率。
每日站会
- 时间:不超过15分钟
- 内容:
- 昨天完成的工作
- 今天计划的工作
- 遇到的阻塞问题
- 规则:
- 站着开会保持高效
- 不深入讨论技术细节
- 问题记录后单独讨论
需求评审会议
- 会前准备:
- 产品经理提前24小时发送需求文档
- 团队成员预先阅读并标记问题
- 会议重点:
- 需求可行性评估
- 技术方案讨论
- 工作量估算
- 输出:
- 确认的需求文档
- 技术方案初稿
- 排期计划
代码评审标准
-
基础检查项:
- 代码是否符合规范
- 是否有明显的性能问题
- 测试覆盖率是否达标
- 文档是否更新
-
评审示例:
// 改进前
function getUserData(id) {
return fetch(`/api/user/${id}`)
.then(res => res.json())
.then(data => {
console.log(data);
return data;
});
}
// 改进建议
async function getUserData(id) {
try {
const response = await fetch(`/api/user/${id}`);
if (!response.ok) throw new Error('Network response was not ok');
return await response.json();
} catch (error) {
console.error('Fetch error:', error);
throw error;
}
}
- 评审流程:
- 创建Pull Request
- 添加至少2名评审者
- 解决所有评论后才能合并
- 使用
resolve conversation
标记已解决的问题
技术方案评审
-
评审内容:
- 架构设计合理性
- 第三方库选型依据
- 性能优化措施
- 兼容性考虑
- 错误处理机制
-
方案示例:
## 状态管理方案选择
### 需求背景
- 应用需要管理复杂的全局状态
- 多个组件需要共享状态
- 需要支持时间旅行调试
### 方案对比
| 方案 | 优点 | 缺点 |
|------------|-----------------------|---------------------|
| Redux | 成熟生态,时间旅行 | 样板代码多 |
| MobX | 编写简单,响应式 | 过于魔法,调试困难 |
| Context API| 原生支持,轻量 | 性能问题 |
### 推荐方案
采用Redux Toolkit简化Redux使用:
1. 减少样板代码
2. 内置Immer处理不可变数据
3. 完整的TypeScript支持
回溯会议
- 频率:每两周一次
- 流程:
- 列出期间完成的主要工作
- 识别做得好的方面
- 找出需要改进的问题
- 制定具体的改进措施
- 示例输出:
- 好的实践:代码评审响应时间缩短至4小时内
- 待改进:测试覆盖率从65%提升到80%
- 行动项:增加单元测试培训课程
文档规范
完善的文档能降低新成员上手成本,保证知识传承。
项目README
必备内容结构:
# 项目名称
## 项目简介
- 功能概述
- 技术栈说明
## 开发环境
1. Node版本:14.x
2. 安装依赖:`npm install`
3. 启动命令:`npm start`
## 构建部署
- 测试环境:`npm run build:staging`
- 生产环境:`npm run build:prod`
## 代码规范
- ESLint规则
- 提交消息格式
组件文档
使用Storybook或自定义文档:
// Button.stories.jsx
import Button from './Button';
export default {
title: 'Components/Button',
component: Button,
};
const Template = (args) => <Button {...args} />;
export const Primary = Template.bind({});
Primary.args = {
children: 'Primary Button',
variant: 'primary'
};
API文档规范
使用Swagger或类似工具:
paths:
/api/user/{id}:
get:
tags:
- User
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
'200':
description: 用户信息
content:
application/json:
schema:
$ref: '#/components/schemas/User'
质量保障措施
自动化测试
- 单元测试示例(Jest):
// utils.test.js
import { formatDate } from './utils';
describe('formatDate', () => {
it('formats ISO date string correctly', () => {
const result = formatDate('2023-05-15T00:00:00Z');
expect(result).toBe('2023年5月15日');
});
it('handles invalid date', () => {
const result = formatDate('invalid-date');
expect(result).toBe('日期无效');
});
});
- E2E测试(Cypress):
// login.spec.js
describe('Login', () => {
it('successfully logs in', () => {
cy.visit('/login');
cy.get('#username').type('testuser');
cy.get('#password').type('password123');
cy.get('button[type="submit"]').click();
cy.url().should('include', '/dashboard');
});
});
持续集成
GitHub Actions配置示例:
name: CI Pipeline
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
- run: npm ci
- run: npm test
- run: npm run build
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- run: npm ci
- run: npm run lint
工具与资源配置
开发工具推荐
-
编辑器配置:
- VS Code推荐插件:
- ESLint
- Prettier
- EditorConfig
- GitLens
- VS Code推荐插件:
-
调试工具:
- React Developer Tools
- Redux DevTools
- Chrome Performance Monitor
团队知识库
-
内容分类:
- 技术决策记录(ADR)
- 常见问题解答
- 最佳实践指南
- 学习资源
-
ADR示例:
# 2023-05-01 采用TypeScript
## 状态
已采纳
## 背景
JavaScript的类型问题导致多次生产环境bug
## 决策
从2023年Q2开始,所有新项目使用TypeScript开发
## 影响
- 初期学习成本增加
- 类型定义需要额外工作量
- 长期维护成本降低
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn