Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Random syntax tweaks!  (Read 2282 times)
0 Members and 1 Guest are viewing this topic.
Offline atombrot
« Reply #30 - Posted 2014-06-01 21:01:31 »

This ?? operator just tries to hide a problem instead of fixing it. Getting rid of null and using something like a Option type is way better in my opinion.

Exactly. It's called the Billion Doller Mistake by the "creator" of
null
, Tony Hoare.

While I won't comment on the issue of having null references at all (I don't know enough about the issue to make an useful statement), we have to deal with null references anyway, no matter if null references are a good design decision or not. The coalesce operator can help shortening code (at least I have seen many places in production, where this operator was applied or could have been used to make classes morre concise). So while it might be here to counter bad design decision in our programming languages, I still find it a very useful small tool. I definitely can live without it in java, but I like getting rid of useless clutter. If there is a simpler way of expressing code that has a similar readability, I'm happy to use it, and this applies to ?? if you use it at the right places.
Offline Screem
« Reply #31 - Posted 2014-06-01 21:35:59 »

Dart has really nice constructors and methods with default values:

1  
2  
3  
4  
String method(String foo, {String bar:'default value', String ball:'bat'})

var variable, number, name;
SomeObject(this.variable, this.number, this.name);

Offline Roquen
« Reply #32 - Posted 2014-06-01 21:47:52 »

nulls are awesome.  They're fail-fast and don't have any additional overhead.  If you want something different in a specific context you use a proxy/sentinel.  When will language designers learn that it's impossible to protect programmers from themselves.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ags1

JGO Wizard


Medals: 67
Projects: 3
Exp: 5 years


Make code not war!


« Reply #33 - Posted 2014-06-01 21:54:30 »

And let's not forget the wonderful Groovy safe dot operator:

thing?.field?.subField

which evaluates to null if thing or field are null, rather than giving an NPE. This feature of Groovy is indispensable in my day job (navigating large domain models).

Offline Slyth2727
« Reply #34 - Posted 2014-06-01 22:08:15 »

I've always longed for being able to explicitly create pointers. I think technically every variable in Java is already a pointer, but it would be nice if we could reference and all that stuff like in C languages.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline BurntPizza
« Reply #35 - Posted 2014-06-01 22:12:02 »

I've always longed for being able to explicitly create pointers. I think technically every variable in Java is already a pointer, but it would be nice if we could reference and all that stuff like in C languages.

You can do something a bit like raw pointers with Unsafe: http://stackoverflow.com/questions/8820164/is-there-a-way-to-get-a-reference-address?lq=1
It's not really worth attempting 99.9% of the time though.
Offline SHC
« Reply #36 - Posted 2014-06-02 02:57:50 »

1  
public void init(int width, int height); System.out.println(width, height);

I'd prefer a colon instead of semi-colon.

1  
public void init(int width, int height): System.out.println(width, height);

I think this makes more sence.

Offline Roquen
« Reply #37 - Posted 2014-06-02 05:21:46 »

With JDK8 you can effectively have a pointer to a method inside the language spec.
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #38 - Posted 2014-06-02 09:19:54 »

Multidimensional arrays!
int[,,] stuff = new int[5,2];
or if that's too hard for the JDK writers to cope with then
int[5,2] stuff = new int[];
would do.

Would love default arguments for constructors. Would save a ton of boilerplate coding.

Would be very nice to declare multiple groups of modifiers in declarations eg. static/private/transient etc. wrapped in {} which would save a ton of boilerplate coding too.

Ability to add "finally" to method declarations eg.
1  
2  
3  
4  
public void blah() {
...
} finally {
}


and lastly I'd like "not null" to be part of the language spec instead of just an optionally implemented @annotation and while we're at it, proper design by contract. Bing!

Cas Smiley

Offline Agro
« Reply #39 - Posted 2014-06-02 10:22:46 »

or instead of dealing with null, use option monad Cheesy

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #40 - Posted 2014-06-02 15:29:19 »

[...]we have to deal with null references anyway, no matter if null references are a good design decision or not. [...]
You could use other languages *hides in the woods persecutioncomplex*

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Roquen
« Reply #41 - Posted 2014-06-02 16:17:06 »

The only situation I find hiding nulls is useful is if you have a scripting language that you want to be fault tolerant.  Of course it does nothing to correct errors.  Contracts can be very useful...too bad virtually nobody thinks so.
Offline BurntPizza
« Reply #42 - Posted 2014-06-02 17:00:59 »

What about nested functions?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public List<Point> blah(int t) {

    // blahblah preconditions here

    List<Point> p = new ArrayList<>();
    for(int i = 0; i < t; i++)
        p.add(initPoint());

    Point initPoint() { // uses those preconditions, can't easily move to private method
        /*   */
    }
}


Yeah, it's mostly for convenience, but they can clean up some code quite a bit, esp. if your code is so designed that you can't really pull it out into a private method (which is kinda "ehh" anyway).
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #43 - Posted 2014-06-02 17:09:00 »

What about nested functions?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public List<Point> blah(int t) {

    // blahblah preconditions here

    final class $ { public Point initPoint() {
       /*   */
    } } $ _ = new $();

    List<Point> p = new ArrayList<>();
    for(int i = 0; i < t; i++)
        p.add(_.initPoint());
}


Viewtiful persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #44 - Posted 2014-06-02 17:11:14 »


Oh wow. Beautiful, at least it works ^^

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #45 - Posted 2014-06-03 16:06:52 »

Bit of a coincidence but http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html
Of course it's a bit useless without mandatory non-nullness in the rest of the JDK but there we go.

Cas Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #46 - Posted 2014-06-03 17:37:22 »

Way to make every piece of code calling into a library supporting Optional look ugly and bloated Emo


Current Java code:
1  
String name = car.getEngine().getType().name().toLowerCase(); // potential  NPEs everywhere!


C#'s syntax to solve this problem:
1  
string name = Car?.GetEngine()?.GetType()?.Name().ToLowerCase();


Java 8's fix: (rewrite your entire model, bloat your callsites)
Quote
Optional forces you to actively unwrap an Optional to deal with the absence of a value; as a result, you protect your code against unintended null pointer exceptions.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
public class Car
{
-   public Engine getEngine() { return engine; };
+   public Optional<Engine> getEngine() { return Optional.of(engine); };
}

public class Engine
{
-   public Type getType() { return type; };
+   public Optional<Type> getType() { return Optional.of(type); };
}

public enum Type
{
   STEAM, GASOLINE, MUSCLE
}


-   String name = car == null ? null :
-                 car.getEngine() == null ? null :
-                 car.getEngine().getType() == null ? null :
-                 car.getEngine().getType().name().toLowerCase();

+   String name = car == null ? null :
+                !car.getEngine().ifPresent() ? null :
+                !car.getEngine().unwrap().getType().ifPresent() ? null :
+                !car.getEngine().unwrap().getType().unwrap().name().toLowerCase();

It got worse, much worse... and not even close to C#'s approach... somebody point out my obvious mistake please. persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Spasi
« Reply #47 - Posted 2014-06-03 19:05:28 »

It got worse, much worse... and not even close to C#'s approach... somebody point out my obvious mistake please. persecutioncomplex

It is indeed pointless, unless you use the functional APIs:

1  
2  
3  
4  
5  
6  
7  
String type =
   Optional.of(new Car(new Engine(Type.GASOLINE))) // Optional<Car>
      .flatMap(Car::getEngine) // Optional<Engine>
      .flatMap(Engine::getType) // Optional<Type>
      .map(Type::name) // Optional<String>
      .map(String::toLowerCase) // Optional<String>
      .orElse("<unknown type>"); // String

It becomes more interesting when the source is a stream, instead of a single value.
Offline Roquen
« Reply #48 - Posted 2014-06-03 19:08:42 »

Quote
...somebody point out my obvious mistake please
The fact that the code looks like that ignoring the possibility of a NPE? Wink
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #49 - Posted 2014-06-03 19:15:15 »

It got worse, much worse... and not even close to C#'s approach... somebody point out my obvious mistake please. persecutioncomplex
It is indeed pointless, unless you use the functional APIs:

I'd prefer adding support to the functional libraries instead... (and not go much further)
1  
2  
3  
4  
5  
6  
7  
String type =
      optional(car)
     .optionalFlatMap(Car::getEngine)
     .optionalFlatMap(Engine::getType)
      .map(Type::name)
      .map(String::toLowerCase)
      .orElse("<unknown type>");


It becomes more interesting when the source is a stream, instead of a single value.
Yeah, that was clear to me, I however think that the biggest problem with current code is method chaining, not with functional-style APIs.

I'd hate all business classes / libraries getting sprinkled with Optional. Emo

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #50 - Posted 2014-06-03 20:29:57 »

Hmm... doesn't Java 8 have the ?. and ?: operators though?

Cas Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #51 - Posted 2014-06-03 20:40:29 »

Hmm... doesn't Java 8 have the ?. and ?: operators though?

Cas Smiley

Quote
[...snipped paragraph about Groovy, C# with ?: ?. syntax....]



OK, we diverged a bit and all this sounds fairly abstract. You might now wonder, "so, what about Java SE 8?"

Optional in a Nutshell

Java SE 8 introduces a new class called java.util.Optional<T>

I took that as implying Java 8 takes another route. We'll see.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Agro
« Reply #52 - Posted 2014-06-03 20:48:05 »

yea Java 8 has possibilties for monads now, although its syntax might not be the most *fluent*. the future monad usually takes an async stream of data and does whatever computations on it, then you can fmap/bind a closure on it after its done.

but netflix has spit out a reactive way of handling streams Smiley https://github.com/Netflix/RxJava

Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #53 - Posted 2014-06-03 21:51:48 »

I'll have to check with JDK8 tomorrow about ?. and ?: ... I don't have high hopes though.

Cas Smiley

Offline Endos
« Reply #54 - Posted 2014-06-04 11:12:25 »

I love the princec idea but applied to constructors to have a destructor

1  
2  
3  
4  
5  
public foo() {
...
} finally {
    yourDisposableObject.dispose();
}


It will be nice to have a short object construction syntax when left & right is the same. Instead of
1  
YourClassObject<Class1,Class2> var= new YourClassObject<Class1,Class2>(params);

have this
1  
YourClassObject<Class1,Class2> var= new(params);


And to have by reference parameters instead of only by value
1  
2  
3  
4  
5  
void swap(int a, int b){
int c=a; a=b; b=c;
}

swap(&a,&b);


Bored Birds - End with your boredom for Android
Retroships - Space Shooter for Android
Ultimate - 2D Side-Scrolling Platformer project
Offline OpenGLShaders
« Reply #55 - Posted 2014-06-04 13:03:28 »

1  
2  
3  
4  
int x, y;
if (x && y < 10) {

}
Offline Drenius
« Reply #56 - Posted 2014-06-04 13:07:47 »

^ Not exactly like that, but at least a way to short comparisons like this
Pages: 1 [2]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!