Do you avoid using magic string when referencing property/variable names

Updated by Brady Stroud [SSW] 1 year ago. See history

123
<introEmbed body={<> Hard coded strings when referencing property and variable names can be problematic as your codebase evolves, and can make your code brittle. </>} />
(if customer.Address.ZipCode == null) throw new ArgumentNullException("ZipCode");

❌ Figure: Figure: Bad Example - Hardcoding a reference to a property

(if customer.Address.ZipCode == null) throw new ArgumentNullException(nameof(customer.Address.ZipCode));

✅ Figure: Figure: Good Example - Using nameof() operator to avoid hardcoded strings

Acknowledgements

Adam Cogan
Liam Elliott
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star