To be honest I really don't like this hack. It is hard to follow and not so easy to understand. For me better solution is if you create list of devices to reconfigure and call it recursive ( and e.g. add to args parameter to skip network restart until it is fully configured.
So in this case you can use recursion or you can split method to more methods and reuse parts to directly configure it.
What can also help you is to sort interfaces and process first bridges.