The Artima Developer Community
Sponsored Link

Weblogs Forum
Clear, Consistent, and Concise Syntax (C3S) for Java

23 replies on 2 pages. Most recent reply: Apr 16, 2007 8:31 PM by Howard Lovatt

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 23 replies on 2 pages [ « | 1 2 ]
Achilleas Margaritis

Posts: 674
Nickname: achilleas
Registered: Feb, 2005

Re: Clear, Consistent, and Concise Syntax (C3S) for Java Posted: Nov 1, 2006 5:34 AM
Reply to this message Reply
Advertisement
> If no changes to Java are made then I think it will
> eventually stagnate, this is what has happened to other
> languages in the past.

So? how is this a bad thing by itself? I would prefer to use a mature platform for my products rather than something that would create problems.

Syntactic sugar by itself is not very important. The changes in Java 5 are not only syntactic, but also semantic: at source code level, the presence of types in containers (for example) makes a great difference in productivity (I have participated in an app with a complex workflow that had some bugs that were solved when the code used generics; some wrong objects were erroneously placed in some containers, but the error was not easy to see).

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: Clear, Consistent, and Concise Syntax (C3S) for Java Posted: Nov 1, 2006 9:01 AM
Reply to this message Reply
> If no changes to Java are made then I think it will
> eventually stagnate, this is what has happened to other
> languages in the past. I think it is valuable to update
> the language and keep it fresh and it has happened in the
> past.

I really think that Java "jumped the shark" with generics. It's not that they aren't a nice feature, it's just that they move the language away from its original audience.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: Clear, Consistent, and Concise Syntax (C3S) for Java Posted: Nov 1, 2006 9:30 AM
Reply to this message Reply
>     final mouse = MouseAdapter.new {
>       method mousePressed( event ) {
>         if ( !event.isPopupTrigger ) { return }
>         ...
>       }
>       method mouseReleased( event ) {
>         if ( !event.isPopupTrigger ) { return }
>         ...
>       }
>     };
>     panel.addMouseListener mouse;
> or
>     panel.addMouseListener new {
>       method mousePressed( event ) {
>         if ( !event.isPopupTrigger ) { return }
>         ...
>       }
>       method mouseReleased( event ) {
>         if ( !event.isPopupTrigger ) { return }
>         ...
>       }
>     };


What I would normally code, is the following. I have a NB editor 'code template' that expands this text and leaves the cursor at the ${name} point. Tab switches to ${comp} and another tab switches to ${cursor}

panel.addMouseListener( new MouseAdapter() {
    public void mousePressed( MouseEvent ev ) {
        if( ev.isPopupTrigger() ) { showPopup(ev); }
    }
    public void mouseReleased( MouseEvent ev ) {
        if( ev.isPopupTrigger() ) { showPopup(ev); }
    }
    private void showPopup( MouseEvent ev ) {
        JPopupMenu pop = new JPopupMenu();
        JMenuItem mi = new JMenuItem(${name});
        ${cursor}
        pop.show( ${comp}, ev.getX(), ev.getY() );
    }
});


In total, I don't see any change in the readability. It might be just a few more characters to type, but that's not gonna put millions in the bank directly.

> This is a good example of how the proposal simplifies things.
>     interface TwoMethodsTwoExceptions {
>         method< Throwable... T1s > void method1( boolean which ) throws T1s;
>         method< Throwable... T2s > void method2( boolean which ) throws T2s;
>     }


> In current Java the above is roughly (only two
> exceptions):
>     interface TwoMethodsTwoExceptions {
>         < T1A extends Throwable, T1B extends Throwable > void method1( boolean which ) throws T1A, T1B;
>         < T2A extends Throwable, T2B extends Throwable > void method2( boolean which ) throws T2A, T2B;
>     }


There are some new capabilities here, in the form of the elipses designation of multiple, similar type parameters. There is some benefit in terms of expressivity.

But, I'm troubled by the notion that the compiler would have to assume 'throws Throwable' or whatever super class was specified. The interface declaration could just as well say "throws Throwable" and move on.

Howard Lovatt

Posts: 321
Nickname: hlovatt
Registered: Mar, 2003

Re: Clear, Consistent, and Concise Syntax (C3S) for Java Posted: Nov 1, 2006 7:22 PM
Reply to this message Reply
@Achilleas, Michael, & Gregg,

Yep I agree with you, the proposal adds very little in terms of extra semantics and very little in terms of extra type checking. With regard to Michael's comment about generics moving Java away from its original audience, I am with you on this one and hope that this proposal pulls it back towards an easier to use language. Primarily because the proposal expands the type inference that the compiler does considerably.

But I do think that the proposal adds Clarity, Consistency, and Conciseness - all of which are highly desirable. E.G. what about filtering file names:
In C3S
    final names = file.list method ( notUsed, name ) { name.equals "..." };
 
and in current Java
    final String[] names = file.list( new FilenameFilter() {
      public boolean accept ( File notUsed, String name ) {
        return name.equals( "..." );
      }
    } );

The current Java version is rather off putting because of all the syntax that is quite low level boiler plate and not necessary to the problem at hand and the proposed version is clear and concise and therefore desirable. This easy to read syntax for short methods is what has made Ruby so popular; you can write an API that is really easy to use by making the declaration of a simple method easy (block closure in Ruby parlance), e.g. Rails.

Howard Lovatt

Posts: 321
Nickname: hlovatt
Registered: Mar, 2003

Re: Clear, Consistent, and Concise Syntax (C3S) for Java Posted: Nov 1, 2006 8:09 PM
Reply to this message Reply
@Gregg,

> There are some new capabilities here, in the form of the
> elipses designation of multiple, similar type parameters.
> There is some benefit in terms of expressivity.
>
> But, I'm troubled by the notion that the compiler would
> have to assume 'throws Throwable' or whatever super class
> was specified. The interface declaration could just as
> well say "throws Throwable" and move on.

The difference is what you have to catch, e.g. lets take a generic list with a filter method that accepts a Filter and the filter can throw an Exception or Exceptions.
In C3S (note that an empty list of exceptions is inferred when the Filter is used because isToBeSelected doesn't throw any exceptions)
    interface< T, Exception... Es > Filter { method boolean accept( T item ) throws Es }
    ...
      final filtered = unfiltered.filter method ( item ) { item.isToBeSelected };
 
and in current Java
    interface Filter< E > { boolean accept( E e ) throws Exception; }
    ...
      try {
        final List< Type > filtered = unfiltered.filter( new Filter< Type >() {
          public boolean accept( Type e ) {
            return e.isToBeSelected;
          }
        } );
      }
      catch( Exception e ) {
        throw new AssertionError( e ); // Need to re-throw and to keep the compiler happy you have to wrap
      }

This situation of having to deal with an Exception even if it isn't actually used currently arises with any interface that throws an Exception, e.g. Callable.

In other cases you might have to catch an Exception in current Java even if you know that only specific Exceptions could be thrown. E.G. say the Filter threw IOException, then in C3S you would catch IOException but in current Java you would have to catch both IOException and Exception.

Achilleas Margaritis

Posts: 674
Nickname: achilleas
Registered: Feb, 2005

Re: Clear, Consistent, and Concise Syntax (C3S) for Java Posted: Nov 3, 2006 5:34 AM
Reply to this message Reply
Can't you use Java to build a simpler language on top of it? With these syntax changes you propose, a new set of IDE tools would be needed anyway.

Regarding generics, I agree that Java "jumped the shark" with generics, but it was necessary. But I think Java should not be evolved any further, because it would be a monster no one would want to play with.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: Clear, Consistent, and Concise Syntax (C3S) for Java Posted: Nov 3, 2006 8:43 AM
Reply to this message Reply
> Can't you use Java to build a simpler language on top of
> it? With these syntax changes you propose, a new set of
> IDE tools would be needed anyway.

I think that this is a key question/observation. One of the biggest reasons why we keep seeing new languages is that people think that a language can only support operations that are syntactically driven into the language definition.

There are a number of interesting opportunity to develop domain languages in the form of APIs instead of as syntactic sugar.

For decades we've had exactly the same functionality available in 100's of languages as people refuse to just deal with 'syntax issues' and move on.

Sure, there are some symantic meanings embedded in language definition that seem tied to syntax, but in the end, they are computations based on the features of the underlying hardware, which all these languages share access to. So, it's entirely possible to create the same algorithms in most other languages.

The predominate issue that I hear of is 'takes too long to type' and 'takes too long to setup to use'. So, people are considering the short term investment in one time typing or environment setup to be more important than the long range capabilities of common programming environments based on common language runtimes which would allow much more reuse of existing solutions.

Instead, people rewrite things in new languages, and then pitch them out into the world which only provides more disturbance in the development of real programming solutions for exising languages/platforms that would benefit much more from having such things available.

Howard Lovatt

Posts: 321
Nickname: hlovatt
Registered: Mar, 2003

Re: Clear, Consistent, and Concise Syntax (C3S) for Java Posted: Nov 4, 2006 6:54 PM
Reply to this message Reply
@Achilleas & Gregg,

I agree totally with you both that you can write many of the newer API in Java already, for example you could write something along the lines of Rails. But the resulting API would be very verbose to use because it would make extensive use of inner classes. This seems to be a barrier to most people and hence the proposed changes.

This idea that the syntax to some people is so important is a surprise to me. For example pre-readily available C++ compilers I wrote a simulation program using OO techniques in C that essentially hand coded what cfront (the original C++ compiler that translated into C) did. But other people who used and maintained the program found that very difficult and they eventually re-coded in C++. The lesson I learned is that I am much more tolerent of syntax than other people are, they find it hard to see what is happening if there is a lot of boiler plate code around.

I think this is why new languages come along, because a new coding style becomes common and therefore it is worth supporting with new syntax. I think that is now happening with Ruby that is making such extensive use of inner classes (block closures in Ruby parlance) and this is only acceptable to most people because the syntax to make a simple method is Clear and Concise.

Howard Lovatt

Posts: 321
Nickname: hlovatt
Registered: Mar, 2003

Re: Clear, Consistent, and Concise Syntax (C3S) for Java Posted: Apr 16, 2007 8:31 PM
Reply to this message Reply
I have blogged a comparison of different inner class/closure proposals at:

http://www.artima.com/weblogs/viewpost.jsp?thread=202004

Flat View: This topic has 23 replies on 2 pages [ « | 1  2 ]
Topic: Clear, Consistent, and Concise Syntax (C3S) for Java Previous Topic   Next Topic Topic: Dreams and Nightmares


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us