Coding style: const QString foo = foo();

Sune Vuorela nospam at vuorela.dk
Tue Oct 29 08:43:06 GMT 2024


On 2024-10-28, Albert Astals Cid <aacid at kde.org> wrote:
> My vote goes for rename the variable, there's 2 reasons i can think why you do 
>
> const QString foo = foo();
>
> (there's probably many more, i spent 5 seconds thinking)
>
> Reason 1: You're caching it because foo is expensive, rename the variable to 
> cachedFoo
>
> Reason 2: You're remembering it because you're going to change it and then 
> later want to compare against it, rename it to oldFoo

Reason 1a: You cache it because you use it multiple times in the
function and it gets annoying to type the parenthesis and/or you don't
want anything you do to by accident change the foo() you are evaluating.

Thanks for your feedback. I'm hoping for a bit more voices before
heading somewhere with some MR's.

/Sune



More information about the kde-devel mailing list