Forgot your password?
typodupeerror
Java

Sanity's Journal: An alternative to syntax extensions 9

Journal by Sanity
One of the common criticisms made by advocates of scripting languages such as Python, Perl, and Ruby, of languages like Java is that programming in Java can be quite verbose due to the lack of syntactic support for things like maps. In Python, if you want to access a variable in a map you can do so with simething like:

value=map["key"]

In Java, you must use:

value=(ObjectType) map.get("key");

Java's syntax is obviously more verbose, but Java advocates defend this verbosity on the basis that a language's syntax should be as simple as possible, and the notion of extending the syntax to support a particular (albeit popular) type of datastructure is anathema.

This conservative attitude is understandable when one looks at a language like C++, which in many ways gleefully encourage the programmer to muck around with syntax, the result being that one practically needs to learn to interpret a new programming language every time they try to decipher a new developer's C++ code.

So, what hope for reducing Java's verbosity without falling into the C++ trap? Salvation may come from an unlikely source, the IDE. The notion that a developer might wish to have a more concise way to express and view common code structures and expressions is understandable, the difficulty comes when they must impose their syntactic preferences on others.

The solution? What if an IDE allowed a developer to define syntactic shortcuts which they could use, and which would be used in the code they are viewing, but which would automatically be converted to "vanilla" Java before being saved to disk.

This would afford most, if not all of the benefits of a more consise syntax, without the disadvantage of having to endure the ill-judged syntactic conventions of others.

Thoughts?

This discussion has been archived. No new comments can be posted.

An alternative to syntax extensions

Comments Filter:
  • The solution? What if an IDE allowed a developer to define syntactic shortcuts which they could use, and which would be used in the code they are viewing, but which would automatically be converted to "vanilla" Java before being saved to disk.

    Is this not akin to the #define preprocessor command in C (and perhaps the typedef keyword too)?

    Regardless, such a mechanism needs to be a language itself.
    • Is this not akin to the #define preprocessor command in C (and perhaps the typedef keyword too)?
      Definitely not, you need to read the article more carefully.
      • I meant the macro capabilities of #define. I understand that this article (and you right now) are talking about complete syntax control; I was talking about the ability for macros to shortcut syntax. This is why I specifically said "akin"; they are related, not equivalent.

        I was looking at your example with maps when I originally replied.

        Of course, now looking at the article again, that suggestion of mine goes against the principles of OOP.

        But the other -- that syntax extensions should reduce to some kind
  • If you're really sharing the source with others, and they're making modifications without the benefit of your special syntax, I can't imagine that your IDE will be able to properly remap their changes to your private syntax.

    An algorithm could obviously be devised to map many of their changes back to your syntax, but then you'd be changing the source without knowing it the next time you posted.

    If it's ok that your special syntax would disappear the second that someone else touched it, then you wouldn't wan
    • I can't imagine that your IDE will be able to properly remap their changes to your private syntax.
      Then you must not have a very good imagination - this could be achieved using pattern matching. Note that Eclipse already parses Java code.
      • "Pattern matching" is too vague. That describes anything that Turing machines do. If you're talking about human written regexps, I think you're wrong. The task would be far too complex.

        If you're talking about something as involved as Eclipse's incremental compilation, then yes, remapping someone else's code to your syntax style would absolutely be possible. It wouldn't work in all cases, and when it did work, it would change the source without you knowing it. At best, it'd be as good as a really good forms
        • All that is required is matching subsections of a parse tree and temporarily substituting them for the user-defined structure - since Eclipse is already creating a parse tree. This isn't rocket science.

I never cheated an honest man, only rascals. They wanted something for nothing. I gave them nothing for something. -- Joseph "Yellow Kid" Weil

Working...