动态语言在现实生活中的好处?

发布于 2024-07-25 09:07:44 字数 438 浏览 2 评论 0原文

我正在探索开发新系统(网络应用程序)的几种可能性。

我是一个“老式”的人,本质上是面向对象的(多年前从程序转换而来)。 我尝试了 Python 并研究了一点 Ruby,但坦率地说,我又被 Microsoft 的工具(C#、ASP.NET MVC)所吸引。 所有这些运行时输入、基本内容上没有编译器错误等等只会让我在构建大型复杂应用程序时变得更加困难。

我经常听到人们谈论使用动态语言可以做的伟大事情,但除了狗、猫的例子以及如何快速地编写一种很酷的方法来计算事物之外,Visual Studio 的“工业实力”似乎消除了这些动态语言提供了一些巧妙的小东西,特别是现在您拥有 VS 的免费快速版本和免费供初创企业使用的完整版本。

我觉得我在这里遗漏了一些东西,因为大型应用程序确实是用动态语言开发的,那么在查看大型复杂应用程序时,这些语言使您能够做哪些伟大的事情呢? 是什么让你放弃VS的实力?

I'm exploring several possibilities for developing a new system (web application).

I'm an "old fashioned" kinda guy, object oriented in nature (converted from procedural many years ago). I played around with Python and studied a bit Ruby, but frankly I'm attracted back to using Microsoft's tools (C#, ASP.NET MVC). All this run-time typing, no compiler errors on basic stuff, etc just makes my life harder when it comes to building large complex applications.

I constantly hear people speak about the great things you can do with dynamic languages, but except for examples with dogs, cats and how quickly you can code a cool way to count things, the "industrial strength" of Visual Studio just seems to eliminate those neat small things dynamic languages offer, especially now that you have free express versions of VS and full versions available for free for start-ups.

I feel like I'm missing something here, because big applications are indeed being developed with dynamic languages, so what are those great things these languages enable you to do, when looking at large complex applications? What can make you give away the strength of VS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

镜花水月 2024-08-01 09:07:45

考虑这样的情况:您有一个带有单个参数的子例程:

sub calculate( Int $x ){ ... }

现在您的需求发生了变化,并且您必须处理多个参数:

multi sub calculate( Int $x ){ ... }
multi sub calculate( Int @x ){
  my @ret;
  for @x -> $x {
    push @ret, calculate( $x );
  }
  return @ret;
}

请注意,不同版本之间几乎没有变化。

现在,如果您发现确实应该使用浮点数,该怎么办:

multi sub calculate( Num $x ){ ... }
multi sub calculate( Num @x ){
  my @ret;
  for @x -> $x {
    push @ret, calculate( $x );
  }
  return @ret;
}

这是一个比以前更小的更改,并且请注意,调用这些子例程的任何代码段都将继续工作,而无需进行任何更改。

这些示例是用 Perl6 编写的

Consider the case where you have a subroutine that takes a single argument:

sub calculate( Int $x ){ ... }

Now your requirements change, and you have to deal with multiple arguments:

multi sub calculate( Int $x ){ ... }
multi sub calculate( Int @x ){
  my @ret;
  for @x -> $x {
    push @ret, calculate( $x );
  }
  return @ret;
}

Notice that there was very little change between the different versions.

Now what if you found out that you really should have been using floating point numbers:

multi sub calculate( Num $x ){ ... }
multi sub calculate( Num @x ){
  my @ret;
  for @x -> $x {
    push @ret, calculate( $x );
  }
  return @ret;
}

That was a smaller change than before, and note that any piece of code that called these subroutines would continue to work without a single change.

These examples were written in Perl6

悲欢浪云 2024-08-01 09:07:45

我知道已经有一段时间了,但接受的答案列出了不限于动态类型语言的属性。 例如,#4(更少的代码)对于 Scala 也是如此(如果我没记错的话)。 这对于 Groovy 来说绝对是正确的(它构建在 Java 之上,因此从技术上讲,groovy 既是静态的,也是动态的)。 我唯一同意的是

来自维基百科的#7(韧性),

动态编程语言是一种高级编程语言,它在运行时执行静态编程语言在编译期间执行的许多常见编程行为。 这些行为可能包括通过添加新代码、扩展对象和定义或修改类型系统来扩展程序。 动态程序员还可能包括动态类型。

根据我的经验,动态语言的优点是更少的代码/更高效的代码和更好的“最佳”实践(例如强大的测试文化)......这些并不是动态语言特有的。

说到测试文化,某些动态语言爱好者(特别是 Ruby)声称他们编写的所有测试(测试感染)也使他们能够轻松重构应用程序。 我倾向于不相信这种说法——编写得不好的测试(非常非常容易编写)往往会成为维护的噩梦。

总结一下:动态语言往往有助于快速交付产品,应用程序可能易于维护,也可能不易维护,但对于高性能应用程序来说却很糟糕(静态编译的应用程序要快得多)

I know it's been a while but the accepted answer lists properties that aren't limited to dynamically-typed languages. For example, #4 (less code) is also true of Scala (if I recall correctly). It's definitely true of Groovy (which is built on top of Java, so technically groovy is both static and dynamic). The only one I agree with is #7 (resilience)

From wikipedia,

Dynamic programming language is a ... high-level programming language which, at runtime, execute many common programming behaviors that static programming languages perform during compilation. These behaviors could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system. Dynamic programmer may also include dynamic typing.

From my experience, the advantages of dynamic languages are less code/more efficient code and better "best" practices (like strong testing culture)... things which are not specific to dynamic languages.

Speaking of test culture, certain dynamic language enthusiasts (Ruby in particular) claim that all the tests that they write (test infection) allows them to also refactor applications easily. I tend not to buy this claim---poorly written tests (which are very, very easy to write) tend to become a maintenance nightmare.

To summarize: dynamic languages tend to make for quick product delivery, the app may or may not be easy to maintain, but are horrible for high-performance apps (statically compiled apps are way, way faster)

偷得浮生 2024-08-01 09:07:44

“你能多快编码”让我非常担心,所以我很高兴地放弃了漫长而缓慢的编译过程。

动态语言的优点。

  1. 没有编译,没有构建。 只需编码和测试,然后部署到生产环境。

  2. 立即满足。 无需花时间思考 API 调用可能是什么。 只需在 Python >> 中交互式输入即可。 提示并查看它实际做了什么。

  3. 设计周期非常非常短。 我可以只对类进行编码,对它们进行单元测试,然后就可以完成,而不是用额外的接口定义和适当的抽象声明和覆盖来精心制作一个类层次结构。

  4. 更少的代码。 动态语言内省减少了源代码的体积。 我不会在我的应用程序中写这些东西; 我依靠框架来为我做到这一点。 但基于框架的代码往往很短; 没有 Java 中常见的重复声明,您必须在 XML 配置中重复某些内容。

  5. 没有什么神秘的。 正如我们在 Python 社区中所说:“使用源代码,Luke。” 框架的作用或 API 的真正含义没有任何歧义。

  6. 绝对的灵活性。 当我们的需求发生变化时,我们不必与破坏整个架构的毁灭性变化作斗争。 我们可以简单地对一些类进行更改,因为 Python 的 Duck Typing 消除了在我们认为不需要的地方修改缺失的接口定义的需要。 他们只是不存在; 我们没有编写的代码是我们不需要修复或维护的代码。

  7. 韧性。 当我们的精算师脑子有问题时,我们不必花几个月的时间来弄清楚如何将这种新的、更复杂的承保模型集成到应用程序中。 几乎任何东西都可以被挤进去。同样,这完全是鸭子类型的结果。 我们不再需要将其强制安装到无法预测新业务模型的架构中。

  8. 由于源应用程序,因此源可以是它自己的配置文件。 我们没有某些外国语法的 XML 或 INI 配置文件。 我们有 Python 的配置文件。 Django 框架做到了这一点,我们遵循他们的领导。 我们有非常复杂的模型数据声明用于销售演示和单元测试。 超级复杂的数据实际上是来自数据库的 Python 对象的集合——除了——我们省略了加载数据库。 只需调整 Python 对象构造函数而不是加载 SQL 数据库会更简单。

[顺便提一句。 在使用 Cobol、Fortran、PL/I、Java、C、C++ 开发软件 30 多年之后,我只是厌倦了大多数编译语言所需的相对较低级别的手动优化。 几年前,我读过一篇关于大多数编译器效率低下的评论:它引导我们创建复杂的构建系统来解决编译器的限制。 我们只需要 make 因为 cc 太慢了。]


编辑

动态编程并不能让你成为天才。 它只是节省了很多时间。 您仍然需要管理学习过程。 在所有语言中,你不知道的事情都是困难的。 动态语言可以让您逐步进行,一次发现一件新事物,而无需做大量设计​​工作才发现您的假设是错误的,从而为您提供杠杆作用。

如果您想根据被误解的 API 编写大量代码,那么动态语言可以提供帮助。 你可以自由地用一种语言编写大量崩溃和烧毁的代码:C#、VB、C++、Java 或 Python。 你总是可以编写出不起作用的代码。

编译器会提前警告您该代码将无法运行。 通常,不编译是一个很大的提示。 但是,您仍然可以编写大量可以编译但无法通过所有单元测试的代码。 编译器只检查语法,不检查语义。

Python 可以提前警告您代码将无法运行。 通常,您无法让它以交互方式运行。 但是,您仍然可以编写大量无法通过所有单元测试的代码。

"how quickly you can code" so totally concerns me that I happily gave up the long, slow slog through getting things to compile.

Advantages of dynamic languages.

  1. No compile, no build. Just Code and Test followed by deploy to production.

  2. Immediate gratification. No time spent wringing my hands over what an API call might be. Just type it interactively in the Python >>> prompt and see what it actually does.

  3. Very, very short design cycles. Rather than carefully crafting an class hierarchy with bonus interface definitions and proper abstract declarations and overrides, I can just code the classes, unit test them and be done.

  4. Less code. Dynamic language introspection reduces the volume of source. I don't write this stuff in my applications; I depend on frameworks to do this for me. But the framework-based code is often very short; there are no duplicative declarations that are so common in Java, where you have to repeat things in an XML config.

  5. No mysteries. As we say in the Python community: "Use the source, Luke." There's no ambiguity in what a framework does or what an API really means.

  6. Absolute Flexibility. As our requirements change, we don't have to struggle with devastating changes that break the entire architecture. We can -- trivially -- make changes to a few classes because Python's Duck Typing eliminates the need to retrofit missing interface definitions where we didn't think they'd be needed. They're just aren't any; the code we didn't write is code we don't have to fix or maintain.

  7. Resilience. When our actuaries have a brain-fart, we don't have to spend months figuring out how to integrate this new, more sophisticated underwriting model into the apps. Almost anything can be squeezed in. Again, this is strictly a consequence of duck typing. We're freed from force-fitting it into an architecture that couldn't anticipate a new business model.

  8. Since the source is the application, the source can be it's own configuration file. We don't have XML or INI configuration files in some foreign syntax. We have configuration files in Python. The Django framework does this and we follow their lead. We have very complex mocked-up data declarations for sales demo and unit testing. The super-complex data is actually a collection of Python objects that would have come from a database -- except -- we omitted loading the database. It's simpler to just tweak the Python object constructor instead of loading a SQL database.

[BTW. After 30+ years of developing software in Cobol, Fortran, PL/I, Java, C, C++, I'm just tired of the relatively low-level hand-optimization that most compiled languages require. Years ago, I read a comment on the inefficiency of most compilers: it leads us to create elaborate build systems to work around the compiler limitations. We only need make because cc is so slow.]


Edit

Dynamic programming doesn't make you a genius. It just saves a lot of time. You still have to manage the learning process. Things you don't know are hard in all languages. A dynamic language gives you leverage by allowing you to proceed incrementally, uncovering one new thing at a time without having done a lot of design work only to find your assumptions were wrong.

If you want to write a lot of code based on a misunderstood API, then a dynamic language can help. You are free to write a lot of code that crashes and burns in a language: C#, VB, C++, Java or Python. You can always write code which won't work.

The compiler gives you some advance warning that the code won't work. Typically, not compiling is a big hint. However, you can still write a lot of code that compiles and fails all the unit tests. The compiler only checks syntax, not semantics.

Python can give you some advance warning that the code won't work. Typically, you can't get it to run interactively. However, you can still write a lot of code that fails all the unit tests.

探春 2024-08-01 09:07:44

我的经验

我曾与这两家公司共事过,总共大概有十年的专业经验。

有趣的是,我花费了更多的时间尝试让静态类型语言理解我想要做的事情(可能是几周 - 可能总共是几个月),而不是修复动态类型错误引起的错误(可能是每年一两个小时?)。

研究

人们已经非常努力地寻找证据表明其中一种在程序员生产力方面更好,但我读到的最新评论表明没有强有力的证据证明这两种方法。

极权主义

静态类型分析在某些情况下很有用。 我认为它不应该被固有地融入到你的语言中。 它应该是你的交互式计算环境中的一个工具,与你的测试、你的 REPL、你的重构、你的文档、你的文字编码等一起。

静态类型系统可以让你思考有用的事情。 在像 Haskell 这样带有类型类和 Monad 的语言中尤其如此(我承认我仍然没有真正理解)。 这是一件好事,但我觉得相信它总是是一件好事是极权主义的。 你应该在适当的时候考虑一​​下。 一种语言不应该让你从开发之初就思考它或意识到它。

限制太多和限制太多 限制不够 非

图灵完备的静态类型系统的表达能力有限。 为什么要使用一种新的特殊领域特定语言将其融入到语言中,只是为了讨论类型? 现在,您的语言决定了您可以访问的表达能力的确切水平。 想要更多? 您需要重新编写代码或更新语言。 想要更少吗? 你运气不好——使用不同的语言。

相反,为什么不在需要时使用基本动态语言来描述类型和类型系统呢? 作为一个图书馆。 它更具表现力; 更强大(如果愿意的话); 更灵活; 并意味着更小的基础语言。

样板文件

静态类型语言似乎鼓励样板文件或代码生成。 我不确定这是否是固有的。 也许一个足够强大的宏观系统可以克服它。 我正在将 Swift 中测试的模拟状态与 Objective C 中的测试状态进行比较。

整体静态

类型语言似乎鼓励整体应用程序——这是我无法支持的观点和观察,但它似乎成立……它们或它们附带的工具似乎鼓励整体应用程序和思维。

相反,在交互式计算环境中,您不需要构建新的应用程序,而是扩展系统以使其执行更多操作。 我所知道的系统(Lisp 机器、Smalltalk 和 Unix——其工具之间有一个动态类型接口)使用动态类型将各个部件组装在一起。

我们应该为整体构建微小的扩展,而不是着手构建新的整体应用程序,因此这种单一趋势(如果存在)是具有破坏性的。

速度

最快的动态跟踪 JIT 编译器可以生成快速的代码,而且它们仍然是一项相当年轻的技术。 不过,您也可以使用静态类型语言来做到这一点。

长期

我怀疑从长远来看,我们最终会得到支持强大静态分析的环境,您将能够在其中声明类型和协议,并且系统将帮助您查看它们是否满意,或者帮助向您展示隐含类型。 但您不需要这样做。

My Experience

I've worked with both, probably about a decade with each professionally, in all.

I've anecdotally spent far more time trying to make statically typed languages understand what I want to do (probably weeks – perhaps months in total) than I've spent fixing bugs caused by dynamic type errors (perhaps an hour or two a year?).

Studies

People have tried quite hard to get evidence that one or the other is better in terms of programmer productivity, but the most recent review I've read says there's no strong evidence for either.

Totalitarian

Static type analysis is useful in some situations. I don't think it should not be inherently baked in to your language. It should be a tool within your interactive computing environment, along with your tests, your REPL, your refactors, your docs, your literate coding, etc.

Static type systems can make you think about useful things. This is especially true in a language like Haskell with Type Classes and Monads (which I confess I still don't really get). This is a good thing, but I feel it is totalitarian to believe it is always a good thing. You should think about it when appropriate. A language should not make you think about it or be aware of it from the outset of development.

Both Too Restrictive & Not Restrictive Enough

Static type systems that aren't Turing complete have limited expressivity. Why bake that in to the language using a new special domain specific language just for talking about types? Now your language sets the exact level of expressivity you have access to. Want more? You need to re-write your code or update the language. Want less? You're out of luck – use a different language.

Instead, why not use a base dynamic language to describe types and type systems when you want to? As a library. It's more expressive; more powerful (if wished); more flexible; and means a smaller base language.

Boilerplate

Statically typed languages seem to encourage boilerplate or code generation. I'm not sure if this is inherent. Perhaps a sufficiently powerful macro system would overcome it. I'm comparing the state of mocks for testing in Swift with that of objective c.

Monolithic

Statically typed languages seem to encourage monolithic applications – this is an opinion and an observation that I can't back up, but it seems to hold… They, or the tooling they come with, seems to encourage monolithic applications and thinking.

In contrast, in interactive computing environments you don't build a new application, you instead extend out the system so that it does more. The systems I know of (Lisp machines, Smalltalk, and Unix – its tools have a dynamically typed interface between them) use dynamic typing to assemble parts together.

We should be building tiny extensions to a whole, rather than setting out to build new whole applications, so this monolithic tendency, if it exists, is damaging.

Speed

The fastest dynamic tracing JIT compilers produce fast code, and they're still a fairly young technology. Still – you could also do this with a statically typed language.

Long Term

I suspect that long term, we'll end up with environments that support powerful static analysis, where you'll be able to declare types and protocols, and the system will help you to see if they are satisfied, or help show you the implied types. But you won't need to do that.

飘落散花 2024-08-01 09:07:44

静态类型是过早优化的一种形式。 它迫使您预先做出详细决定,而您可能不具备做出这些决定的知识。 除非您创建足够的类型以具有逻辑意义,否则它并不会特别有助于程序的正确性。 这使得动态更改数据结构变得困难。

你从中得到的是非常有限的正确性检查:非常有限,因为它没有区分使用整数的方法。 假设我们正在处理行和列; 两者都可能是整数,但行和列变量不应该互换使用。 您还可以获得优化,这可能是一件非常有用的事情,但不值得放慢初始开发速度。 您可以通过编写适当的测试来弥补正确性检查。

Common Lisp 类型系统对此很有帮助。 所有数据对象都知道它们的类型,如果您愿意,您可以显式指定该类型。

eval 循环类型的执行模型使得在编写例程时测试例程变得非常容易。 您不必预先显式地编写测试(尽管没有什么可以阻止您这样做); 您可以编写它们并即时执行它们(然后您可以将其细化为测试套件 - 将其视为增量测试开发)。

没有很长的构建步骤可以更轻松地进行测试驱动开发,因为运行测试的速度要快得多。 您不必每次想要测试时都中断正在做的事情。

当我听到人们抱怨动态语言时,我想起了人们抱怨没有独占锁的版本控制系统。 有些人需要很长时间才能意识到迁移到现代 VCS 所获得的好处,同样有些人也需要很长时间才能欣赏动态语言。

Static typing is a form of premature optimization. It forces you to make detail decisions up front, when you may not have the knowledge to make them. It doesn't particularly help program correctness unless you create enough types to make logical sense. It makes it difficult to change data structures on the fly.

What you get out of it is a very limited amount of correctness checking: very limited because it doesn't separate ways to use ints, for example. Suppose we're dealing with rows and columns; both are probably ints, but row and column variables shouldn't be used interchangeably. You also get optimization, which can be a very useful thing, but isn't worth slowing down initial development for. You can make up for the correctness checking by writing appropriate tests.

The Common Lisp type system is good for this. All data objects know their type, and you can explicitly specify that type if you like.

An eval loop sort of execution model makes it very easy to test routines as you write them. You don't have to explicitly write tests up front (although there's nothing to stop you from doing that); you can write them and execute them on the fly (and then you can refine that into a test suite - think of it as incremental test development).

Not having a long build step makes it easier to do test-driven development, because it's a whole lot faster to run tests. You don't have to break up what you're doing every time you want to test.

When I hear people complaining about dynamic languages, I'm reminded of people complaining about version control systems without exclusive locks. It takes some people a long time to realize what they gain by moving to a modern VCS, and equally it takes some people a long time to appreciate dynamic languages.

箜明 2024-08-01 09:07:44

一般来说,我更喜欢谈论“交互式”语言而不是“动态”语言。 当您只有一个编辑/编译/运行周期时,任何周转都需要很长时间。 嗯,至少按照“需要保存、编译、检查编译报告、测试运行、检查测试结果”的顺序。

使用交互式语言,通常很容易修改一小部分,然后立即测试结果。 如果您的测试运行仍然需要很长的时间,那么您还没有赢得那么多,但您通常可以在较小的情况下进行测试。 这有利于快速发展。 一旦您有了已知正确的实现,这也有助于优化,因为您可以快速开发和测试新的改进函数并尝试不同的表示或算法。

In general, I prefer talking about "interactive" languages rather than "dynamic" languages. When you only have an edit/compile/run cycle, any turn-around takes a long time. Well, at least on the order of "need to save, compile, check the compilation report, test-run, check test results".

With an interactive language, it's usually easy to modify a small part, then immediately test results. If your test runs still take a lomg time, you haven't won that much, but you can usually test on smaller cases. This facilitates rapid development. Once you have a known-correct implementation, this also helps optimisation, as you can develop and test your new, improved function(s) quickly and experiment with different representations or algorithms.

我三岁 2024-08-01 09:07:44

这是我的回答的一部分 之前的类似问题(我知道 C# . 使用 Python 会提高工作效率吗?):

我自己就有 C#/.NET 背景。 开始在 abt 中进行 .NET 编程。 2001 年,大约在同一时间 Python 被引入。 2001 年,我花在 C# 和 Python 上的时间大约是 90% C# / 10% Python。 现在,这个比例是 5% C# / 95% Python。 在我的公司,我们仍然维护着基于.NET 的产品线。 但所有新东西都是基于 Python 的。

我们已经用 Python 创建了不平凡的应用程序。

根据我的经验,与 C# 相比,Python 的工作效率更高的是:

  • 它是一种动态语言。 使用动态语言通常可以让您从应用程序中删除整个架构层。 Python 的动态特性允许您以比 C# 更自然、更灵活(语法方面)的方式创建可重用的高级抽象。
  • 图书馆。 社区提供的标准库和大量开源库都是高质量的。 Python 的应用范围广泛意味着库的范围广泛。
  • 更快的开发周期。 没有编译步骤意味着我可以更快地测试更改。 例如,在开发 Web 应用程序时,开发服务器会检测更改并在保存文件时重新加载应用程序。 只需按一下键即可从编辑器中运行单元测试,并立即执行。
  • “轻松访问”常用功能:列表、列表推导式、生成器、元组等。
  • 语法更简洁。 与典型的 .NET web.config 文件相比,您可以用更少的代码行创建基于 WSGI 的 Python Web 框架 :-)
  • 很好的文档。 好书。

Here is parts of my answer to a previous, similar question (I know C#. Will I be more productive with Python?):

I come from a C#/.NET background myself. Started programming in .NET in abt. 2001, and at about the same time was introduced to Python. In 2001, my time spent in C# vs. in Python was about 90% C# / 10% Python. Now, the ratio is 5% C# / 95% Python. At my company, we still maintain a product line based on .NET. But all new stuff is based on Python.

We have created non-trivial applications in Python.

In my experience, what makes me more productive in Python vs. C#, is:

  • It is a dynamic language. Using a dynamic language often allows you to remove whole architectural layers from your app. Pythons dynamic nature allows you to create reusable high-level abstractions in more natural and flexible (syntax-wise) way than you can in C#.
  • Libraries. The standard libraries and a lot of the open-source libraries provided by the community are of high quality. The range of applications that Python is used for means that the range of libraries is wide.
  • Faster development cycle. No compile step means I can test changes faster. For instance, when developing a web app, the dev server detects changes and reloads the app when the files are saved. Running a unit test from within my editor is just a keystroke away, and executes instantaneously.
  • 'Easy access' to often-used features: lists, list comprehensions, generators, tuples etc.
  • Less verbose syntax. You can create a WSGI-based Python web framework in fewer lines of code than your typical .NET web.config file :-)
  • Good documentation. Good books.
愛上了 2024-08-01 09:07:44

交互式外壳!
这是生产力的巨大提升。 只需启动 irb/python 即可进入交互式 shell
(分别适用于 ruby​​ 和 python)。 然后,您可以交互地测试您的类、函数、试验表达式(非常适合正则表达式)、不同的语法和算法。 这是真正的程序员游乐场,也是调试的好工具。

关于错误,我只有两分钱:

所有这些运行时输入,没有编译器
基本内容等方面的错误只会导致
我的生活变得更加艰难
构建大型复杂应用程序。

编译器可检测错误是您在执行各种自动测试(单元、功能等)时将检测到的错误,以及您无论如何都应该编写的错误。
也许 Linus Torvalds 会说:回归测试”?那是什么?如果能编译,那就很好,如果能启动那就完美,但他有数千名测试人员可以完成这项工作
他。 啊,使用交互式 shell,您将获得有关语法的自动反馈,就像编译应用程序时一样,但代码会就地执行。

Interactive Shells!
This is a huge productivity gain. Just launch irb/python to sit in front of interactive shell
(for ruby and python respectively). You can then interactively test your classes, functions, experiment with expression (great for regex), different syntax and algorithms. This is real programmers playground and great tool for debugging too.

Just me two cents about errors:

All this run-time typing, no compiler
errors on basic stuff, etc just makes
my life harder when it comes to
building large complex applications.

Compiler detectable errors are the kind of errors you will detect when you execute various automatic tests (unit, functional etc...) and those you should write anyway.
Probably Linus Torvalds can say: Regression testing"? What's that? If it compiles, it is good, if it boots up it is perfect but he has thousands of testers that will do the job for
him. Ah and using interactive shells you are getting the automatic feedback about your syntax like when you compile the application but the code gets executed in place.

挖鼻大婶 2024-08-01 09:07:44

我也喜欢静态类型。 但我发现,当我编写 Python 时,我并没有那么想念它(只要我使用的类有很好的文档记录——而且我认为没有任何语言功能可以让我们避免糟糕的文档) 。 在编写 C++ 时,我也不会怀念 Python 的大部分动态功能(我怀念 lambda:即使语法很糟糕,也要使用 C++0x。还有列表推导式)。

对于错误检测,大多数“传递了错误的类型”和“调用了错误的方法”错误并不微妙,因此主要区别在于异常替换了编译器错误。 您必须确保实际执行每个代码路径和所有重要数据路径,但当然,无论如何您都会为严肃的项目执行此操作。 我怀疑测试可以分配给给定变量的所有类很少会很困难。 主要问题是,习惯 C++ 的人已经学会了依赖编译器,而不是努力避免编译器会捕获的大类错误。 在 Python 中,您可以选择在编码时考虑它,或者等到测试运行后再找出它。 同样,如果要找到所有调用站点,则需要在 Python 中对“更改参数并查看哪些内容无法编译”的 C++“自动重构”操作进行一些修改。

因此,您必须确保运行正确的测试子集,因为大型 Python 应用程序的完整单元测试运行将花费比当前 C++ 代码/编译/代码/的“编译”阶段可接受的时间更长的时间编译/编码/编译/测试循环。 但这与不想用 C++ 重建所有内容是完全相同的问题。

当您的代码实际上很难运行时(例如使用嵌入式设备,或者您无法在本地重现的特别奇怪的服务器环境),静态类型会获胜,因为您在弄乱串行电缆之前会捕获更多错误,并且同步。 但这就是为什么无论您使用什么语言编写,都需要一个模拟器,以及为什么对于严肃的服务器代码,如果开发人员的机器无法模拟生产,您需要一个适当的测试环境。

另外,值得记住的是,许多 C++ 编译器错误和警告实际上与您的设计无关,它们与这样一个事实有关:有很多不同的方法可以编写看起来正确但行为完全错误的代码。 Python 程序员不需要警告他们不小心插入了三字符组,或者类型双关的指针,因为他们没有精神病预处理器,也没有基于严格别名的优化。

I like static typing too. But I don't find I miss it all that much when I'm writing Python, (as long as the classes I'm using are well documented - and I'd argue that no language feature will ever save us from bad documentation). Neither do I miss most of Python's dynamic features when writing C++ (lambdas, I miss: bring on C++0x even if the syntax is horrible. And list comprehensions).

For error-detection, most "wrong type passed" and "wrong method called" errors are not subtle, so the main difference is that exceptions replace compiler errors. You have to make sure you actually execute every code path and all significant data paths, but of course you're doing that anyway for serious projects. I suspect that it's rare to be difficult to test all classes that could be assigned to a given variable. The main problem is that people used to C++ have learned to lean on the compiler, and don't try hard to avoid large classes of errors which the compiler will catch. In Python you have the option of thinking about it as you code, or waiting until the tests run to find out about it. Likewise, the C++ "automatic refactoring" operation of "change the parameters and see what fails to compile" needs some modification in Python if you want to locate all call sites.

You therefore have to make sure you're running the right subset of tests, since a full unit test run of a large Python app will take far longer than is acceptable in the "compile" phase of your current C++ code/compile/code/compile/code/compile/test cycle. But that's exactly the same problem as not wanting to rebuild everything in C++.

Static typing wins when it's actually difficult to run your code (for instance with embedded devices, or especially weird server environments you can't reproduce locally), because you catch more errors prior to the point where you're messing about with serial cables and rsync. But that's why you want an emulator regardless of what language you're writing in, and why for serious server code you have a proper test environment if developer's machines can't simulate production.

Plus it's worth remembering that a lot of C++ compiler errors and warnings aren't actually about your design, they're about the fact that there are so many different ways to write code which looks correct, but behaves completely wrong. Python programmers don't need warning that they've accidentally inserted a trigraph, or type-punned a pointer, because they don't have a psychotic preprocessor, or optimisations based on strict aliasing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文