We changed our privacy policy. Read more.

Hot answers tagged

118

First, I'd say that the answer you link to overstates that particular issue and that the primary evil of global state is that it introduces coupling in unpredictable ways that can make it difficult to change the behaviour of your system in future. But delving into this issue further, there are differences between global state in a typical object-oriented ...


101

"web applications should be stateless" should be understood as "web applications should be stateless unless there is a very good reason to have state". A "shopping cart" is a stateful feature by design, and denying that is quite counter-productive. The whole point of the shopping cart pattern is to preserve the state of the application between requests. An ...


65

It's true that web applications should be stateless. However, session variables, cookies, and tokens don't violate this when they are all stored on the client (web browser). They can be parameters in the request. Here's a simplified model: Web Browser (has state) <-> Web Server (stateless) <-> Database (has state) This could work for Software ...


53

You have state when you associate values (numbers, strings, complex data structures) to an identity and a point in time. For example, the number 10 by itself does not represent any state: it is just a well-defined number and will always be itself: the natural number 10. As another example, the string "HELLO" is a sequence of five characters, and it is ...


52

I like this question. The following is from my head but I think it fits quite well. status is used to describe an outcome of an operation (e.g. success/fail). state is used to describe a stage in a process (e.g. pending/dispatched). I also like this definition: status is a final (resulting) state. It is quite clear when applied to programming. Much less ...


45

I'd offer a few observations: Yes, a database is global state. In fact, it's a super-global state, as you pointed out. It's universal! Its scope entails anything or anyone that connects to the database. And, I suspect lots of folks with years of experience can tell you horror stories about how "strange things" in the data led to "unexpected behavior" in ...


38

There is no rule. It's entirely up to how you want to make your API "feel." Personally, in a music player, I think a transition from the state Stopped to Stopped by means of the method Stop() is a perfectly valid state transition. It's not very meaningful, but it is valid. With this in mind, throwing an exception would seem pedantic and unfair. It would ...


32

What exactly is meant by this? In this context, it means that OOP obscures the state of a program by hiding it away from the programmer but still making it visible via (leaky) accessor methods. The argument is that by obscuring the state, it makes it more difficult to work with, and by extension lead to more bugs. how accurate/relevant do you guys think ...


32

web applications should be stateless Nonsense. Web requests should be stateless. Or more accurately, web requests are stateless. But, saying that a whole application should be stateless is complete nonsense. each request is treated as an independent transaction. Yes, exactly. Or more accurately, yes, necessarily. Over HTTP, each request is inherently ...


29

All else being equal, you should express your invariants in code. In this case you have the invariant $this->tax = $this->taxPC * 0.01 * $this->price; To express this in your code, remove the tax member variable and replace getTaxAmt() with public function getTaxAmt() { return $this->taxPC * 0.01 * $this->price; } You should do ...


26

What's the difference between new CustomerReceiptCreator().createReceipt() and CustomerReceiptCreator.createReceipt()? Pretty much none. The only significant difference is that the first case has considerably more awkward syntax. If you follow the first in the belief that somehow avoiding static methods makes your code better OO, you are gravely mistaken. ...


22

I disagree with the fundamental claim that: When your program is working with data from a database, you don't care if other code in your system is changing it, or even if an entirely different program is changing it, for that matter. My initial thought was "Wow. Just Wow". So much time and effort is spent trying to avoid exactly this - and working out ...


18

There are two distinct kinds of actions one may wish to perform: Simultaneously test that something is in one state, and change it to another. Set something to a particular state, without regard for the previous state. Some contexts require one action, and some require the other. If a media player which reaches the end of content will remain in a "...


18

The point that the sole reason global variables can't be trusted since the state can be changed somewhere else is, in itself, not reason enough to not use them, agreed (it's a pretty good reason though!). It's likely the answer was mainly describing usage where restricting a variable's access to only areas of code that its concerned with would make more ...


18

In the context of web applications, we call the server stateful if it maintains transient state in memory, rather than storing any data externally (e.g. in a database). Stateful applications have a number of problems, for example: you can't have more than one server running without pinning sessions to a particular server the state is lost when the server ...


17

Short answer: yes. According to Wikipedia, the equivalence of lambda calculus to Turing machines as an universal model of computation was shown 1937 by Alan Turing. The computational model of a Turing machine is what you typically have in mind when talking about imperative or stateful programming, and lambda calculus is a mathematically formalization of "...


16

Statefulness is not necessarily a bad thing, but you need to understand the difference between stateful and stateless apps. In short, stateful apps maintain information about the current session, and stateless apps do not. Information stored permanently as part of a user account may or may not be stored in a session, but storing information related to a user ...


15

State is simply information about something held in memory. As a simple exercise in object orientation, think of a class as a cookie cutter, and cookies as objects. You can create a cookie (instantiate an object) using the cookie cutter (class). Let's say one of the properties of the cookie is its color (which can be changed by using food coloring). The ...


15

The whole program is going to end up contained in the IO monad, basically. That's the bit where I think you're not seeing it from the Haskellers' perspective. So we have a program like this: module Main main :: IO () main = do xmlData <- readFile "input.xml" let jsonData = convert xmlData writeFile "output.json" jsonData convert :: String -> ...


14

In whatever dynamic system, the "state" is what make your present to be influenced by your past or future (the arrow of time is not a mathematical issue, just a physical constrain). Whether you have something to "remember" or that depends on what you did, you have a state. A system with no state is not "dynamic": it is just a combinatorial function. That ...


13

Any disadvantages[?] Sure. This method relies on everyone always remembering to do something. Any method relying on everyone&always is bound to fail sometimes. maybe better ways to do this? One way to avoid the burden of remembering ceremony is calculating properties of the object that depend on other properties as needed, as @eigensheep suggested. ...


13

Is this what the global state must overcome to be accepted? No. Adding contracts to what goes into and out of global state is as easy as using a language with real types. There are lots of those and global state is still evil there. Things you're missing: Who fubar'd my data?!? - the biggest, most obvious problem with global mutable state is that anyone ...


12

(I don't know Erlang, and I can't write Haskell, but I think I can answer nevertheless) Well, in that interview the example of a random number generation library is given. Here is a possible stateful interface: # create a new RNG var rng = RNG(seed) # every time we call the next(ceil) method, we get a new random number print rng.next(10) print rng.next(10)...


11

Do you think that is wise to skip the status field? Yes. For several reasons. The status field is a cached value of something that can be computed. Caching is an optimization, the premature application of which is an known evil. Caches go out of date, and it takes work to keep them accurate. If you can avoid this it is for the best. I would employ ...


11

I think I know what you mean. Basically you solve the problem by adding either a 'controller' or a 'master' viewmodel (excuse psudocode) ie public class MasterVM { public ChildVM View1 {get;set;} public ChildVM View2 {get;set;} private Data data; public MasterVM() { View1.OnEvent += updateData; } private Action<int&...


11

There is no general rule. In this specific case, the intention of the user of your API is to stop the player from playing the media. If the player is not playing the media, the MediaPlayer.stop() may do nothing and the goal of the method caller will still be achieved - the media is not playing. Throwing an exception would require the user of the API to ...


11

The purpose of exceptions isn't to signal that something bad has happened; it's to signal that Something bad has happened that I don't know how to fix here that the caller, or something up the callstack from it should know how to deal with so I'm bailing out quickly, halting execution of the current code path in order to prevent damage or data corruption, ...


11

But when I look at that, I can't help but think that that's a really weak explanation, because how is that any different from working with data stored in a database? Or any different from a working with an interactive device, with a file, with shared memory, etc. A program that does exactly the same thing every time it runs is a very boring and rather ...


10

I think the term "state" (as opposed to a concrete type of state such as "member variable") is most useful when comparing a stateful API to a stateless one. Trying to define "state" without mentioning APIs is a bit like trying to define "variable" or "function" without mentioning programming languages; most of the correct answers only make sense to people ...


Only top voted, non community-wiki answers of a minimum length are eligible