beanshell / beanshell

Beanshell scripting language
Apache License 2.0
815 stars 183 forks source link

Should BeanShell be type safe? #759

Open opeongo opened 3 months ago

opeongo commented 3 months ago

There are a couple of changes in #721 that remove useful warning messages for the average programmer. Sure, this PR removes warning from the interpreter, but this might just be hiding programming problems that would be better to be warned about. For example, auto-converting one data type to another. This may seem like a minor convenience to save a cast or a perhaps relational binary operator, but my experience in large code bases is that it is nice to have the compiler catching my mistakes... either a typo or referencing the wrong variable.

So if you try to use a number or string as a conditional test in an if statement there should be a warning about this. If the string to boolean conversion is blank=false/non-blank=true then all this is saving is (a) vs (a.isEmpty()), And for numbers if it is 0=false/!0=true then the saving is (a) vs (a==0). Going from boolean to number the saving is (a?1:0). That is really not many characters to add to make it explicit. Having the explicit test also improves the clarity of the code, making obvious what would otherwise be something to have to think about.

I have used languages that do these sort of automatic conversions (perl for example). Most people consider that perl programs are write-only code: the author can understand the code as it is being written, but wait a few months and the statements make as much sense as modem line noise. So convenient to write in because so economical of keystrokes. So terrible to maintain. This is an important aspect of language design.

So I think that there are two points to consider here:

  1. Compiler warnings that indicate that something unexpected is happening often catch unexpected things, and this is a good thing. This is the benefit of having a type system. BeanShell is loosely typed which makes it easy to write scripts, and also the type checking messages from the compiler are a nice thing to assist developers.
  2. Clarity is an important aspect of writing program/scripts that are clear and that other people will find easy to read and follow. Having the compiler perform slight-of-hand automatic conversion reduces that clarity and adds a code smell. Just like ((a/b)*c)+d is easier to quickly understand than a / b * c + d, unexpected conversions make more sense when they are made explicit.

The automatic type conversion between numbers-string-boolean gives up warnings which is a loss, and in return it really does not add much to the language other than save a minimal number of keystrokes. The loss of clarity that comes with automatic type conversion really does not help anyone.

I would like to keep the majority of #721 as is, but back out the automatic datatype conversion portions.

nickl- commented 1 month ago

have the compiler catching my mistakes

Is this not an issue that we are not compiling but interpreting? As discussed in #501

Many scripting languages ie. JavaScript is loose typed, with large code bases, without any problems. The convenience outweighs the cons. And you cannot have it both ways, type safe requires type declaration, BeanShell never required type declaration so is therefor by definition loosely typed.

If you want type safe then use JAVA compiled.

Suggestion close issue: works as implemented.