Web开发中的编码约定

Coding conventions in web development Today I would like to share with you a grain of my experience and we’ll talk about coding conventions. Every person who writes code for a long time, somehow comes to the question of standardization of the writing style of the code. I think that each of you have already faced the fact that different projects can be written using different rules and styles, and sometimes it’s annoying, and you want some standardization. In other words, coding conventions are a set of guidelines and rules for a certain programming language that recommend programming style, methods and practices for each case of a piece script written in that language. These conventions usually cover such aspects like comments, folders and files organization, indentation, declarations, white space, naming conventions, programming practices and principles, architectural best practices, and so on. We advise you to follow these rules when you write code, it will help to improve the readability of your code and make maintenance easier.

Web开发中的编码约定今天,我想与大家分享我的经验,我们将讨论编码约定。 每个长时间编写代码的人都以某种方式提出了代码编写风格的标准化问题。 我认为你们每个人都已经面临这样一个事实,即可以使用不同的规则和样式编写不同的项目,有时这很烦人,并且您需要一些标准化。 换句话说,编码约定是某种编程语言的一组准则和规则,它们针对用该语言编写的片断脚本的每种情况推荐编程风格,方法和实践。 这些约定通常涵盖诸如注释,文件夹和文件组织,缩进,声明,空白,命名约定,编程实践和原则,体系结构最佳实践等方面。 我们建议您在编写代码时遵循这些规则,这将有助于提高代码的可读性并简化维护。

Most of our examples are given in PHP language, but it is easily applicable to all other web languages

我们的大多数示例都是用PHP语言给出的,但它很容易适用于所有其他网络语言

1.制表 (1. Tabulation)

In the beginning, I would recommend you to decide this question, what to use – tabs or spaces? In general, indentation is not a requirement of most programming languages, where it is used as secondary notation. Rather, developers indent to better convey the structure of their programs to human readers. In particular, indentation is used to show the relationship between control flow constructs such as conditions or loops and code contained within and outside them.

首先,我建议您决定这个问题,使用什么–制表符或空格? 通常,缩进并不是大多数编程语言的要求,在大多数语言中,缩进都用作辅助表示法。 而是,开发人员倾向于将程序的结构更好地传达给人类读者。 特别是,缩进用于显示控制流构造(例如条件或循环)以及它们内部和外部包含的代码之间的关系。

I never really understood the Tabs vs Spaces debate. But anyway, I can recommend you to use four spaces instead of tab. I have a helpful argument about that: when we view and compare codes using different software (for example, we compare two files in Total Commander), tabs and spaces look differently. Just try to indent any two lines of code, the first one with tab, the second with 4 spaces. It will look the same in Notepad++. But then, try to compare this file with it’s backup version (in Total Commander), and you will notice that the tab is longer than four spaces (in the result, we can get badly formatted code). Thus, please adjust your program to use four spaces instead of tab when you push the ‘TAB’ key. It will help you in the future.

我从不真正了解Tabs vs Spaces辩论。 但是无论如何,我可以建议您使用四个空格而不是制表符。 关于这一点,我有一个有用的论据:当我们使用不同的软件查看和比较代码(例如,我们在Total Commander中比较两个文件)时,制表符和空格看起来不同。 只需尝试缩进任意两行代码,第一行使用制表符,第二行使用4个空格。 在Notepad ++中,外观相同。 但是,然后,尝试将此文件与其备份版本(在Total Commander中)进行比较,您会注意到选项卡的长度超过四个空格(结果,我们可能会得到格式错误的代码)。 因此,请在按“ TAB”键时将程序调整为使用四个空格而不是Tab。 它将对您将来有所帮助。

2.班级名称 (2. Class names)

To avoid repetition of names and to make more unique classes written by developer, the class names must begin with a certain prefix and each word begin with a capital letter. Example:

为了避免名称重复和使开发人员编写更多独特的类,类名称必须以特定的前缀开头,并且每个单词都以大写字母开头。 例:


class MyClassName {
    function MyClassName() {
    }
}

class MyClassName {
    function MyClassName() {
    }
}

3.变量名 (3. Variable names)

Because the strict data typing (in most web languages) is missing (although it is still available), for the convenience, the variable name should begin with a lowercase letter of the first character in the name of a particular data type prefix.

由于缺少严格的数据键入(在大多数Web语言中)(尽管仍然可用),为方便起见,变量名称应以特定数据类型前缀的名称中第一个字符的小写字母开头。

Possible type prefixes:

可能的类型前缀:

  • i: integer

    i:整数
  • f: float / double

    f:浮动/双
  • s: string

    s:字符串
  • a: array

    a:数组
  • o: object

    o:对象
  • r: resource

    r:资源
  • b: boolean

    b:布尔值
  • is: boolean

    是:布尔值

After the prefix, we can use Camel style (every word used in the Variable name must begin with a capital letter). If you need to declare a private variable, you can use the underscore. Examples (PHP code):

在前缀之后,我们可以使用驼峰样式(变量名称中使用的每个单词都必须以大写字母开头)。 如果需要声明私有变量,则可以使用下划线。 示例(PHP代码):


public $sFirstName = "some value";
public $sSecondName = "some value";
private $_iVariable;
$iDigit1 = $iDigit2 = 10;
$oMyClass = new MyClass();

public $sFirstName = "some value";
public $sSecondName = "some value";
private $_iVariable;
$iDigit1 = $iDigit2 = 10;
$oMyClass = new MyClass();

4.常量名称 (4. Constant names)

Constants are static variables, it means that its values won’t be changed. As usual, we use all capital letters for constants (don’t forget about possible prefixes). Example:

常量是静态变量,这意味着其值不会更改。 像往常一样,我们使用所有大写字母作为常量(不要忘记可能的前缀)。 例:


define('PRJ_WEBSITE_URL', 'http://www.website.com/');
define('PRJ_GLOBAL_CURRENCY', '$');

define('PRJ_WEBSITE_URL', 'http://www.website.com/');
define('PRJ_GLOBAL_CURRENCY', '$');

5.函数声明(名称和格式) (5. Function Declaration (names and formatting))

All function names should begin with a lowercase letter, and every word should begin with a capital letter. Curly braces should be given on the same line (after the list of parameters). Example:

所有函数名称均应以小写字母开头,每个单词均应以大写字母开头。 括号应在同一行上(在参数列表之后)。 例:


function getPropertyValue($sName) {
    // your custom code is here
}
function setPropertyValue($sName, $sValue) {
    // your custom code is here
}

function getPropertyValue($sName) {
    // your custom code is here
}
function setPropertyValue($sName, $sValue) {
    // your custom code is here
}

6.特殊结构格式 (6. Special structures formatting)

Special structures, for instance: if/else, for, foreach, while and so on, should follow next rules:

特殊结构,例如:if / else,for,foreach,while等,应遵循以下规则:

  • There should be a space between the name of the structure and the following parentheses (for better readability)

    结构名称和以下括号之间应有一个空格(以提高可读性)
  • Conditions which are concluded within the brackets should be separated with space (for better readability)

    括号内得出的条件应以空格分隔(以提高可读性)
  • There is no space after the left bracket and before the right bracket

    左括号后和右括号前没有空格
  • The open curly brace is on the same line

    大括号在同一行
  • Inner conditions (cases or other code) should be indented with the tab

    内部条件(案例或其他代码)应在标签中缩进

Examples:

例子:


foreach ($aKeys as $iKey => $sValue) {
    // your custom code is here
}
if ($bCondition) {
    // your custom code is here
} else {
    // your custom code is here
}
switch ($sKey) {
    case 1:
        // your custom code is here
        break;
    case 2:
        // your custom code is here
        break;
    default:
        // your custom code is here
        break;
}

foreach ($aKeys as $iKey => $sValue) {
    // your custom code is here
}
if ($bCondition) {
    // your custom code is here
} else {
    // your custom code is here
}
switch ($sKey) {
    case 1:
        // your custom code is here
        break;
    case 2:
        // your custom code is here
        break;
    default:
        // your custom code is here
        break;
}

7.数据库表名称 (7. Database tables names)

When you create a table, use logically comprehensible prefixes, and separate the words with an underscore. The use of upper case letters – not necessarily. The same applies to the table fields. Example:

创建表时,请使用逻辑上可理解的前缀,并用下划线分隔单词。 使用大写字母-不一定。 表字段也是如此。 例:


CREATE TABLE IF NOT EXISTS `myprj_records_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pid` int(11) NOT NULL,
  `snippet` varchar(128) NOT NULL,
  `description` varchar(255) NOT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `myprj_records_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pid` int(11) NOT NULL,
  `snippet` varchar(128) NOT NULL,
  `description` varchar(255) NOT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

8.项目的目录结构 (8. The directory structure of the project)

When we work in a team it is important to keep the structure of folders and files in a logical order. Don’t drop everything in a single folder without any organization, it would be a mess. Example:

当我们在团队中工作时,重要的是要使文件夹和文件的结构保持逻辑顺序。 不要在没有任何组织的情况下将所有内容都放在一个文件夹中,那会很混乱。 例:


/root  folder
  /backup
  /cache
  /classes
  /css
  /js
  /media
    /images
    /mp3
    /video
  index.php
  otherFiles.php

/root  folder
  /backup
  /cache
  /classes
  /css
  /js
  /media
    /images
    /mp3
    /video
  index.php
  otherFiles.php

结论 (Conclusion)

In the end, I would like to say that I can not force you to precisely follow all these rules, I can only advise. In fact, many dev teams prepare and use their own coding convention directives and guidelines, but in any event, our guidelines will be useful to everyone. I hope you enjoy our article. It would be kind of you to share it with your friends. Good luck and welcome back!

最后,我只能说,我不能强迫您严格遵守所有这些规则。 实际上,许多开发团队都准备并使用他们自己的编码约定指令和准则,但是无论如何,我们的准则对所有人都有用。 希望您喜欢我们的文章。 您希望与朋友分享。 祝你好运,欢迎回来!

翻译自: https://www.script-tutorials.com/coding-conventions-in-web-development/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值