views:

108

answers:

6

Due to a wave of criticism to use Global Vars in my Java post, I want to use a language without global vars. One suggestion per answer, thank you.

+2  A: 

If you don't want global variables then don't use them. I'm don't see why finding a language that doesn't support them would offer any value. You're dealing with a question of scope. Make your variables exist in the lowest scope possible and you'll be fine. There is no language that doesn't have a global scope.

Peter
+3  A: 

I'm not sure if you literally mean no global variables (values that change), or if you also want to rule out global constants (values that don't change). Haskell is one such language that doesn't have global variables, although it does have global constants (in the form of parameterless functions, essentially). In Haskell, if you want to emulate global state, you have to pass your "global" variables into each function you call. A better description is available from the HaskellWiki.

mipadi
Does standard Haskell even have "variables" (not counting mutable values faked by monads)?
KennyTM
It depends on how you define a variable. The variables in Haskell are best described as constants in imperative languages. In Haskell the value assigned to a variable can never be changed. See also http://www.haskell.org/haskellwiki/Variable
Rudi
+3  A: 

If you want to avoid side-effects you can use one of the pure functional programming languages like haskell (or erlang or ...), which have only constants. But be prepared for a completely different kind of programming compared to the imperative programming style.

Rudi
A: 

Well, I read that Graal is a functional programming language without variables, so I guess that might qualify.

High Performance Mark
A: 

I believe Eiffel doesn't use global variables but that isn't a serious criterion
of whether to use a language or not.

Nick D
+1  A: 

Java (and similar OOP-languages) doesn't have really global variables, all variables are tied to an instance or a class. But I think you want also outrule the 'simulated' global variables (public fields with the only reason to be accessed from the outside). I don't think any of the popular languages don't allow this, but more uncommon language-concepts may have ways to avoid such things.

Mnementh