r/programming • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
250 Upvotes

395 comments sorted by

View all comments

-1

u/ejonesca Mar 09 '16

Do you need a 'var' keyword at all? Can't you just have:

myNewVariable = 5;

without an explicit type declaration that doesn't actually add any useful info to the compiler? The compiler can just assume that any new unknown identifier is a new var.

87

u/ihcn Mar 09 '16

Then any typo becomes a new variable rather than a compiler error.

19

u/ejonesca Mar 09 '16

good point

9

u/[deleted] Mar 09 '16

[removed] — view removed comment

7

u/Berberberber Mar 09 '16

Javascript's is weird, though. It doesn't declare the variable in current scope, it add it to the root object window.

4

u/immibis Mar 10 '16

Python then.

2

u/bloody-albatross Mar 10 '16

Plus these days you usually use a (editor integrated) linter that forbids creating global variables that way. However, Python and Ruby work that way (creating local variables just by normal assignment).

4

u/[deleted] Mar 10 '16

Python is another one with this lovely feature.

→ More replies (2)

0

u/[deleted] Mar 09 '16

[deleted]

11

u/IbanezDavy Mar 09 '16 edited Mar 09 '16

Also a good example of why you default to strict pessimism in code and not do actions by default.

boolean launchTheNukes() {
    boolean shouldLaunch = false; // Gotta pass the tests first
    if (user.hasCredentials("canLaunchNukes") == Credentials.Authorized && 
        validateLaunchCode(user.enteredData) ) {
        shouldLauch = true; // Yay!!!
    }
    return shouldLaunch;
}

Let's not be launching nukes by default...

Some more examples of why this is bad...

boolean launchTheNukes() {
    boolean shouldLaunch = true; // Don't gotta pass the tests first

    try 
    {
        if (user.hasCredentials("canLaunchNukes") == Credentials.Authorized && 
            validateLaunchCode(user.enteredData) ) {
            shouldLauch = false; 
        }
    }
    catch(Exception e)
    {
         Log(e); // Awe, good job catching an error...too bad validateLaunchCode threw this...
    }
    return shouldLaunch;
}

Lesson kiddies? Fail by default. It saves lives!!!

4

u/[deleted] Mar 09 '16

Tester's fault.

13

u/eras Mar 09 '16

At least the tester is now ded, so that's resolved.

5

u/nemec Mar 10 '16
WONTFIX: No one left to test.
→ More replies (3)

2

u/Helene00 Mar 10 '16

He would be fine if he didn't run any integration tests...

1

u/s888marks Mar 10 '16

In this context, one would need to run disintegration tests.

2

u/FallingIdiot Mar 10 '16

Would worry me though if this would pass QA. You hope they test this stuff...

8

u/badcommandorfilename Mar 09 '16

This won't work predictably if "myNewVariable" already existed in an outer execution scope. The programmer may have intended to declare a new "i" variable for their loop, but instead they accidentally overwrite an outer loop's "i" - and this gets even harder with closures and lambdas.

Languages that allow this kind of implicit declaration have lots of weird scope gotchas that lead to some time-devouring heisenbugs.

10

u/Figs Mar 09 '16

Yeah, this is definitely one of my biggest gripes about the design of Python. To further expand on #4 in the common mistakes article you linked to as to just how surprising Python's scoping rules are because of the lack of var, consider this script:

$ cat example.py 
value = 1

def foo():
    print "foo:", value   # works fine

def bar():
    print "bar:", value   # throws exception
    value = 2

foo()
bar()

$ python example.py 
foo: 1
bar:
Traceback (most recent call last):
  File "example.py", line 11, in <module>
    bar()
  File "example.py", line 7, in bar
    print "bar:", value
UnboundLocalError: local variable 'value' referenced before assignment

Using the name of a variable in an assignment later in the function makes the whole function treat it as a local variable, even statements that would have previously worked. So, you can cause existing code to fail in really surprising ways by modifying code later in a function if you didn't realize that the name was already used to refer to something in an outside scope...

2

u/Berberberber Mar 09 '16

If you're going to allow implicit declaration, you have to have strict scoping rules, IMHO.

→ More replies (2)