当前位置:首页 > 教育 >

蛇形命名法和驼峰命名法(驼峰命名和下划线命名)

来源:原点资讯(www.yd166.com)时间:2024-05-17 20:46:39作者:YD166手机阅读>>

概述

英语习惯使用空格来间隔开单词,然而这种用法在编程语言中会带来一些麻烦,所以程序员们就创造出了以下方法: 蛇形命名法(snake case) 驼峰命名法(camel case) 匈牙利命名法(HN case) 帕斯卡命名法(Pascal case) 脊柱命名法(spinal case) 自由命名法(studly caps) 驼峰蛇形命名法。

总体而言,这些命名法都是要克服单词间的空格,从而把不同单词串连起来,最终达到创造出一种新的“单词”的效果。

When it comes to naming resources in a program, there are 3 main types of case conventions: CamelCase, snake_case, and spinal-case. They are just a way of naming the resources to resemble natural language, while avoiding spaces, apostrophes and other exotic characters. This habit is universal in programming languages where only a finite set of characters is authorized for names.

不过主要常用的还是 蛇形命名法(snake case)、驼峰命名法(camel case)和 脊柱命名法(spinal case)

蛇形命名法和驼峰命名法,驼峰命名和下划线命名(1)


一、驼峰命名法

CamelCase has been popularized by the Java language. It intends to emphasize the beginning of each word by making the first letter uppercase. E.g. CamelCase, CurrentUser, AddAttributeToGroup, etc. Aside from debates about its readability, its main drawback is to be ineffective in contexts which are not case sensitive.
Two variants coexist:
lowerCamelCase: where lowercase is used for the first letter.
UpperCamelCase: where the first letter is capital.

用驼峰命名法(camel case)为变量起名字,即第一个单词首字母小写,后面单词首字母大写,例如:fileName、lineNumber。


二、蛇形命名法

snake_case has been widely used for years by C programmers, and more recently in Ruby. Words are separated by underscores “_”, thus letting a compiler or an interpreter understand it as a single symbol, but also allowing readers to separate words fluently. However, its popularity has decreased due to a lot of abuses in C programs with over-extended or too short names. Unlike camel case, there are very few contexts where snake case is not usable. Examples: snake_case, current_user, add_attribute_to_group, etc.

用蛇形命名法(snake case)为变量起名字,即用下划线将单词连接起来,例如:file_name、 line_number。


三、spinal-case

spinal-case is a variant of snake case which uses hyphens “-” to separate words. The pros and cons are quite similar to those of snake case, with the exception that some languages do not allow hyphens in symbol names (for variable, class, or function naming). You may find it referred to as lisp-case because it is the usual way to name variables and functions in lisp dialects. It is also the traditional way of naming folders and files in UNIX and Linux systems. Examples: spinal-case, current-user, add-attribute-to-group, etc.

These 3 cases have their variants, based on criteria such as first letter case, behavior with accents or other special characters. Using English is recommended, to avoid special characters.

spinal case是snake case的变体,它使用连字符“-”来分隔单词,如spinal-case, current-user, add-attribute-to-group等。


后面会分享更多devops和DBA方面内容,感兴趣的朋友可以关注下!

蛇形命名法和驼峰命名法,驼峰命名和下划线命名(2)

,

栏目热文

小驼峰命名法的规则(驼峰命名法分别在什么时候用)

小驼峰命名法的规则(驼峰命名法分别在什么时候用)

说明:AE一般指Adobe After Effects。大小写规范AE表达式是一种区分大小写的语言,可以识别大小写字母之...

2024-05-17 20:48:29查看全文 >>

为什么不建议驼峰命名(驼峰命名和下划线命名)

为什么不建议驼峰命名(驼峰命名和下划线命名)

编程是一门需要精确和清晰的语言艺术。在编程过程中,变量、函数、类、模块等都需要被命名,这些命名不仅代表了它们所代表的概念...

2024-05-17 20:47:31查看全文 >>

驼峰命名法的缺点(驼峰命名法和下划线)

驼峰命名法的缺点(驼峰命名法和下划线)

关于变量的命名,这又是一个容易引发程序员论战的话题。如何命名才能更具有可读性、易写性与明义性呢?众说纷纭。本期“Pyth...

2024-05-17 21:04:48查看全文 >>

大驼峰命名法和小驼峰命名法(驼峰命名法和下划线命名法)

大驼峰命名法和小驼峰命名法(驼峰命名法和下划线命名法)

大驼峰:第一个单词大写,其他单词首字母也大写 : MyFirstStudent小驼峰:第一个单词小写,其他单词首字母大写...

2024-05-17 21:04:58查看全文 >>

聊斋女鬼梅三娘(老版聊斋梅三娘)

聊斋女鬼梅三娘(老版聊斋梅三娘)

她曾被称为“最美女鬼”,三任男友皆是影帝,这旺夫腿真是一绝!特别经典的《新聊斋志异之画皮》,剧中饰演主角梅三娘的演员曾黎...

2024-05-17 21:18:26查看全文 >>

驼峰命名法举例(大驼峰命名与小驼峰定义)

驼峰命名法举例(大驼峰命名与小驼峰定义)

前言程序员们为了自己的代码更容易地在同行之间交流,所以多采取统一的可读性比较好的命名方式。目前业界共有四种命名规则:驼峰...

2024-05-17 21:10:04查看全文 >>

小驼峰命名法在线转换(驼峰命名法怎么设置)

小驼峰命名法在线转换(驼峰命名法怎么设置)

JavaScript变量的小驼峰命名法什么是小驼峰命名法JavaScript中变量的小驼峰命名法,一般指的是JavaSc...

2024-05-17 21:13:22查看全文 >>

驼峰命名有什么缺点(驼峰命名和下划线命名)

驼峰命名有什么缺点(驼峰命名和下划线命名)

良好的命名是编写优良程序必不可少的,通俗易懂的命名能够提升代码的可读性,从而提升项目的维护性,下面我来介绍下三种常见的...

2024-05-17 20:51:02查看全文 >>

为什么不允许使用驼峰命名(驼峰命名法好处)

为什么不允许使用驼峰命名(驼峰命名法好处)

什么是 RESTful百度一下 RESTful,查到的资料很多都讲得不清楚,看完了都不知道说的是啥,导致很多人对 RES...

2024-05-17 21:11:26查看全文 >>

驼峰式命名的优缺点(驼峰命名法和下划线命名法)

驼峰式命名的优缺点(驼峰命名法和下划线命名法)

关于今天要讲的,小编想想还有点小激动。那就是关于变量的命名,这个一直都是容易引起程序员论战的话题。如何命名才更具可读性、...

2024-05-17 21:07:18查看全文 >>

文档排行