Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exercise 19 of The Pragmatic Programmer

This is taken from Exercise 19 of The Pragmatic Programmer.

A quick reality check. Which of these "impossible" things can happen?:

  1. A month with fewer than 28 days

  2. stat("." ,&sb) == -1 (that is, can't access the current directory)

  3. In C++: a = 2; b = 3; if (a + b != 5) exit(1);

  4. A triangle with an interior angle sum ≠ 180 °

  5. A minute that doesn't have 60 seconds

  6. In Java: (a + 1) <= a

I have some answers but I don't think my answers are "right" (in the sense that it is the answer that the writers were thinking about when they wrote the question).

This is my attempt:

1) September 1752

2) --I think we can skip this, not understanding the question--

3) I don't know C++, but is it something to do with threading and volatile variables (like Java) ?

4) impossible..

5) Due to occurence of leap Seconds

6) double a = 100000000000000001d;

I was wondering does anyone know the correct answer to these questions?

like image 479
Pacerier Avatar asked Nov 04 '11 12:11

Pacerier


3 Answers

3: If a and b are of class type then the overloaded operator+ could do anything (or an overloaded assignment operator, or operator !=, or even an implicit conversion operator for that matter).

4: A triangle on a curved surface does not have an interior angle sum of 180.

6: Overflow in a.

The point of the questions is IMHO to demonstrate that a lot of "impossible" things do actually happen, and that you might not just be being paranoid when you are programming very defensively.

like image 192
Joris Timmermans Avatar answered Oct 10 '22 17:10

Joris Timmermans


2 can happen quite easily. e.g. If you delete your directory while you're in in it and then execute something. Many programs actually handle this.

noufal@sanitarium% ls
noufal@sanitarium% cd
noufal@sanitarium% mkdir /tmp/foo
noufal@sanitarium% cd /tmp/foo
noufal@sanitarium% touch x
noufal@sanitarium% ls
x
noufal@sanitarium% rm -Rf /tmp/foo
noufal@sanitarium% ls
noufal@sanitarium% touch x
touch: cannot touch `x': No such file or directory
like image 20
Noufal Ibrahim Avatar answered Oct 10 '22 19:10

Noufal Ibrahim


nr 5 happens in the case of leap seconds

like image 3
Alfirin Avatar answered Oct 10 '22 17:10

Alfirin