我发现很多程序员,写代码都是不规范的。
新手程序员,写代码不规范,是因为在一开始的时候,就没有一个人告诉他正确的代码格式是什么样子的。等他写了几年之后,成了老程序员,那么就是写代码不规范的老程序员了。
如果我招到这样的程序员,如果他不能按我的要求改正,一般是过不了试用期的。
我的建议是,一开始就把代码写规范。不要觉得自己的习惯大于规范,这是不正确的。尤其是在工作中,如果是你自己,不参与到团队,也不参加工作。纯粹就是自己写,自己看,那么随便都无所谓。
几个通用规范
以下列出的和开发语言无关,只是用lua作为示例:
- 等号两边加空格
local a = 12
local b = "string"
- 比较符两边要加空格
if a == b then
print("--------------> a == b")
end
- 逻辑块的代码要换行,并且缩进
if true then
print("------------->一层缩进")
while false do
print("------------->二层层缩进")
end
end
- 缩进要使用空格,不要使用制表符,这个一般在编辑器里都会有设置,当按下 TAB 键的时候,会自动的转换为空格
sublime的一般设置
{
"color_scheme": "Packages/Color Scheme - Default/Monokai.sublime-color-scheme",
"default_line_ending": "unix",//行结束符号
"ensure_newline_at_eof_on_save": true,//保存时,添加最后一行为空行
"expand_tabs_on_save": true,
"font_face": "consolas",
"font_size": 22,
"highlight_line": true,//高亮显示行
"highlight_modified_tabs": true,
"ignored_packages":
[
"Vintage",
],
"line_padding_bottom": 5,
"show_encoding": true,//显示文件编码
"show_line_endings": true,//显示行结束符号类型
"tab_size": 4,//一个tab是4个空格
"theme": "Adaptive.sublime-theme",
"translate_tabs_to_spaces": true,//自动转换tab为空格
"trim_trailing_white_space_on_save": true,//保存时自动去除行尾的空格
}
sublime中lua格式化的插件
插件只是起辅助作用,我是不建议程序员使用。但是这个插件给策划使用是非常不错的,以下是github的地址。