什么是"声明格式"?

Joe*_*nin 14 coding-style

十二因素应用说:

使用声明性格式进行设置自动化,以最大限度地减少新开发人员加入项目的时间和成本

"声明格式"是什么意思?

Cla*_*diu 7

声明性格式是指声明程序的意图/最终结果,而不是它应该如何到达的格式.本质上,它是配置文件和一段代码之间的区别.相比:

CC=gcc
CFLAGS=-I.
DEPS = hellomake.h

%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

hellomake: hellomake.o hellofunc.o 
    gcc -o hellomake hellomake.o hellofunc.o -I.
Run Code Online (Sandbox Code Playgroud)

与(python-pseudocode)类似的东西:

CC = "gcc"
CFLAGS = "-I."
DEPS = ["hellomake.h"]
def make_o_file(o_file, deps):
    #don't recompile if it exists & is current
    c_file = "%s.c" % os.path.splitext(o_file)[0]
    if (os.path.exists(o_file) 
            and is_newer(o_file, c_file) 
            and all(is_newer(o_file, dep) for dep in deps)):
        return

    #else actually compile it
    compile_c_file(CC, code_file, o_file, CFLAGS)

if target == "hellomake":
    make_o_file("hellomake.o", DEPS)
    make_o_file("hellofunc.o", DEPS)
    link_o_files("hellomake", ["hellomake.o", "hellofunc.o"])
Run Code Online (Sandbox Code Playgroud)

如果格式设计合理,前者可以更容易地处理.关于声明性编程的维基百科可能很有用.

  • 我不确定这些示例是否经过精心选择。这些示例希望每个人都能理解一些奇怪的语法。即使对于像我这样有经验的开发人员,也很难不重新阅读它们就很难理解两个示例之间的差异。 (4认同)