• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

what is a legal state?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading "Java in a Nutshell" book and in a section about safe java programming it's listing 4 requirements of a safe program which are:

All objects start off in a legal state after creation
Externally accessible methods transition objects between legal states
Externally accessible methods must not return with objects in an inconsistent state
Externally accessible methods must reset objects to a legal state before throwing



the term legal state is repeated a few times here, what is it referring to?
 
Marshal
Posts: 79698
381
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds rather like class invariants. An invariant is omething that has to be true at all times. For example, a kettle must at all times contain a non‑negative (that includes 0) amount of water and that water has to be at a temperature between 0℃ and 100℃ inclusive. An object is in a legal state when all its invariants are satisfied.
Every constructor must establish all the invariants and every method call must preserve them, in my opinion. It is however permissible for intermediate calculations in a method to breach the invariants, but that method must restore the invariants before it completes. Note in the following example I have omitted a bit of defensive programming; I challenge you to tell me what it is.
 
Enthuware Software Support
Posts: 4856
52
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A legal state means that the values of the instance fields of an object make sense as per the business logic that the class is meant to represent/model. To continue with Campbell's example, if you are modeling a real kettle using a Kettle class and using an int field named t for temperature in degree Celsius, does your business logic expect its value to be less than 0 or greater than 100? If no, then a Kettle object with t = -1 is in an illegal state even though an int can very well store a value of -1.

The legality is decided by you, the developer, and not by Java.
 
Saloon Keeper
Posts: 15712
367
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I means whatever YOU, the designer, has defined it to mean for a particular class.

For instance, let's say you have a bank account:

The "legal state" of a bank account is that the amount of money deposited in the account is greater than or equal to the lower limit. Here is a valid use:


At all times, the account is in a legal state. However, the following code demonstrates that the account can be in an illegal state:

The banked amount is 0, which is less than the lower limit. This should not be possible! The first requirement was violated:

All objects start off in a legal state after creation



We fix this by adding validation to the constructor:


Now, what if a bank account is not constructed with a lower limit, but instead the lower limit is read from a file:

"lower-limit.txt" is supposed to contain the value -300.

Assume that there is a problem with reading "lower-limit.txt". Maybe the file doesn't exist or is in the wrong location:

The object transitioned from a legal to an illegal state because it didn't reset before throwing. Here is the fix:

The bankedAmount is only changed if the readLowerLimit() call succeeds. If an exception occurs, the account is unchanged, and so remains in a legal state.
 
Naadiyaar Mansouri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have omitted a bit of defensive programming; I challenge you to tell me what it is.



I hope I understood what is in your mind correctly, so in this case since we're accepting any range of int values, getting a negative degree can cause the method to behave unexpectedly and despite its name being cool, it actually warms up the water
Therefor adding another if statement to check int's sign seems like a good idea
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naadiyaar Mansouri wrote:. . . I hope I understood what is in your mind correctly . . .

Yes Spot on. Well done.
 
Master Rancher
Posts: 5001
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other thing I noticed there is, what if degrees is a large enough number that subtraction would cause overflow?  That's effectively impossible in the real world (i.e. for remotely realistic temperatures), but within your program, it could happen...
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have conveniently forgotten about overflow.
 
What's gotten into you? Could it be this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic