美文网首页
声明式编程和命令式编程的理解

声明式编程和命令式编程的理解

作者: 高冷的咸鱼 | 来源:发表于2017-12-30 02:26 被阅读0次

    1 简介

    声明式编程 Declarative Programing

    In computer science, declarative programming is a programming paradigm — a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.

    This is in contrast with imperative programming, which implements algorithms in explicit steps.

    命令式编程 Imperative Programming

    In computer science, imperative programming is a programming paradigm that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how a program operates.

    The term is often used in contrast to declarative programming, which focuses on what the program should accomplish without specifying how the program should achieve the result.

    以上的内容摘自维基百科:

    https://en.wikipedia.org/wiki/Declarative_programming
    https://en.wikipedia.org/wiki/Imperative_programming

    因为百度以及维基百科中文对这两者介绍的太随便了,所以还是上了英文版的要保险一些。比如百度百科 https://baike.baidu.com/item/%E5%A3%B0%E6%98%8E%E5%BC%8F%E7%BC%96%E7%A8%8B/9939512?fr=aladdin 中提到了两者的关系是“对立”。而维基百科中使用了“contrast”这个词,也就是说只是两者做了“对比”,在我看来这两者的差别还是很大的。


    简单介绍一下这两个东西。两者其实都是Programming Paradigm,也就是编程范式。

    声明式编程主要关注“我想要什么”,而不关注具体该怎么实现。

    命令式编程主要关注“让计算机应该如何做”,计算机会严格遵循你的指令,而不理会最后的结果是不是你所想要的。

    2 两者的对立和统一

    从字面意思可以看出来,声明式编程,在于声明了我要什么,而命令式编程,在于命令计算机如何去做

    举例说明,我需要实现传入年月日,得到对应日期是星期几。

    // C Language Imperative Programming
    
    #include <time.h>  
    #include <stdio.h>  
      
    int main()  
    {
        time_t t;
        struct tm *lt;
        
        int y, m, d;
        int w;
        char *weekday[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        
        time (&t);              //获取Unix时间戳
        lt = localtime (&t);    //转为时间结构
        
        y = lt -> tm_year + 1900;
        m = lt -> tm_mon;
        d = lt -> tm_mday;
        
      if (m==1 || m==2)
      {
          m=(m==1?13:14);
          y=y-1;              //此处表示把1,2月计算到上一年的13,14月
        }
        
        w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1) % 7;
      printf("%s\n",weekday[w]);
      
        return 0;  
    }
    
    // javaScript Language Declarative Programing
    
    var weeks = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    
    var week = weeks[new Date().getDay()]
    
    console.log(week);
    
    

    以上两份代码分别使用了C语言和JavaScript语言实现了获取当前日期的星期并打印。

    可以看出,在C语言中,我们首先要获取到时间戳,再把时间戳转换为时间结构体,再从结构体获取到年月日,再使用吉姆拉尔森公式计算星期,由于公式的特性,我们还需要将每年的1月和2月看作为前一年的13月和14月以便于公式计算。在这个过程中,我们消耗了大量的精力去考虑如何正确的获取当前的星期数,告诉程序一步一步该怎么处理。

    在js语言中,我们只需要通过Date().getDay(),就可以直接获取到当天的星期数,我们只告诉了程序我们要什么,而至于程序怎么计算出来就不用管了。

    这两者,就是声明式和命令式的对立。一个专注于结果,一个专注于过程。

    当然,其实叫对立是不合适的,两者之间也有统一。比如说,我们把C语言中的main()函数打包为函数int getWeek(),其中返回值为0到6,对应周末到周一。这样,我们需要在主函数获取日期的时候就变成了

    #include <stdio.h>  
      
    int main()
    {
        char *weekday[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        
        int week = getWeek();
        
        printf("%s\n",weekday[week]);
        
        return 0;
    }
    

    个人对这两者的理解,套用一句话,世上本没有路,走的人多了,也就成了路。也就是说所谓的声明式编程,只关注结果,是因为已经有了支持了解决问题的实现。已经有了路,就不用我们自己去修路了。我们发挥拿来主义,直接用就好了,至于它们怎么实现的,封装好的库、架包、模组等等已经帮我们考虑了。我们不用花费大量的精力在如何实现上面,专注于我们想要的,则大大提高了工作效率。

    以上即为本人对声明式编程和命令式编程的理解,若有错误或不足之处,敬请谅解并欢迎指正。

    相关文章

      网友评论

          本文标题:声明式编程和命令式编程的理解

          本文链接:https://www.haomeiwen.com/subject/qccggxtx.html