|
*points and laughs*
Pointless. Really.
The DateTime class can can convert a string to date.
Separating the both dates must be done manually, but the conversion can be done by the DateTime class.
|
|
|
|
|
I was just going through block of code for refactoring and found this. I wonder why is this even allowed in .net framework? What is benefit of it after all?
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
namespace abc{
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
namespace abc{
Ahsan Ullah
Senior Software Engineer
|
|
|
|
|
Did you paste your code twice, or is that what was actually inside the file?
|
|
|
|
|
I agree.
The using directive's only purpose is to save the developer keystrokes and obfuscate the code.
The only acceptable use for the using directive is to define an alias of a type.
And it has nothing to do with the .net framework; it's merely a compiler directive.
|
|
|
|
|
AhsanS wrote: namespace abc{
Is this really twice in the source or is it just a copy/paste mistake? If so, this code should not compile.
If not:
using statements can occur in one of two places:
- Above namespace declaration
- Inside namespace declaration, but outside any other element
This combined with the fact that duplicating a using directive is only treated as a warning not as an error, your code will compile - even if it's quite horrible indeed.
AhsanS wrote: What is benefit of it after all?
- It forces the developer to be explicit about what he's doing - always a good thing.
- Types with identical names can occur in more than one namespace. With using you can distinguish them properly.
(Take for example System.Windows.Forms.Timer vs. System.Threading.Timer ...).
Regards
Thomas
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
modified on Thursday, November 6, 2008 12:30 PM
|
|
|
|
|
Thomas Weller wrote: - It forces the developer to be explicit about what he's doing - always a good thing.
- Types with identical names can occur in more than one namespace. With using you can distinguish them properly.
(Take for example System.Windows.Forms.Timer vs. System.Threading.Timer...).
I think you have that backward.
|
|
|
|
|
I'm afraid I don't get what you mean...
Regards
Thomas
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
|
|
|
|
|
Using fully-specified names do what you say; not the using directive.
|
|
|
|
|
PIEBALDconsult wrote: Using fully-specified names do what you say; not the using directive.
There is absolutely no difference (in terms of syntactical functionality) between the two alternatives. The first is totally equivalent to the second.
The only difference is that with fully qualified type names you will end up with horrible long lines of code that say nothing more than if the programmer had used a using statement. - You will produce code that tends to become unreadable and your keyboard will soon give up...
Regards
Thomas
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
|
|
|
|
|
I didn't pasted it twice, it was like that in a file.
Ahsan Ullah
Senior Software Engineer
|
|
|
|
|
AhsanS wrote: I didn't pasted it twice, it was like that in a file.
Then indeed it's complete nonsense. Does it compile?
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
|
|
|
|
|
Yes it compiles and where the reference is used it is used as using abc.abc
it is working perfectly fine and perhaps for these sorts of issue, i am looking into each file again.
Ahsan Ullah
Senior Software Engineer
|
|
|
|
|
AhsanS wrote: where the reference is used it is used as using abc.abc
So the author of this piece of code accidentally created a nested namespace...
This is syntactically correct and being allowed to do so makes perfect sense - generally.
Of course it makes no sense at all to do it that way - I bet this is a CPD (CPD = copy paste desaster ).
Regards
Thomas
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
|
|
|
|
|
it might be CPD, but he must have known the issue when using "using abc.abc"????
Ahsan Ullah
Senior Software Engineer
|
|
|
|
|
AhsanS wrote: he must have known the issue
Maybe, maybe not.
There are some tools/functionalities out there that generate those using statements automatically for you (e.g. VS 2008 and Resharper can do that). And pressing Ctrl+Enter when Intellisense offers you something slightly strange but working (like the second abc after the first) is also simpler than thinking about the code.
So chances are he was just lazy. Just taking something that reliably works without really understanding why...
Regards
Thomas
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
|
|
|
|
|
Actually this doesn't force the developer to do anythin, on the contrary. It allows to use simple class names instead of full class names.
If you hvae a name that can refer to more thsn 1 class, you get a compiler error and forced to use the full name, regardless of using s.
But using s that are not use at the beginning of the file but at the beginning of a namespace sounds like more like an accident than anythign else.
|
|
|
|
|
Hmmmm. You'd think the compiler would catch something like that.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
|
I guess can't blame the compiler for catching it. I wonder why he (programmer) used it like that.
And i also wonder if he did it twice why not thrice
Ahsan Ullah
Senior Software Engineer
|
|
|
|
|
Try to compile following without first or second using:
using System;
namespace MyCode.Console
{
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test");
}
}
}
|
|
|
|
|
bool foo(int n)
{
if (n > 16)
return true;
else if (n <= 16)
return false;
else
return false;
}
i love this one!
|
|
|
|
|
|
this sort of thing can happen if you blindly follow the compiler checking messages
if (n > 16)<br />
return true;<br />
else if (n <= 16)<br />
return false;
would probably report that not all paths return a value
so the developer adds the else to get rid of the error. Granted with ints you should pick it up if you're not in a hurry but with more complex objects it's always possible to end up in this case without thinking.
Russell
|
|
|
|
|
Oh c'mon:
Use return (n > 16); and don't even consider thinking...
Regards
Thomas
|
|
|
|
|
bool foo(int n)<br />
{<br />
if (n > 16)<br />
return true;<br />
else if (n <= 16 && n>4)<br />
return false;<br />
else if (n <= 4)<br />
return true;<br />
}
Coding from scratch, I agree but it's late on deadline day when someone realises that the case of n<=4 no longer applies, these kind of things can happen.
|
|
|
|