DEV Community

Discussion on: Properties in Swift: How to Avoid Shooting Yourself in the Foot

Collapse
 
roddi profile image
Ruotger Deecke

In my opinion you should avoid computed properties whenever possible. It is exactly for the reasons that you lay out. If you see code like

let session = manager.urlSession

Would expect to get a brand new one session? No you wouldn't. That's clearly an unexpected side-effect. You would expect to always get the same.

Computed properties are evil exactly for those side-effects. But Juniors like to play with these shiny toys. …sigh…

So my rule of thumb is: If it has a side-effect make it a func with a name that clearly indicates said side-effect.