转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Why Pascal is Not My Favourite Programming Language         ★★★★

Why Pascal is Not My Favourite Programming Language

作者:闵涛 文章来源:闵涛的学习笔记 点击数:4904 更新时间:2009/4/23 18:38:49
''''' This botch is the biggest single problem with Pascal. I believe that if it could be fixed, the language would be an order of magnitude more usable. The proposed ISO standard for Pascal 13 provides such a fix (``conformant array schemas''''''''), but the acceptance of this part of the standard is apparently still in doubt. 2.2. There are no static variables and no initialization A static variable (often called an own variable in Algol-speaking countries) is one that is pri vate to some routine and retains its value from one call of the routine to the next. De facto, For tran variables are internal static, except for COMMON;# in C there is a static declaration that can be applied to local variables. __________________ # Strictly speaking, in Fortran 77 one must use SAVE to force the static attribute. Pascal has no such storage class. This means that if a Pascal function or procedure intends to remember a value from one call to another, the variable used must be external to the function or procedure. Thus it must be visible to other procedures, and its name must be unique in the larger scope. A simple example of the problem is a random number generator: the value used to compute the current output must be saved to compute the next one, so it must be stored in a vari able whose lifetime includes all calls of the random number generator. In practice, this is typi cally the outermost block of the program. Thus the declaration of such a variable is far removed from the place where it is actually used. One example comes from the text formatter described in Chapter 7 of Tools. The variable dir controls the direction from which excess blanks are inserted during line justification, to obtain left and right alternately. In Pascal, the code looks like this: program formatter (...); var dir : 0..1; { direction to add extra spaces } . . . procedure justify (...); begin dir := 1 - dir; { opposite direction from last time } ... end; ... begin { main routine of formatter } dir := 0; ... end; The declaration, initialization and use of the variable dir are scattered all over the program, liter ally hundreds of lines apart. In C or Fortran, dir can be made private to the only routine that needs to know about it: ... main() { ... } ... justify() { static int dir = 0; dir = 1 - dir; ... } There are of course many other examples of the same problem on a larger scale; functions for buffered I/O, storage management, and symbol tables all spring to mind. There are at least two related problems. Pascal provides no way to initialize variables stati cally (i.e., at compile time); there is nothing analogous to Fortran''''s DATA statement or initializers like int dir = 0; in C. This means that a Pascal program must contain explicit assignment statements to initialize variables (like the dir := 0; above). This code makes the program source text bigger, and the program itself bigger at run time. Furthermore, the lack of initializers exacerbates the problem of too-large scope caused by the lack of a static storage class. The time to initialize things is at the beginning, so either the main routine itself begins with a lot of initialization code, or it calls one or more routines to do the initializations. In either case, variables to be initialized must be visible, which means in effect at the highest level of the hierarchy. The result is that any variable that is to be initialized has glo bal scope. The third difficulty is that there is no way for two routines to share a variable unless it is declared at or above their least common ancestor. Fortran COMMON and C''''s external static stor age class both provide a way for two routines to cooperate privately, without sharing informa tion with their ancestors. The new standard does not offer static variables, initialization or non-hierarchical commu nication. 2.3. Related program components must be kept separate Since the original Pascal was implemented with a one-pass compiler, the language believes strongly in declaration before use. In particular, procedures and functions must be declared (body and all) before they are used. The result is that a typical Pascal program reads from the bottom up -- all the procedures and functions are displayed before any of the code that calls them, at all levels. This is essentially opposite to the order in which the functions are designed and used. To some extent this can be mitigated by a mechanism like the #include facility of C and Ratfor: source files can be included where needed without cluttering up the program. #include is not part of standard Pascal, although the UCB, VU and Whitesmiths compilers all provide it. There is also a forward declaration in Pascal that permits separating the declaration of the function or procedure header from the body; it is intended for defining mutually recursive proce dures. When the body is declared later on, the header on that declaration may contain only the function name, and must not repeat the information from the first instance. A related problem is that Pascal has a strict order in which it is willing to accept declara tions. Each procedure or function consists of label label declarations, if any const constant declarations, if any type type declarations, if any var variable declarations, if any procedure and function declarations, if any begin body of function or procedure end This means that all declarations of one kind (types, for instance) must be grouped together for the convenience of the compiler, even when the programmer would like to keep together things that are logically related so as to understand the program better. Since a program has to be presented to the compiler all at once, it is rarely possible to keep the declaration, initialization and use of types and variables close together. Even some of the most dedicated Pascal supporters agree: 14 ``The inability to make such groupings in structuring large programs is one of Pascal''''s most frustrating limitations.'''''''' A file inclusion facility helps only a little here. The new standard does not relax the requirements on the order of declarations. 2.4. There is no separate compilation The ``official'''''''' Pascal language does not provide separate compilation, and so each imple mentation decides on its own what to do. Some (the Berkeley interpreter, for instance) disallow it entirely; this is closest to the spirit of the language and matches the letter exactly. Many others provide a declaration that specifies that the body of a function is externally defined. In any case, all such mechanisms are non-standard, and thus done differently by different systems. Theoretically, there is no need for separate compilation -- if one''''s compiler is very fast (and if the source for all routines is always available and if one''''s compiler has a file inclusion facility so that multiple copies of source are not needed), recompiling everything is equivalent. In practice, of course, compilers are never fast enough and source is often hidden and file inclusion is not part of the language, so changes are time-consuming. Some systems permit separate compilation but do not validate consistency of types across the boundary. This creates a giant hole in the strong typing. (Most other languages do no cross compilation checking either, so Pascal is not inferior in this respect.) I have seen at least one paper (mercifully unpublished) that on page n castigates C for failing to check types across sepa rate compilation boundaries while suggesting on page n+1 that the way to cope with Pascal is to compile procedures separately to avoid type checking. The new standard does not offer separate compilation. 2.5. Some miscellaneous problems of type and scope Most of the following points are minor irritations, but I have to stick them in somewhere. It is not legal to name a non-basic type as the literal formal parameter of a procedure; the following is not allowed: procedure add10 (var a : array [1..10] of integer); Rather, one must invent a type name, make a type declaration, and declare the formal parameter to be an instance of that type: type a10 = array [1..10] of integer; ... procedure add10 (var a : a10); Naturally the type declaration is physically separated from the procedure that uses it. The disci pline of inventing type names is helpful for types that are used often, but it is a distraction for things used only once. It is nice to have the declaration var for formal parameters of functions and procedures; the procedure clearly states that it intends to modify the argument. But the calling program has no way to declare that a variable is to be modified -- the information is only in one place, while two places would be better. (Half a loaf is better than none, though -- Fortran tells the user nothing about who will do what to variables.) It is also a minor bother that arrays are passed by value by default -- the net effect is that every array parameter is declared var by the programmer more or less without thinking. If the var declaration is inadvertently omitted, the resulting bug is subtle. Pascal''''s set construct seems like a good idea, providing notational convenience and some free type checking. For example, a set of tests like if (c = blank) or (c = tab) or (c = newline) then ... can be written rather more clearly and perhaps more efficiently as if c in [blank, tab, newline] then ... But in practice, set types are not useful for much more than this, because the size of a set is strongly implementation dependent (probably because it was so in the original CDC implementa tion: 59 bits). For example, it is natural to attempt to write the function isalphanum(c) (``is c alphanumeric?'''''''') as { isalphanum(c) -- true if c is letter or digit } function isalphanum (c : char) : boolean; begin isalphanum := c in [''''a''''..''''z'''', ''''A''''..''''Z'''', ''''0''''..''''9''

上一页  [1] [2] [3] [4] [5]  下一页


[Web开发]您试图从目录中执行CGI、ISAPI 或其他可执行程序,…  [Web开发]NET操作IIS(添加虚拟目录、更改虚拟目录属性、删…
[Web开发]Visual Studio.Net鲜为人知的技巧  [Web开发]如何给动态的或静态的CheckBoxList多选钮添加选择…
[Web开发]Vista系统IIS7设置全攻略  [Web开发]长篇大论—图文解说DridView、DataList、DetailsV…
[Web开发]抛弃IIS,青睐Apache Web服务器的配置方法  [Web开发]启动IIS提示另一个程序正在使用此文件的原因及解决…
[Web开发]ASP.NET环境下如何使用ArrayList和Hashtable  [网页制作]CSS之Lists及Classification对象说明、例子
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台