What is the correct way of validating running code when a particular role accesses a screen?

Reducing the number of checks increases the performance of your code, so yes, check is_user_logged_in() and current_user_can() as few times as you can.

For executing functions depending on the admin page, I’d probably attach callbacks to load-{$pagenow} hook (untested):

function wpse417218_do_something_for_edit_page() {
    if ( 'page' !== get_current_screen()->id ) {
        return;
    }

    if ( ! current_user_can( 'activate_plugins' ) ) {
        return;
    }

    // Do something.
}
add_action( 'load-post.php', 'wpse417218_do_something_for_edit_page' );

This will keep everything organized, and also check the user’s capabilities only if on the appropriate page.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)