Any program is like a photon

Any program is like a photon

Have you ever had such a thing that one fine morning, over a cup of coffee and your favorite livers, you suddenly remember that you have a dead line today? Then, by lightly pressing the button on the case, you turn on your work computer, enter the password and start the Discord program, where the customer has been waiting for you for about half an hour.
Surprisingly, you made it to the grandpa! Congratulations, you were able to meet the deadline for the first time in a long time. You are calm, the feeling of unfinished homework is not haunting you (at least when completing this order), your heart beats at a rate of 90 oscillations per minute, and your cat lies sweetly in your lap. It’s easy to catch nirvana at such moments, isn’t it?
But at some point, it will be time to check your five-day job, get your hard-earned money and go pamper your cat. You open the Zakaz freelance(26) folder with two simple clicks, simultaneously launching Visual Studio Code. Next comes the business that is absolutely familiar to all of us: checking the constants, running the code, and most importantly, the prayer that you read in the console, because entering it in the console has already become a habit for you. And it would seem that everything is going well, the customer is quite looking at the result of your efforts, when suddenly… You get a banal error that you can correct, but you cannot explain it to the customer. Panic begins, stress, the desire to invent a time machine to move a couple of hours ago to correct the error of the previously planned dialogue. But, unfortunately, you are clearly not into such an invention.
The customer, smoking slowly, says the harshest and scariest words in your life: “Work.” After that, he hangs up, and you go to run the code again in a panic to make sure of the error. By checking all the actions again, the program performs all the actions that you have prepared, all the functions are used, the program is executed in the previously measured time. There is no mistake.

If this has not happened to you, it will definitely happen. In this article, we will analyze why this can happen and any program is ambiguous.

Why like a photon?

He named this article in honor of a popular experiment where a change in the behavior of photons is observed in the presence of an observer.

In our case, the customer is the observer, and the program is the photon. Interesting analogy, isn’t it?

MR

PAN is an acronym of three words (Repetition, Alternative, Observation) that I once heard from a programmer I know. This term is not particularly introduced in the community of programmers, but by following three simple rules, you can greatly reduce the likelihood of unexpected errors occurring the day before.

Repetition

What do I mean? For example, you work with the python language and the selenium library.
If you’ve ever worked with this library, you know that even in perfect code, some error will pop up. And it doesn’t matter how much you corrected, because anything can happen. For example, an unstable connection, too fast loading, a window that accidentally appeared on the page, and other unexpected actions that cause an error when working with the browser driver. Most such errors are solved simply, you just need to reload the driver. It seems banal.
So, the first solution is to repeat the action. Yes, it’s corny, but really effective. The code might look something like this:

def func():
  while True: # цикл нужен, чтобы в любом случае повторять действие вне зависимости от ошибки
    try:
      # код
      # 
      # /код
      break
    except:
      print("Error. I repeat the action")

Yes, this very simple code already solves many of your problems. Personally, I thought of committing approximately also very recently.
However, experienced programmers will definitely not be surprised by this 😀

Alternative

If we give the same example with Selenium, then it is worth understanding that it is always worth looking for an alternative to a sick solution. Repetition will not be able to help in solving a certain task, if the solution itself is not correct. using the same try-except construct, make several alternative solutions.

try:
  # код
  #
  # /код
  print("Решение 1")
except:
  try:
    # код
    #
    # /код
    print("Решение 2")
  except:
    # код
    #
    # /код
    print("Решение 3")

It is also trite, but many people forget about such a solution to some problems.

Observation

And suppose the problem is not so obvious at first glance? Remember that observation before the dead line is very important. It is necessary to analyze absolutely every detail, from how much RAM is required for the full operation of your program to the change in the parsing resource that is observed, for example. This is also an important detail. Everyone should watch.

Results

Yes, I do not argue, it is quite likely that you think that it is all too banal and well-worn, a real programmer will accurately predict absolutely every detail, and similar constructions are in any code. But you’d be surprised if you look at the number of people who don’t do at least one item.
Note that I also did not propose a too complicated solution. If you see small details in the program here, then yes, it is. But adding such a solution, especially improving it, will be appropriate in almost any code.

Learning to code cannot teach you to be an expert, any more than learning brushes and paints can turn anyone into an artist.– Eric S. Raymond

Related posts