LBYL vs EAFP

Содержание
Введение
LBYL
EAFP
Похожие статьи

Введение

LBYL - Look before you leap

Проверяй прежде чем делать. Семь раз отмерь один раз отрежь



EAFP - Easier to ask forgiveness then permission

Проще извиниться чем спросить разрешение

LBYL

LBYL на примере Python

# Process file: LBYL import os p = '/path/to/datafile.dat' if os.path.exists(p): process_file(p) else: print(f'No such file as {p}')

Перед тем как обрабатывать файл проверяем, что путь до него существует.

Проблемы:

Проверили только путь. А что если файл содержит какой-то мусор?

Что если путь указывает на директорию?

Получается, что нужно сделать ещё две проверки.

И даже в этом случае есть возможная проблема - гонка - что если файл удалят между проверкой и открытием? Получается, что нужно обрабатывать исключение в любом случае.

EAFP

EAFP на том же примере из Python

# Process file: EAFP p = '/path/to/datafile.dat' try: process_file(f) except OSError as e: print(f'Error: {e}')

Проверяем получилось или нет, если нет обрабатываем исключение

Article author: Andrei Olegovich

Похожие статьи
Теория
Программирование
Boilerplate код
Виды копирования
Время работы алгоритма

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: