Leaving a MacBook unlocked on a desk at work or in a café is how people end up with a funny message sent from their account. The fix is two seconds of habit. macOS has a lock shortcut, and it’s one key away from the shortcut that quits an app.
Press Control-Command-Q. The screen locks right away and the Mac needs your password, Touch ID or your Apple Watch to unlock. Your apps keep running, so a download or a long build carries on while you’re gone.
Control-Command-Q lock your screen
Shift-Command-Q log out (asks to confirm)
Option-Shift-Command-Q log out right away
An example: you get up for a coffee, press Control-Command-Q while standing up, and the lock screen is there before you’ve pushed in your chair. If you have an Apple Watch, as I do with my Apple Watch Ultra, unlocking can be a matter of sitting back down once you’ve turned on unlocking with your watch. It’s the same idea as locking your phone before you put it on the table.
The caveat is the neighbours of that shortcut. Command-Q without Control quits the front app, and Shift-Command-Q starts a log out, which closes all your apps. Apple notes that you may need to hold some of these shortcuts a little longer than others, to avoid triggering them by accident, so give it a firm press. A lock also doesn’t help if your Mac never asks for a password: check the Lock Screen settings to see how soon one is required after the screen saver or sleep.
Further reading: Mac keyboard shortcuts (Apple Support)