Skip to main content

Reduce the verbosity of declarations

6 replies [Last post]
hlovatt
Offline
Joined: 2003-11-18

Reduce the verbosity of declarations by:

1. Make the new keyword optional.

EG instead of:
String s = new String();
Allow:
String s = String();

2. For methods and classes make the curly braces optional if there is only one statement. IE like for, if, etc.

EG instead of:

class IntWrapper {<br />
    public int i;<br />
}

Allow:
class IntWrapper public int i;

And instead of:

public String toString() {<br />
    return "Hello";<br />
}

Allow:
public String toString() return "Hello";

3. If a class needs to only implement a single method that is defined in either an abstract class of an interface, then allow the method qualifiers, return type and name and the arguments types to be omitted.

EG instead of:

class MyAction implements ActionListener {<br />
    public void actionPerformed( ActionEvent notUsed ) {<br />
		textArea.append( textField.getText() );<br />
	}<br />
}

Allow (only using simplification 3, see below for an example using all 3 rules):
class MyAction implements ActionListener {<br />
    ( notUsed ) {<br />
		textArea.append( textField.getText() );<br />
	}<br />
}

Now an example of combining all three rules, instead of:

textField.addActionListner( new ActionListener() {<br />
    public void actionPerformed( ActionEvent notUsed ) {<br />
		textArea.append( textField.getText() );<br />
	}<br />
});

Allow (using all 3 simplifications):
textField.addActionListner( ActionListener() ( notUsed ) textArea.append( textField.getText() ); );

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jwenting
Offline
Joined: 2003-12-02

iow: "I want to be able to compile Python and Ruby code with the Java compiler".

-0x100

s690716
Offline
Joined: 2004-03-04

> Reduce the verbosity of declarations by:
> ...
> Allow (using all 3 simplifications):
> [code]textField.addActionListner( ActionListener() (
> notUsed ) textArea.append( textField.getText() );
> );[/code]

Yes, the mentioned code is very compact. But it has nothing to do with maintainability. I hope, Java will never be a second "Perl" language.

hlovatt
Offline
Joined: 2003-11-18

Your observation is correct that the syntax is less readable to people with a Java background and with a modern IDE the typing issue is minimal. This begs the question as to why I proposed the syntax change if the case for it doesn't appear that strong?

There seems to be a lot of interest in closures (Groovy) and delegates (C#) and the motivation for these in many circumstances appears to be shorter syntax, since lets face it an inner class is generally superior to either. People seem to like the shorter syntax because to them it is easier to read than the longer syntax, they are used to this short syntax and they find the long syntax confusing and complicated.

I have used Java and other languages and are happy with either syntax, but I do think it important that Java caters for many people - hence the suggestion.

hlovatt
Offline
Joined: 2003-11-18

I should add that the intension is to make small inner classes have similar syntax to closures in Groovy or delegates in C#.

jwenting
Offline
Joined: 2003-12-02

so use C#

mayhem
Offline
Joined: 2003-06-11

I found a nice programming language for .NET yesterday: http://boo.codehaus.org. It's similar to Python but has static typing and infers types of fields, local variables, return types etc. so the code is very compact (like a scripting language). For example:
[code]
def spam():
return "spam!"

a = spam()
[/code]
It also has support for closures and properties, making GUI code very compact:
[code]
button1 = Button(Text: "Click me", Click: { print("clicked!") })
[/code]
Although incomplete, I think the language contains some interesting features.