Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Error in demo code (on http://www.dartlang.org/) #812

Closed
nicolasgarnier opened this issue Aug 26, 2014 · 7 comments
Closed

Error in demo code (on http://www.dartlang.org/) #812

nicolasgarnier opened this issue Aug 26, 2014 · 7 comments
Labels

Comments

@nicolasgarnier
Copy link
Contributor

From [email protected] on January 09, 2014 04:13:32

What code is the problem?

this line:

if (4 <= n && n <= 20) {   

in this code:

class Die { // Define a class.
static Random shaker = new Random(); // Define a class variable.
int sides, value; // Define instance variables.

String toString() => '$value'; // Define a method using shorthand syntax.

Die({int n: 6}) { // Define a constructor.
if (4 <= n && n <= 20) {
sides = n;
} else {
throw new ArgumentError(/* */); // Support for errors and exceptions.
}
}
int roll() { // Define an instance method.
return value = shaker.nextInt(sides); // Get a random number.
}
}

What is the expected line?

if (4 < n && n > 20) { Please provide any additional information below. Just a stupid error and proof that nobody tests demos, nor expects them to be taken seriously.

Original issue: http://code.google.com/p/dart/issues/detail?id=15981

@nicolasgarnier
Copy link
Contributor Author

From [email protected] on January 08, 2014 19:28:10

Status: Triaged
Cc: [email protected] [email protected]
Labels: Area-Samples

@nicolasgarnier
Copy link
Contributor Author

From [email protected] on January 08, 2014 21:25:39

Owner: [email protected]
Cc: [email protected] [email protected]
Labels: -Priority-Unassigned -Area-Samples Priority-High Area-Site

@nicolasgarnier
Copy link
Contributor Author

From [email protected] on January 09, 2014 02:09:50

I do not see any error in the code. What is the problem?

@nicolasgarnier
Copy link
Contributor Author

From [email protected] on January 09, 2014 04:58:56

Thanks for looking. If code is to make any sense it should be:

if (4 >= n && n <= 20) {   

The test as is:

if (4 ***<=*** n && n <= 20) {   

which makes the second test useless!

@nicolasgarnier
Copy link
Contributor Author

From [email protected] on January 09, 2014 14:14:59

I am afraid that you have some fundamental misunderstanding. The code correctly checks whether n is at least 4 and at most 20, and neither condition is redundant. On the other hand, the condition “4 >= n && n <= 20” which you wrote in comment #4 is equivalent to “n <= 4”, and with that change, the program would throw ArgumentError because the constructor is called with n == 12.

@nicolasgarnier
Copy link
Contributor Author

From [email protected] on January 09, 2014 14:50:34

You are right:

if (4 <= n && n <= 20) {   

generates confusion, this code is clearer:

if (n >= 4 && n <= 20) {   

at least for me :(

@nicolasgarnier
Copy link
Contributor Author

From [email protected] on February 06, 2014 11:01:42

Status: AsDesigned

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

2 participants