Naming scheme for fooIfExists/ensureFoo - Round 2
Hi all, We discussed this topic 11 years ago in https://lists.webkit.org/pipermail/webkit-dev/2013-June/025056.html but we seem to have never codified it in our coding style guideline. It really bothers me that we have a mixture of two styles so I’d like to propose that we pick one style and put that into style guideline & style checker. The question is as follows. A pair of functions which lazily construct an object and one which returns nullptr if the object had not already been constructed can be named: StyleResolver* styleResolver(); StyleResolver& ensureStyleResolver(); or: StyleResolver* styleResolverIfExists(); StyleResolver& styleResolver(); Which one should we use? - R. Niwa
Hi,
On Oct 3, 2024, at 1:47 PM, Ryosuke Niwa via webkit-dev <webkit-dev@lists.webkit.org <mailto:webkit-dev@lists.webkit.org>> wrote:
The question is as follows. A pair of functions which lazily construct an object and one which returns nullptr if the object had not already been constructed can be named:
StyleResolver* styleResolver(); StyleResolver& ensureStyleResolver();
I have been using this style consistently so this is my personal preference. That said, I agree with you we should just agree on one and use it consistently (and add to coding style). Do we have stats about which pattern is more common in our codebase? Chris.
Sometimes I can’t use ensureXXX() because there is no guarantee the object will be created always. And I have to even use this pattern: Object* objectIfExists() { return m_object.get(); } Object* object(); // It try to create m_object but it still may return nullptr;
On Oct 3, 2024, at 2:33 PM, Abrar Protyasha via webkit-dev <webkit-dev@lists.webkit.org> wrote:
On Oct 3, 2024, at 14:24, Chris Dumez via webkit-dev <webkit-dev@lists.webkit.org> wrote:
Do we have stats about which pattern is more common in our codebase?
% rg "\* .+IfExists\(" -th | wc -l 95
% rg "\& ensure.+\(" -th | wc -l 176 _______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
On Oct 3, 2024, at 2:37 PM, Said Abou-Hallawa <sabouhallawa@apple.com> wrote:
Object* objectIfExists() { return m_object.get(); } Object* object(); // It try to create m_object but it still may return nullptr;
Well, this is super confusing to me and I think we should find a better pattern for this particular edge case. Maybe something with “try” in the name like we do for “tryCreate()” factory functions. `Object* object()` & `Object* tryEnsureObject()` ?
I tend to prefer ensureXXX(). It also maps nicely with HashMap::ensure. Le jeu. 3 oct. 2024 à 13:48, Ryosuke Niwa via webkit-dev < webkit-dev@lists.webkit.org> a écrit :
Hi all,
We discussed this topic 11 years ago in https://lists.webkit.org/pipermail/webkit-dev/2013-June/025056.html but we seem to have never codified it in our coding style guideline. It really bothers me that we have a mixture of two styles so I’d like to propose that we pick one style and put that into style guideline & style checker.
The question is as follows. A pair of functions which lazily construct an object and one which returns nullptr if the object had not already been constructed can be named:
StyleResolver* styleResolver(); StyleResolver& ensureStyleResolver();
or:
StyleResolver* styleResolverIfExists(); StyleResolver& styleResolver();
Which one should we use?
- R. Niwa
_______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
On 3 Oct 2024, at 1:48 pm, Ryosuke Niwa via webkit-dev <webkit-dev@lists.webkit.org> wrote:
StyleResolver* styleResolverIfExists(); StyleResolver& styleResolver();
I myself prefer this one, it’s more concise in its most common use. It’s also the style I’ve personally seen the most around the code I’m involved with. So I adapted to that style. But happy to use either.
participants (7)
-
Abrar Protyasha
-
Chris Dumez
-
Jean-Yves Avenard
-
Ryosuke Niwa
-
Said Abou-Hallawa
-
Sergio Villar Senin
-
youenn fablet