没有 ELSE 关键字的编程语言 - 是否更复杂?
我正在为儿童开发一种简单的编程语言,基于 Karel。为了控制程序流程,我目前提供以下设施(以伪代码):
- 定义无参数过程
- if [not] EXPRESSION STATMENT
- while [not] EXPRESSION STATMENT
我没有任何方法从过程中返回,并且 我不' t提供else
语句。
以下面的代码为例:
if something
statement1
if not something
statement2
代码的执行流向if
,执行statement1
if 某件事
是真的; 然后测试某事
是否不成立(但程序的状态已更改!),然后执行statement2
。 这可能会导致两个测试都成功。
这会限制程序员吗?到目前为止,我已经能够通过使用 if< 来解决所有示例问题/code> ...
if not
...,或者先使用if not
,然后使用if
。
所以,我的问题是: 是否有必要添加 else
语句? 关键字越多,语言就会变得越复杂。是否所有可以用 else
语句解决的问题在没有它的情况下也可以解决,尽管更复杂?
或者省略 else
语句实际上会使语言变得更加复杂且违反直觉?
I'm working on a simple programming language for kids, based on Karel. For controlling program flow, I currently provide these facilities (in pseudocode):
- defining parameterless procedures
- if [not] EXPRESSION STATEMENT
- while [not] EXPRESSION STATEMENT
I don't have any means to return from a procedure, and I don't provide the else
statement.
Take the following code for an example:
if something
statement1
if not something
statement2
The execution of code flows to if
, executing statement1
if something
is true;
then testing if something
is not true (but the state of the program has changed!), then executing statement2
. This can lead to both tests succeeding.
Does this limit the programmer? So far I've been able to solve all of my example problems by just using if
... if not
..., or using if not
first, then if
.
So, my question is:
Is adding the else
statement necessary? It would make the language a bit more complicated with having more keywords. Are all problems that would be solvable with else
statement solvable also without it, albeit more complicated?
Or is omitting the else
statement actually making the language more complicated and counter-intuitive?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果
某物
的评估成本很高,那么您使用else
的语言可能会出现问题,因为评估将执行两次。另一个潜在的问题是,如果statement1可以修改
something
的值,您可能最终会两个测试都成功 - 如果您使用其他方法,则不会发生这种情况。当然,可以通过将结果存储在临时局部变量中来缓解这些问题:
所以不,您并没有限制程序员的可能性 - 使用
else
可以完成的所有事情都可以在没有它的情况下完成通过使用上述方法。但每次都要编写更多的代码,并且会给粗心的程序员带来一些新的潜在问题,如果您允许else
,这些问题就可以避免。If
something
is expensive to evaluate then your language withelse
might give a problem because the evaluation will be performed twice.Another potential problem is that if statement1 can modify the value of
something
you may end up with both tests succeeding - something that could not happen if you used else.Of course these problems can be mitigated by storing the result in a temporary local variable:
So no you aren't limiting the programmer in what is possible - everything that can be done with
else
can be done without it by using the above approach. But it is a little more code to write each time and it introduces a few new potential problems for the unwary programmer that would be avoided if you allowedelse
.从语义上讲,您可以避免使用
else
构造,但从实际角度来看,我认为没有必要这样做。如果某事为真则做某事,否则做其他事的概念并不那么奇怪和令人困惑,听起来实际上非常简单,必须再次评估和否定一个表达式只是为了检查其否定..自由(在“不增加复杂性”的意义上)可选的语法糖,在开发语言时是自动的。
与
else
语句相比,我看到许多其他功能确实更无用。那么您没有考虑到对一个条件进行两次评估可能对副作用或<强>复杂性(浪费CPU?)或者事实上你已经计算过它,但由于缺乏语言而必须再次计算,而不是因为它有意义。Semantically speaking you could avoid having the
else
construct, but from practical point of view I don't see any necessity of doing that.The concept of do something if something is true, otherwise something else is not so strange and confusing, it sounds actually quite straightforward that having to evaluate and negate an expression again just to check its negation.. it's a free (in sense of "with no added complexity") optional synctactic sugar that is automatic when developing a language.
I saw many other features really more useless compared to the
else
statement.. then you are not considering the fact that evaluating a condition twice maybe harmful for side-effects or for complexity (wasted cpu?) or for the fact itself that you already have calculated it and you have to do it again for a lack of the language not because it's senseful.如果
某件事
有副作用,那么你的方法会导致它们发生两次,这可能不是你想要的。If
something
has side-effects than your approach will cause them to happen twice, which is probably not what you want.恕我直言,教孩子重复代码是个坏主意。
IMHO It's bad idea to teach children to duplicate code.