编码标准的重要性

注:机翻,未校对。


Coding Standards: What Are They and Why Are They Important?

The goal of any software engineering team is to create high-quality software through a fast, efficient, and easily repeatable process. But when shrinking budgets, quickly approaching deadlines, and growing customer demands enter the equation, keeping your development process effective is easier said than done.
任何软件工程团队的目标都是通过快速、高效且易于重复的过程创建高质量的软件。但是,当预算缩减、截止日期的快速临近以及不断增长的客户需求进入等式时,保持开发过程的有效性说起来容易做起来难。

The goal is always to produce high-quality code that’s easy to build on and maintain—another “easier said than done” objective. According to a recent report by CISQ, the estimated cost of poor software quality in the U.S. in 2022 was at least $2.41 trillion.
目标始终是生成易于构建和维护的高质量代码,这是另一个“说起来容易做起来难”的目标。根据 CISQ 最近的一份报告,2022 年美国软件质量差的估计成本至少为 2.41 万亿美元。

To keep code clean and healthy, development teams must adhere to guidelines and best practices that enable them to create better code consistently. We refer to these coding rules, techniques, and accepted best practices as coding standards.
为了保持代码的整洁和健康,开发团队必须遵守准则和最佳实践,使他们能够始终如一地创建更好的代码。我们将这些编码规则、技术和公认的最佳实践称为编码标准。

Let’s take a closer look at coding standards to understand what they are, why they’re essential, and how to introduce them into your development process effectively.
让我们仔细看看编码标准,了解它们是什么,为什么它们是必不可少的,以及如何有效地将它们引入到您的开发过程中。

What Are Coding Standards? 什么是编码标准?

Coding standards, also known as coding guidelines or programming style guides, are rules and conventions that developers follow when writing code. These rules define the code characteristics necessary to maintain a uniform codebase and facilitate collaboration among developers.
编码标准,也称为编码指南或编程风格指南,是开发人员在编写代码时遵循的规则和约定。这些规则定义了维护统一代码库和促进开发人员之间协作所需的代码特征。

Teams implement coding standards to create and maintain clean, readable, secure, bug-free, and efficient code. Every development team should explicitly define its coding standards based on specific team and project characteristics, primarily the company’s industry and the programming language(s) their developers use.
团队实施编码标准来创建和维护干净、可读、安全、无错误和高效的代码。每个开发团队都应该根据特定的团队和项目特征明确定义其编码标准,主要是公司的行业和开发人员使用的编程语言。

When coding standards are properly defined and implemented, your developers, even those who have just joined the team, can easily find their way around your code. In ideal circumstances, you want your source code to look like a single developer is writing, debugging, and maintaining it.
当编码标准得到正确定义和实施时,你的开发人员,即使是那些刚刚加入团队的人,也可以很容易地找到你的代码。在理想情况下,您希望源代码看起来像一个开发人员在编写、调试和维护它。

What Are Coding Conventions? 什么是编码约定?

Coding conventions are programming language-specific guidelines that offer recommendations for keeping your code clean. These recommendations focus mainly on programming style and organization best practices. They help maintain consistency but allow some flexibility.
编码约定是特定于编程语言的准则,它提供了保持代码整洁的建议。这些建议主要侧重于编程风格和组织最佳做法。它们有助于保持一致性,但也允许一定的灵活性。

Different programming languages and communities may have their specific conventions, but here are some common coding conventions that are widely followed across various languages:
不同的编程语言和社区可能有其特定的约定,但这里有一些在各种语言中广泛遵循的常见编码约定:

  • Formatting: Be consistent in formatting throughout the codebase. Use an automated tool or formatter (black for Python, Prettier for JavaScript, etc.) to ensure consistent formatting throughout the codebase.
    格式设置:在整个代码库中保持一致的格式设置。使用自动化工具或格式化程序(Python 为 Black Python,JavaScript 为 Prettier 等)来确保整个代码库的格式一致。

  • Indentation: Use consistent indentation (usually 2 or 4 spaces) to improve readability.
    缩进:使用一致的缩进(通常为 2 或 4 个空格)以提高可读性。

  • Naming Conventions: Use descriptive names for variables, functions, classes, etc. Follow a consistent naming convention (e.g., CamelCase, snake_case) depending on the language. Whatever naming convention you choose to use, stick to it.
    命名约定:对变量、函数、类等使用描述性名称。根据语言的不同,遵循一致的命名约定(例如,CamelCase、snake_case)。无论您选择使用哪种命名约定,请坚持使用。

  • Braces and Parentheses: Use consistent placement of braces and parentheses.

    大括号和圆括号:使用一致的大括号和圆括号位置。

  • Comments: Write clear and concise comments for complex code sections. Avoid unnecessary comments that state the obvious. Be specific and concise. Write enough to provide the proper context, but don’t overexplain. Tools like Better Comments and Mintlify are great for helping you write good comments.

注释:为复杂的代码部分编写清晰简洁的注释。避免陈述显而易见的不必要评论。具体明了。写足够多的内容来提供适当的上下文,但不要过度解释。Better Comments 和 Mintlify 等工具非常适合帮助您撰写好评论。

  • Whitespace: Use whitespace effectively to enhance code readability. Avoid excessive or unnecessary whitespace.

    空格:有效使用空格以增强代码的可读性。避免过多或不必要的空格。

  • Code Line Length: Limit the length of lines to improve readability. Many style guides recommend lines not to exceed 80 or 120 characters.

    代码行长度:限制行的长度以提高可读性。许多样式指南建议行不超过 80 或 120 个字符。

  • Function/Method Length: Keep functions/methods short and focused on a specific task. Long functions should be broken down into smaller, more manageable ones.

    函数/方法长度:保持函数/方法简短,并专注于特定任务。长函数应分解为更小、更易于管理的函数。

  • Error Handling: Implement proper error handling and communicate errors clearly. The goal is to identify and properly respond to errors and exceptions. When you have clear guidelines for reporting, logging, and handling errors, your code becomes more reliable and, over time, more error-proof.

    错误处理:实施正确的错误处理并清楚地传达错误。目标是识别并正确响应错误和异常。当您在报告、记录和处理错误方面有明确的准则时,您的代码会变得更加可靠,并且随着时间的推移,会更加防错。

  • File Organization: Organize code into logical sections or modules. Follow a consistent file and directory structure.
    文件组织:将代码组织成逻辑部分或模块。遵循一致的文件和目录结构。

Once your team agrees upon what coding conventions are important to the organization and officially adopts them, they become an integral part of your coding standards—which is the fundamental difference between them.
一旦你的团队就哪些编码约定对组织很重要达成一致,并正式采用它们,它们就会成为你的编码标准的一个组成部分,这就是它们之间的根本区别。

Coding conventions are recommendations, while coding standards are mandatory, non-negotiable rules your team has agreed to follow strictly.
编码约定是建议,而编码标准是强制性的、不可协商的规则,您的团队已同意严格遵守。

Why Coding Standards Are Important 为什么编码标准很重要

Coding standards help your team create high-quality code and software. But what does high-quality code mean?
编码标准可帮助您的团队创建高质量的代码和软件。但是高质量的代码意味着什么?

High-quality code ensures that your software is safe, secure, and reliable for you and your users. When your code is these three things, you and your team can expect to reap the associated benefits.
高质量的代码可确保您的软件对您和您的用户来说安全可靠。当你的代码是这三件事时,你和你的团队可以期望获得相关的好处。

They Make Upholding Code Quality Easier 它们使维护代码质量变得更加容易

Coding standards set the rules and guidelines that enable your team to create more efficient code that’s easier to read, analyze, maintain, and extend. When code quality is high, every developer in your team, even ones who have just joined, should be able to easily understand any part of your code and work on it independently.
编码标准设置了规则和准则,使您的团队能够创建更高效的代码,这些代码更易于阅读、分析、维护和扩展。当代码质量很高时,团队中的每个开发人员,即使是刚刚加入的开发人员,都应该能够轻松理解代码的任何部分并独立处理。

Another benefit of following standards is reduced code complexity. Less complexity and better readability make code less vulnerable to errors.
遵循标准的另一个好处是降低了代码的复杂性。更低的复杂性和更好的可读性使代码不易出错。

They Make Bug Fixing Easier 它们使错误修复更容易

Consistent and clean code makes it easier for developers to recognize code smells and anti-patterns and troubleshoot code when necessary. It is easier to catch errors and bugs early when your code is well-structured and clean, but it’s also easy to perform code reviews and test your code later in the process, before deployment or in production.
一致且干净的代码使开发人员能够更轻松地识别代码异味和反模式,并在必要时对代码进行故障排除。当您的代码结构良好且干净时,可以更容易及早发现错误和错误,但也很容易在流程后期、部署或生产之前执行代码审查和测试代码。

Coding standards also make life easier for your testers, allowing them to focus on analyzing and reviewing your code’s logic instead of fixing formatting and style errors that can slip through the cracks when coding standards aren’t being followed.
编码标准还可以让您的测试人员更轻松,使他们能够专注于分析和审查代码的逻辑,而不是修复在不遵循编码标准时可能会漏掉的格式和样式错误。

They Help You Stay Compliant 它们可以帮助您保持合规

Compliance is a key consideration when creating coding standards. As software developers, regulatory compliance requirements must be met in almost every industry and region to avoid sanctions and fines.
在创建编码标准时,合规性是一个关键考虑因素。作为软件开发人员,几乎每个行业和地区都必须满足法规遵从性要求,以避免制裁和罚款。

A recent report by Fenergo states that international financial regulators levied $189 million in fines for non-compliance with Anti-Money Laundering (AML) regulations, including Know Your Customer (KYC) and Customer Due Diligence (CDD) in just the first half of 2023.
Fenergo 最近的一份报告指出,仅在 2023 年上半年,国际金融监管机构就因不遵守反洗钱 (AML) 法规而处以 1.89 亿美元的罚款,包括了解您的客户 (KYC) 和客户尽职调查 (CDD)。

Some of the most common regulations teams must take into consideration when defining coding standards include:
在定义编码标准时,团队必须考虑的一些最常见的法规包括:

  • The General Data Protection Regulation (GDPR) law, which is mandatory for all teams that handle the personal data of European Union (EU) citizens. One of the key principles is that software companies must request permission before using or deleting user data.

    《通用数据保护条例》(GDPR) 法律,该法对处理欧盟 (EU) 公民个人数据的所有团队都是强制性的。其中一个关键原则是,软件公司在使用或删除用户数据之前必须请求许可。

  • ISO 27001, jointly established by the International Standardization Organization (ISO) and the International Electromechanical Commission (IEC) to regulate information security management across international companies. It includes detailed regulations for creating, deploying, and managing your software to avoid data breaches and privacy issues.

    ISO 27001,由国际标准化组织 (ISO) 和国际机电委员会 (IEC) 联合制定,旨在规范国际公司的信息安全管理。它包括有关创建、部署和管理软件的详细规定,以避免数据泄露和隐私问题。

  • The Payment Card Industry & Data Security Standard (PCI DSS) is a regulatory framework that must be followed by companies that deal with debit, credit, and other types of payment cards.

    支付卡行业和数据安全标准(PCI DSS)是一个监管框架,处理借记卡、信用卡和其他类型的支付卡的公司必须遵守该框架。

  • The Health Insurance Portability and Accountability Act (HIPAA) requires companies that deal with healthcare data to protect these incredibly sensitive records, like medical histories and other health-related user details.

    健康保险流通与责任法案 (HIPAA) 要求处理医疗保健数据的公司保护这些极其敏感的记录,例如病史和其他与健康相关的用户详细信息。

  • The National Institute of Standards and Technology (NIST) is a regulatory body for organizations from the U.S. and those who do business in the country. Companies must comply with their guidelines for preventing cyberattacks and data breaches.
    美国国家标准与技术研究院 (NIST) 是面向美国组织和在该国开展业务的组织监管机构。公司必须遵守其防止网络攻击和数据泄露的准则。

When your team knows which regulations it must comply with, adhering to coding standards helps ensure that software meets all necessary regulations, reducing the risk of non-compliance issues and associated financial penalties.
当您的团队知道他们必须遵守哪些法规时,遵守编码标准有助于确保软件满足所有必要的法规,从而降低不合规问题和相关经济处罚的风险。

They Help Keep Your Code Secure 它们有助于保护您的代码安全

Many of the above-mentioned regulations provide guidelines for secure coding practices to be adopted by your DevSecOps team as part of your coding standards.
上述许多法规都提供了安全编码实践的指南,作为编码标准的一部分,您的 DevSecOps 团队将采用这些指南。

According to a recent IBM survey, 51% of surveyed organizations planned to increase security investments in 2023, adding that the global average cost of dealing with a data breach in 2023 was $4.45 million.
根据 IBM 最近的一项调查,51% 的受访组织计划在 2023 年增加安全投资,并补充说,2023 年处理数据泄露的全球平均成本为 445 万美元。

Security-related coding standards offer recommendations for implementing secure access control processes so that only authorized persons can access sensitive information and functionalities.
与安全相关的编码标准为实施安全访问控制流程提供了建议,以便只有经过授权的人员才能访问敏感信息和功能。

They also guide your team in properly configuring libraries, frameworks, server environments, and more, to protect sensitive information and limit the chances of coding errors that can lead to vulnerabilities that can be exploited.
它们还指导您的团队正确配置库、框架、服务器环境等,以保护敏感信息并限制编码错误的可能性,这些错误可能导致可被利用的漏洞。

Following coding standards can also help you uncover potential security issues, detecting things like secrets and insecure dependencies if they exist. They also ensure you aren’t using deprecated features or outdated protocols that can lead to breaches.
遵循编码标准还可以帮助您发现潜在的安全问题,检测机密和不安全依赖项(如果存在)等内容。它们还确保您没有使用可能导致违规的已弃用功能或过时的协议。

They Can Reduce Costs and Accelerate Time to Market 它们可以降低成本并加快上市时间

Every organization strives to make its development process faster and more efficient over time to save money and increase revenue. According to a 2020 McKinsey report, companies with quicker and more efficient development cycles grow revenue four to five times faster than those that don’t invest in increasing development velocity and productivity.
随着时间的推移,每个组织都努力使其开发过程更快、更高效,以节省资金并增加收入。根据麦肯锡 2020 年的一份报告,开发周期更快、效率更高的公司的收入增长速度是那些不投资于提高开发速度和生产力的公司四到五倍。

By following coding standards, developers can increase speed and efficiency by avoiding common mistakes that can lead to errors and issues that slow down coding processes. According to our 2024 State of Software Quality report, 58% of developers say not having enough time is the single most common challenge faced during code reviews. Coding standards also help improve readability, making it easier and faster for dev teams to understand, change, and maintain their code.
通过遵循编码标准,开发人员可以避免可能导致错误和减慢编码过程的错误和问题,从而提高速度和效率。根据我们的 2024 年软件质量状况报告,58% 的开发人员表示没有足够的时间是代码审查期间面临的最常见挑战。编码标准还有助于提高可读性,使开发团队能够更轻松、更快速地理解、更改和维护其代码。

Better code quality also increases code reusability, another big time-saver. Highly functional code can also be scaled more efficiently, accelerating and improving collaboration among team members.
更好的代码质量还可以提高代码的可重用性,这是另一个大大节省时间的方法。功能强大的代码也可以更有效地扩展,从而加速和改善团队成员之间的协作。

When code is clean and everyone follows the same standards, it becomes much easier for multiple team members to work on the same projects thanks to the consistency that standards introduce. Coding standards often emphasize maintaining robust documentation, making not just collaboration but onboarding new team members much easier and faster.
当代码干净并且每个人都遵循相同的标准时,由于标准引入的一致性,多个团队成员在相同的项目上工作变得更加容易。编码标准通常强调维护强大的文档,不仅使协作变得更容易、更快速地让新的团队成员入职。

Best Practices for Implementing Coding Standards 实施编码标准的最佳实践

The implementation of coding standards is a very subjective process and needs to be molded to the characteristics and needs of your team.
编码标准的实施是一个非常主观的过程,需要根据团队的特点和需求进行调整。

However, some generally accepted best practices can help you get on the right track and ensure you’re getting the most out of the coding standards you’ve adopted.
但是,一些普遍接受的最佳实践可以帮助您走上正确的轨道,并确保您从所采用的编码标准中获得最大收益。

Be Aware of Industry Standards 了解行业标准

Teams that develop video games and ones that work with healthcare providers have very different compliance, security, and safety requirements, making the coding standards they need to follow just as distinct.
开发视频游戏的团队和与医疗保健提供商合作的团队具有非常不同的合规性、安全性和安全要求,因此他们需要遵循的编码标准也截然不同。

For example, MISRA C/C++ is a coding standard specifically written for the automotive and embedded industries, offering best practices for “the safe and secure application of embedded control systems and standalone software.”
例如,MISRA C/C++ 是专门为汽车和嵌入式行业编写的编码标准,为“嵌入式控制系统和独立软件的安全应用”提供了最佳实践。

Some other common industry-specific standards include:
其他一些常见的行业特定标准包括:

Research, recognize, and follow all relevant and established standards to meeting industry-specific coding requirements that ensure the development of secure, reliable, and compliant software.
研究、识别并遵循所有相关和既定的标准,以满足行业特定的编码要求,确保开发安全、可靠和合规的软件。

Follow Language-Specific Standards 遵循特定于语言的标准

Many coding standards and style guides already exist for most popular programming languages. Here’s a list of some readily-used ones:
对于大多数流行的编程语言,已经存在许多编码标准和风格指南。以下是一些现成的列表:

Be Able to Explain the Intent of Your Standards 能够解释您的标准的意图

Before you begin implementing coding standards, be sure that your team understands why they are important. Your development team’s leadership should take time to summarize the intent of every coding standard so that the team understands the context of each rule.
在开始实施编码标准之前,请确保您的团队了解它们的重要性。开发团队的领导应该花时间总结每个编码标准的意图,以便团队理解每个规则的上下文。

Also, briefly define how each rule is applied in the description. Educate your team on each coding standard you introduce by providing support materials to help them understand the advantages of following each coding standard.
此外,简要定义如何在描述中应用每个规则。通过提供支持材料,让您的团队了解您引入的每个编码标准,以帮助他们了解遵循每个编码标准的优势。

To illustrate their importance more clearly, you can also offer your team real-life examples of what problems could arise from failing to follow coding standards.
为了更清楚地说明它们的重要性,您还可以为您的团队提供现实生活中的例子,说明不遵循编码标准可能会导致什么问题。

Evaluate and Update Standards Regularly 定期评估和更新标准

As your team and product evolve and evolve over time, evaluate your coding standards to determine whether they are still effective or need updating. If the proper due diligence was performed when picking and establishing your coding standards to start with, it’s unlikely that radical and constant changes will ever be necessary.
随着您的团队和产品随着时间的推移而不断发展,请评估您的编码标准,以确定它们是否仍然有效或需要更新。如果在一开始就选择和建立您的编码标准时进行了适当的尽职调查,那么不太可能需要进行彻底和持续的更改。

Industry-specific standards like MISRA C/C++ are updated every few years, as well. Ensure your team is aware of these updates when they occur and adopts any new recommendations that have been introduced.
MISRA C/C++ 等行业特定标准也每隔几年更新一次。确保您的团队在发生这些更新时了解这些更新,并采纳已引入的任何新建议。

Uphold Coding Standards More Easily with Automated Tools 使用自动化工具更轻松地维护编码标准

Embracing coding standards is a fundamental practice that elevates software development by enhancing consistency, readability, and security. Popular coding standards are widely accepted as best practices that have been proven to lead teams toward creating healthy, high-quality code more easily.
采用编码标准是一种基本实践,它通过增强一致性、可读性和安全性来提升软件开发。流行的编码标准被广泛接受为最佳实践,这些实践已被证明可以引导团队更轻松地创建健康、高质量的代码。

However, maintaining coding standards across a project or team can be challenging without the right tools. Static code analysis tools are an excellent place to start. Tools like linters can help you automate the code review and analysis process, making it significantly easier for teams to adhere to established coding standards.
但是,如果没有合适的工具,在整个项目或团队中维护编码标准可能会具有挑战性。静态代码分析工具是一个很好的起点。像 linter 这样的工具可以帮助您自动执行代码审查和分析过程,使团队更容易遵守既定的编码标准。


via:

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值