相约福大——Beta冲刺代码规范

相约福大 2024-11-26 15:00:00
这个作业属于哪个课程2401_CS_SE_FZU社区-CSDN社区云
这个作业要求在哪里团队作业—bate冲刺+事后诸葛亮-CSDN社区
这个作业的目标beta冲刺代码规范
其他参考文献

目录

  • 1. 文件和项目结构
  • 2. 代码格式
  • 3. 注释和文档
  • 4. 命名规范
  • 5. 代码风格
  • 6. 性能考虑
  • 7. 代码复用和模块化
  • 8. 版本控制
  • 9. 测试

1. 文件和项目结构

  • Scripts
    • Assets/Scripts/Controllers/PlayerController.cs
    • Assets/Scripts/Helpers/MathExtensions.cs
    • Assets/Scripts/Managers/GameManager.cs
    • Assets/Scripts/Models/PlayerData.cs

2. 代码格式

  • 缩进和空格

    public class PlayerController : MonoBehaviour
    {
        private int health;
    
        private void Update()
        {
            if (health <= 0)
            {
                Debug.Log("Player is dead");
            }
        }
    
        public void TakeDamage(int amount)
        {
            health -= amount;
        }
    }
    
  • 大括号风格

    if (condition)
    {
        // code
    }
    else
    {
        // code
    }
    
    public class MyClass
    {
        // code
    }
    

3. 注释和文档

  • 文件头注释

    // Author: John Doe
    // Created Date: 2024-01-01
    // Description: Controls player movement and actions
    
  • 方法注释

    /// <summary>
    /// Deals damage to the player.
    /// </summary>
    /// <param name="amount">The amount of damage to deal.</param>
    public void TakeDamage(int amount)
    {
        health -= amount;
    }
    

4. 命名规范

  • 类和接口

    public class PlayerController : MonoBehaviour
    {
        // code
    }
    
  • 方法

    public void UpdateHealth(int amount)
    {
        health = Mathf.Clamp(health + amount, 0, MaxHealth);
    }
    
  • 属性和字段

    private int health;
    private float speed;
    
  • 局部变量

    for (int i = 0; i < items.Length; i++)
    {
        // code
    }
    
  • 常量

    public const int MaxHealth = 100;
    

5. 代码风格

  • **使用var**:

    var player = FindObjectOfType<PlayerController>();
    
  • 避免魔法数字

    public const int MaxHealth = 100;
    
    public void HealPlayer(int amount)
    {
        health += amount;
        health = Mathf.Min(health, MaxHealth);
    }
    
  • 字符串连接

    string playerName = "Player";
    string message = $"Welcome, {playerName}!";
    Debug.Log(message);
    
  • 异常处理

    public void LoadGame()
    {
        try
        {
            // Code that might throw an exception
        }
        catch (Exception ex)
        {
            Debug.LogError("Failed to load game: " + ex.Message);
        }
    }
    

6. 性能考虑

  • 避免在Update中使用

    private void Start()
    {
        StartCoroutine(LoadData());
    }
    
    private IEnumerator LoadData()
    {
        // Simulate data loading with a delay
        yield return new WaitForSeconds(2);
        // Load data
    }
    
  • 对象池

    public class ObjectPool : MonoBehaviour
    {
        public List<GameObject> pool;
        public GameObject prefab;
    
        public GameObject GetObject()
        {
            if (pool.Count > 0)
            {
                return pool[0];
            }
            else
            {
                return Instantiate(prefab);
            }
        }
    
        public void ReturnObject(GameObject obj)
        {
            pool.Add(obj);
        }
    }
    

7. 代码复用和模块化

  • 单一职责原则

    // PlayerInputHandler is responsible for handling input
    public class PlayerInputHandler : MonoBehaviour
    {
        public void MovePlayer(Vector3 direction)
        {
            // Move player logic
        }
    }
    
    // PlayerController is responsible for player state and logic
    public class PlayerController : MonoBehaviour
    {
        private PlayerInputHandler inputHandler;
    
        private void Start()
        {
            inputHandler = GetComponent<PlayerInputHandler>();
        }
    
        private void Update()
        {
            Vector3 moveDirection = GetMoveDirection();
            inputHandler.MovePlayer(moveDirection);
        }
    
        private Vector3 GetMoveDirection()
        {
            // Determine move direction based on input
            return new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        }
    }
    
  • DRY原则

    public static class MathExtensions
    {
        public static int Clamp(int value, int min, int max)
        {
            return Math.Max(min, Math.Min(max, value));
        }
    }
    
    public class PlayerController : MonoBehaviour
    {
        private void UpdateHealth(int amount)
        {
            health = MathExtensions.Clamp(health + amount, 0, MaxHealth);
        }
    }
    

8. 版本控制

  • 提交信息

    git commit -m "Add new feature: player sprinting"

  • 分支策略

    • main:稳定版本,用于发布。
    • develop:开发版本,用于日常开发。
    • feature/xxx:新功能开发。
    • bugfix/xxx:修复bug。

9. 测试

  • 单元测试

    [TestFixture]
    public class PlayerTests
    {
        [Test]
        public void Health_ShouldBeFullAfterRest()
        {
            // Arrange
            Player player = new Player();
            player.Health = 50;
    
            // Act
            player.Rest();
    
            // Assert
            Assert.AreEqual(100, player.Health);
        }
    }
    
  • 代码审查

    定期进行代码审查会议,讨论代码改进和最佳实践。

...全文
70 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

113

社区成员

发帖
与我相关
我的任务
社区描述
202401_CS_SE_FZU
软件工程 高校
社区管理员
  • FZU_SE_TeacherL
  • 助教_林日臻
  • 防震水泥
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧