|
Deflinek wrote: Apparently readability is a personal preference Sorry, but it is not; it is just that, a preference. One that may have many motivations, but not speed of interpretation.
Deflinek wrote: Is not overly verbose and makes it clear that you intend to assign something an empty string. It is even against the DRY-principle; it is merely another alias for "".
Deflinek wrote: It is more readable No, you are mixing intent with values; without caring about the intent, reading the damn thing, interpreting what it says, takes more time. The more of that clutter in your code, the more time required to comprehend what it does.
And still there is no argumentation for the aliassing of an empty string literal.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Well, MSDN[^] says String.Empty is "".
MSDN says: The value of this field is the zero-length string, "".
So that sounds to me like they are 100% the same (apart from the readability of course )
|
|
|
|
|
While I agree, StyleCop does not. It may be right, it is different to fetch the value of a field than to compare to a literal.
V. wrote: apart from the readability of course Of course, it needs no explanation on the superiour version of encoding information. I can only conclude that the rule is incorrect - I am glad we agreed on the subject
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
OK, let's see if this works. Based on a real question from QA.
Copy and paste the following code block into Visual Studio:
string s = "";
Console.WriteLine(s.Length);
Now explain why the output is 1 .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Non-printable characters.
Three of them, according to the hex-editor
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I always go with "" now. There are some situations where string.Empty is disallowed, because it is not a compile-time constant (which itself is a major weakness of C# in my eyes - the lack of const-correctness and compile-time constructs).
So, as I sometimes have to use "" anyway, I eventually just gave up on string.Empty, so at least my usage pattern is consistent.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
There are contexts in which "" is the only acceptable form, as C# has no compile-time constants so string.Empty is invalid in some contexts.
I can never remember exactly when it is invalid, so eventually gave up and started using "" everywhere.
(Side rant - one of C#'s major failings in my opinion is the inability to express const-correctness, immutability, and compile-time constants - it leaves potential to have incorrect implementations of interface functions etc.)
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
A bit late, but: Don't use function names with "empty"[^]
If you don't feel like checking out the article, the gist of this item is that the word, "empty" is ambiguous: it can refer to either an action on, or the state of an object. While in your particular example the use of "Empty" seems clear enough, it's inherent ambiguity still makes you hesitate for a moment, whereas the meaning of "" is instantly clear.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Eddy Vluggen wrote: Since when are two words more "readable" than the two symbols that half the world knows and uses? The word "hard coded" is equally out of place - it feels like it means to warn that the constant "might" change and that it therefore must be wrong to hard-code it.
In the beginning, I used string.Empty, until I tried to use one in a switch block, and the compiler balked, because it is not seen as a true constant. Hence, I created a true constant, EMPTY_STRING , and made sure that it is in my root namespace, which I import into virtually every project more complicated than Hello, World. There are many more such goodies where that one lives, in my my DLLServices2 Class Library on GitHub.
David A. Gray
Delivering Solutions for the Ages, One Problem at a Time
Interpreting the Fundamental Principle of Tabular Reporting
|
|
|
|
|
It becomes more readable when you use it as a standard, it isn't just string that supports Empty and your own classes should too (if relevant).
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public static Person Empty
{
get { return new Person {ID = 0, Name = string.Empty}; }
}
}
class Program
{
static void Main(string[] args)
{
string myString = string.Empty;
Guid myGuid = Guid.Empty;
Person myPerson = Person.Empty;
Console.ReadLine();
}
}
|
|
|
|
|
From my point of view string.Empty conveys the intention that the string is not to be null and have no characters.
Seeing a magic value like "", and let's face it - it IS a magic value albeit a simple one, always makes me question the code: "Is that supposed to be like that, or was there some value forgotten in debugging?"
I have yet to encounter having to use an empty string as a case in a switch, but now that I know I will try to avoid that, or use a const value.
|
|
|
|
|
sibling123 wrote: From my point of view string.Empty conveys the intention that the string is not to be null and have no characters. So do two quotes.
sibling123 wrote: Seeing a magic value like "", Two quotes are no magic value, but a global constant expression.
sibling123 wrote: I will try to avoid that, or use a const value. Duh. SA1122 is simply nonsense.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
but I just devised a cunning C macro. I may not be the first to devise this, but I'm likely the only one this decade.
Many-a-day in years past I bemoaned that the argv array includes the name of the running executable and the resultant off-by-one confusion it caused me.
So I was over-joyed to see that C# does away with that nonsense.
Then today I was dabbling in some minor C code and so my depression returned.
It occurred to me that all I wanted was something like the SHIFT command (DOS), so after a few minutes' thought, I wrote:
# define SHIFT (++argv,--argc)
I wrote it that way so it can be used in either of the following ways:
SHIFT ;
if ( argc > 0 ) { ... }
while ( SHIFT > 0 ) { ... }
switch ( SHIFT )
{
case 0 : { ... ; break ; }
case 1 : { ... ; break ; }
case 2 : { ... ; break ; }
...
default : { ... ; break ; }
}
|
|
|
|
|
I don't use C (or C++) much, though.
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
|
Is this for some place where you can't use getopt? (like not supported by the compiler, or want to keep the binary size small?)
|
|
|
|
|
No, but I expect it can be used with it if you want, but that looks dreadful.
|
|
|
|
|
That was exactly my thought when I looked at getopt, but its somehow become the established standard for command-line processing on Unix-like systems.
But then, that's elegant compared to the dog's do-do that is autoconf and the m4 macro-processor.
Shudders.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
Just found this:
private void App_Exit(object sender, ExitEventArgs e)
{
ConnectionManager.IConnectionManager.Shutdown()
Stopwatch sw = new Stopwatch();
sw.Start();
while (sw.Elapsed < TimeSpan.FromMilliseconds(4000))
{
}
sw.Stop();
}
Guess the dev didn't know about
System.Threading.Thread.Sleep(4000);
If it's not broken, fix it until it is
modified 13-May-16 14:04pm.
|
|
|
|
|
But with "Sleep" you get no Attention the Task Manager ...
modified 19-Jan-21 21:04pm.
|
|
|
|
|
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
Shouldn't burden of waiting for the disconnect to happen be placed on the Shutdown method? At most the caller should specify the timeout:
ConnectionManager.IConnectionManager.Shutdown(4000)
|
|
|
|
|
I found almost this exact code in the splashscreen of an app I inherited responsibility for....
"The app takes too long to start" was the complaint from the customer. In fact the splashscreen sat there burning up CPU cycles as fast as possible performing "sw.Elapsed.TotalMilliseconds < 10000". Then once the 10 seconds had elapsed it closed the splashscreen and *then* started all the application init tasks (DB connection, load config etc...)
I replaced the stopwatch with:
- a background thread to perform the app init (which normally took about 7 seconds)
- a thead.sleep to "pad" the splashscreen's lifespan up to 10 seconds
Apparently a 10-second splashscreen was desired, but cramming the app init into that 10 seconds cured the customer's "slow app" nicely.
|
|
|
|
|
I always thought the purpose of the splashscreen was to distract the user from how long the app took to get initialised. Back in the day when I did such things, my strategy was for the mainline of the app to throw out a splashscreen, do the grunt work of initialisation then kill the splashscreen.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Intent and reality intersect far too rarely.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|