-1
\$\begingroup\$

I'm caching the value of a configuration object in a private field:

private Configuration _config;
public Congifuration Config { get { return _config = _config ?? GetConfig(); } }

Config is populated by reading a JSON file from disk, so performance is an obvious concern.

I like how concise this code is.

But I'm wary of assigning on every access. Should I be concerned about performance? And/or (in case I'm missing something) functionality?

\$\endgroup\$

closed as off-topic by πάντα ῥεῖ, Mast, Billal Begueradj, Stephen Rauch, Graipher Jun 27 '18 at 18:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Mast, Billal Begueradj, Stephen Rauch, Graipher
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ You should be more concerned about posting hypothetical code. Here, we require real code, so if you have any then please post your real world solution. \$\endgroup\$ – t3chb0t Jun 27 '18 at 17:58
  • 1
    \$\begingroup\$ I'll update with a real-world example \$\endgroup\$ – Michael Crenshaw Jun 27 '18 at 17:59
  • \$\begingroup\$ @t3chb0t I updated to make it slightly more concrete. If it's still too abstract, maybe another exchange (StackOverflow?) would be the place to go? \$\endgroup\$ – Michael Crenshaw Jun 27 '18 at 18:02
  • \$\begingroup\$ Mhmm... as a matter of fact, you could just use Lazy<T> for that or a shorter getter like Config => _config ?? (_config = GetConfig()); \$\endgroup\$ – t3chb0t Jun 27 '18 at 18:04
  • \$\begingroup\$ @t3chb0t true... I looked into that at one point. I think I passed over it because it's so verbose and seemed like overkill for my (single thread) applications. Maybe worth a second look though. \$\endgroup\$ – Michael Crenshaw Jun 27 '18 at 18:06
4
\$\begingroup\$

I'm not sure about the on-topicness of this question, it's short but it seems to be fine... I'll risk getting downvoted...


As a matter of fact, you could just use Lazy<T> for that

.ctor
{
   _config = new Lazy<Configuration>(GetConfig);
}

public Congifuration Config => _config.Value;

or a shorter getter by using =>. Additionally you can put the assignment on the right side of the ?? so it takes place only once.

public Congifuration Config => _config ?? (_config = GetConfig());

I wouldn't, however, worry about the performance. Unless you call this property millions of times I bet you cannot even measure the difference of the assignment on left or the right side.

\$\endgroup\$
  • 1
    \$\begingroup\$ Thanks much. I should have researched more before posting. This answer (and the associated question) express my concerns about the verbosity of Lazy<T> and the benefit of moving the assignment to the right of ??. On-topic or not, this will help me write better code. If it's too off-topic, I'm sure the question will get closed. Thanks again! :-) \$\endgroup\$ – Michael Crenshaw Jun 27 '18 at 18:13
  • 1
    \$\begingroup\$ _config = new Lazy<Configuration>(GetConfig()); should probably be _config = new Lazy<Configuration>(GetConfig);; doesn't look very lazy otherwise ;) \$\endgroup\$ – VisualMelon Jun 27 '18 at 18:30
  • 1
    \$\begingroup\$ @VisualMelon oh, right! Notepad coding :-) It would have been fine if I at least have written () => GetConfig() \$\endgroup\$ – t3chb0t Jun 27 '18 at 18:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.