Unlogical use of delay!

Why is it that some code for the Arduino or other boards like ESP32 have delay in the code so as not "to run into problems"?

Like for instance:

 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {

   if (count >= 30)

There is a 4 sec. delay and a half a second!

Sometimes it's for good reason. Maybe your attached hardware takes longer to boot than the Arduino. Sometimes it's superstition - I think I need to. Sometimes it's just ignorance - the code I copied had this. Can be tricky to tell which you have in any particular instance.

looks like they just don’t want to print so many "."s and only check status every 1/5 second

i found the with the correct ssid/passwd, wifi connects almost immediately

Well the first one is to wait for things to stabilise after power up and initialising the serial connection.

A delay at startup is not a bad idea, especially if you are interacting with other hardware which may not be as quick to initialise from a power on as you are (in this case the other end of the serial connection).

The second delay is to give a constant delay to the 30 retries (i.e. 15 seconds) before giving up and restarting.

I suppose you could do that with millis() so you weren't waiting. However as there's probably precious little (i.e. nothing) the sketch could do without a WiFi connection there seems little point.

delay() calls yield(), which handles things including watchdog.
most esp boards have CH340 as USB chip and that takes some time to wakeup. (but not 4 seconds)

esp8266 core has waitForConnectResult, to wait for connection to AP, but this while loop is good too.