πŸ“œ ⬆️ ⬇️

Bug optimization ?!

Introduction


I was deeply touched by the statement of my colleague that using exceptions is wrong. And then followed a series of explanations: it is slow, it is ugly, it is inefficient, it is inconvenient.

To structure the opinion of Habra-users on this issue, a small survey on the use of exceptions was published. I want to immediately say that this topic does not pretend to the fullness of the presentation of the material about the exceptions, it only affects the area of ​​their use.

It follows from the statement that during development there is an optimization of errors, or in a more general sense, premature optimization. Many people know that premature optimization is harmful. But optimization of errors leads to the emergence of new errors, complicates the code and destabilizes the entire code.

Simple examples


Consider two simple examples, based on which you can describe most of the errors that occur:
')
Point readPoint() {
  ...
}

Points readPoints() {
  ...
}

, :

Point point = readPoint();
if (point != null) {
  ...
}

Points points = readPoints();
if (points != null) {
  ...  
}

. . , , .

null, . , , null β€” , .

. . , , . .

:

Points points = readPoints();
for (Point point : points) {
  ...
}

, .


, :

Point readPoint() throws PointNotFoundException {
  ...
}

Points readPoints() throws CouldNotReadPointsException {
  ...
}

:

:

try {
  Point point = readPoint();
  ...
} catch(PointNotFoudException ex) {
  ...
}

try {
  Points points = readPoints();
  for (Point point : points) {
    ...
  }
} catch(CouldNotReadPointsException ex) {
  ...
}

-, , . .


. β€” . . . 99,9% ? , .

, , ?

. , . :

while (!queue.isEmpty()) {
  Point point = queue.readPoint();
  if (point != null) {
    ...
  }
}

null , .

:

try {
  while (!queue.isEmpty()) {
    Point point = queue.readPoint();
    ...
  }
} catch(PointNotFoudException ex) {
  throw new QueueReadingException(ex);
}

, - :

try {
  while (!queue.isEmpty()) {
    processNextPoint();
  }
} catch(PointNotFoudException ex) {
  throw new QueueReadingException(ex);
}

void processNextPoint() throws PointNotFoudException {
  Point point = queue.readPoint();
  ...
}

QueueReadingException? β€” ! ?


. , , , , (- ).

, , . . , , , . , .

, . 5 . 100 , 10% . , 100 , 50 . , ( 900 ).


, . - , . , . , . , , .

P. S. get- read. . , .

β€” (). , . .

Source: https://habr.com/ru/post/130534/


All Articles