Question

Syntactic sugar for properties for example in C#:

private int x;

public int X{
    get { return x; }
    set { x = value; }
}

or simply

 public int X{ get; set; }

I am missing verbatim strings in java... @"C:\My Documents\" instead of "C:\\My Documents\\"

Do you agree Java needs more "sugar"? Any one knows is there is sugar coming in next Java versions?

Was it helpful?

Solution

While I don't necessarily agree with Java's philosophy, I think that adding lots of syntactic sugar to Java would go against its philosophy. Java is supposed to be a very simple, easy to reason about language with few constructs, kind of a lowest common denominator lingua franca in the programming community. It was largely a reaction to the complexity of C++. There is supposed to be very little "magic" in it, beyond what is needed (garbage collection, mostly) to make it a memory-safe language.

If you want more sugar, etc. in a reasonably high-performance, statically typed language, I would say that Java is just the wrong language for you. You're probably better off just using C#, or maybe D.

OTHER TIPS

"Syntactic sugar causes cancer of the semicolon."

-- Alan Perlis. Epigrams on Programming.

Sounds like you want Groovy... Apparently properties are on their way, although not in Java 7 (as @erickson helpfully corrected me about in a comment).

Groovy does have nice string sugar.

I've also developed in both Java and C# the last few years, and find C# a superior language with regards to expressiveness and powerful language constructs. The Java language does not undergo the same degree of changes and updates as C#, atleast not at the same pace. I still don't necessarily mean that Java should be drastically updated, but we need a powerful and expressive statically typed language on the Java platform. I think Scala is going to develop into this replacement language, which we Java developers can switch to when ordinary Java does not cut it.

C# is an absolutely fantastic language; probably the "best" statically typed language these days, but Java is still in my opinion a superior platform. I like Java the platform, and I like C# the language.

As per Mark Reinhold's talk at Devoxx 2008, property support will not be added to Java in Java 7.

http://hamletdarcy.blogspot.com/2008/12/java-7-update-from-mark-reinhold-at.html

More info on properties in Java 7 ideas here:

http://tech.puredanger.com/java7#property

While properties are nice, they are not java. I seriously think the javabean spec closed that door ages ago. I think there are clearer cases for syntactic sugar that is needed:

  • Use of inner classes due to lack of delegates/closures. Current syntax is from hell.
  • Methods as first-order language constructs, ie method data types.
  • Type inference with generics
  • Run-time presence of generics in reflection API.
  • Events

If I use java for anything large again, it will be as an output language from another compiler.

That is oldschool C#. Nowadays it is

public int X { get; set; }

Sugar is bad for your teeth. Syntactic sugar is bad for your brain.

This isn't entirely necessary.

A simple editor macro could work:

prop int x -->

private int x;

public int getX(){
   return x;
}
public void setX(int val){ 
   x = val;
}

Edit: (in response to comments)

How is this any less readable than:

private int x;

public int X{
    get { return x; }
    set { x = value; }
}

I've written some annotations (and an annotation processor) that helps this quite a bit.

See http://code.google.com/p/javadude/wiki/Annotations

For example:

@Bean(
    properties={
        @Property(name="name", bound=true), // String is default type
        @Property(name="age", type=int.class, bound=true)
    }
)
public class Foo extends FooGen {}

This generates FooGen containing the fields and get/set methods, as well as making them bound (which is optional). There are many other fun things you can do with these as well.

NOTE: I'm doing a few tweaks now that deprecate the various "override" options.

Enjoy,

-- Scott

One thing Java would do really well to implement is something equivalent to ref and out parameters.

I don't like "syntactic sugar" as is mainly because it would be yet another thing to learn and would most likely get abused eventually. I have cursed at the annoyance of making getters and setters myself though so I understand why one would want to make creating those as easy as possible but I'd rather see @Get, @Set and @GetSet annotations than more syntax thingamajiggers to do the job.

The java platform has adopted a huge amount of programming languages. If you want more syntactic sugar you could use another language like Groovy or (J)Ruby and it will still run on the jvm and work with your other java-libraries. (I even think there is a C# implementation ;)

As with many, i am stuck to manage application written in Java 1.4 (migration to 1.5 is one the way in many cases). So even if Java 7 or 8 get new feature I will not be able to used them... Anyways syntactic sugar is good, it can help the writing, reading and analysis of code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top