Timeline for In C# Is using private constant strings better than using strings directly in the code? [closed]
Current License: CC BY-SA 3.0
22 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 22, 2018 at 11:25 | comment | added | diegosasw | I created a post about this dilemma and explore the Common Intermediate Language (which is identical by the way). The short answer: whatever improves readability. The long answer here: diegodrivendesign.com/2018/09/… | |
| Aug 22, 2018 at 14:35 | comment | added | diegosasw | a string literal is actually a constant. There is nothing more constant than a string literal hardcoded, it won't change at runtime, it's a fixed value ...a constant. I agree with the idea of favouring readability, and a value that it's gonna be used only once it's more readable as a string literal than as a constant whose value I have to chase somewhere else. Case 2 is "better" | |
| Sep 24, 2015 at 5:23 | review | Reopen votes | |||
| Sep 24, 2015 at 15:11 | |||||
| Sep 24, 2015 at 5:06 | history | edited | GawdePrasad | CC BY-SA 3.0 | added 700 characters in body |
| Sep 23, 2015 at 21:24 | history | closed | gnat durron597 Ixrec CommunityBot | Needs details or clarity | |
| Sep 23, 2015 at 15:50 | answer | added | sara | timeline score: 2 | |
| Sep 23, 2015 at 12:16 | comment | added | gnat | Is micro-optimisation important when coding? | |
| Sep 23, 2015 at 11:21 | review | Close votes | |||
| Sep 23, 2015 at 21:24 | |||||
| Sep 23, 2015 at 11:05 | answer | added | Froome | timeline score: 11 | |
| Sep 23, 2015 at 10:52 | answer | added | Thomas Junk | timeline score: 2 | |
| Sep 23, 2015 at 10:45 | answer | added | Nathan | timeline score: 9 | |
| Sep 23, 2015 at 10:39 | comment | added | GawdePrasad | Thanks @Caleth and David Arno. I am trying here to understand what CLR does. I do understand if the string is used at multiple places then it makes total sense to use one reference string for better maintenance. Anyways I am clear that if the string is used only once, it's better not to make it a constant. | |
| Sep 23, 2015 at 10:19 | history | edited | Ben Aaronson | CC BY-SA 3.0 | added 68 characters in body |
| Sep 23, 2015 at 10:13 | comment | added | David Arno | You are micro-optimising. Is your app running too slowly, or using too many resources? If so, profile it, identify the problems and fix them. Until then, write easy-to-read and maintain code. As @Caleth says, the only thing you should be considering at this stage is whether the string is used more than once. If it is, make it a constant. If it needs to support more than one language, make it a resource. To reiterate: absolutely do not try to outsmart the compiler at this sort of level by worrying about speed and memory. | |
| Sep 23, 2015 at 10:10 | comment | added | Caleth | Consider this: You continue writing, with "Hello" used all over your class. Next week, you are asked for a Spanish version, so you have to replace "Hello" with "Hola" everywhere, vs once in the constant | |
| Sep 23, 2015 at 10:07 | comment | added | xlecoustillier | I didn't check, but I bet that if the compiler is smart enough to inline the constants value, he's also smart enough not to keep their initialization (in the heap or wherever), as it becomes useless. | |
| Sep 23, 2015 at 9:51 | comment | added | GawdePrasad | Agreed that if IL is identical then performance will be same but then does declaring the constants is an overhead? Because these constants will be in heap all the time and I do not see much benefit here of using constants. | |
| Sep 23, 2015 at 9:44 | comment | added | Caleth | You've already observed that the generated IL is identical, clearly the performance of identical IL is identical. "Better" in programming can also mean easier on you | |
| Sep 23, 2015 at 9:28 | comment | added | GawdePrasad | better in terms of memory usage and performance? | |
| Sep 23, 2015 at 9:26 | review | First posts | |||
| Sep 23, 2015 at 20:34 | |||||
| Sep 23, 2015 at 9:26 | comment | added | Caleth | what do you mean by "better"? | |
| Sep 23, 2015 at 9:24 | history | asked | GawdePrasad | CC BY-SA 3.0 |