Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I find myself more and more over the year falling back to creating lots of "getOrCreateX(args)" and I must say I still haven't found a single scenario where this is worse than "get" and "create":

1) It helps with encapsculation of race conditions (you don't need to acquire locks outside to do if(get==null) { create } when you can have it in the function itself)

2) I normally don't care in my code if this is a pre-existing instance or one that I just created. I just want it now to use.

I personally find this approach superior to CQS.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: