Example: Why does .NET promote bad programming practices?
-
Really? I did not know that. Hmm..that seems like not a very good thing. I was under the assumption that it didn't throw an exception internally. Weird. I guess next chance I get I'm going to have to lookup the source for that and see. Hopefully it at least does some sanity checking without exception.
-
OK, this is a more language specific post, but the VB thread below got me thinking (a bad thing, I know). For example, being able to do:
switch(myString)
{
case "Hello":
...
case "Goodbye":
...
}is a perfect example of how the language promotes bad programming. The result is a case-sensitive, culture-specific, difficult to extend, and probably format specific within the context of the whole parser, implementation. Now, I personally don't feel a language should (or even can) enforce good programming practices. But a language that supports a bad practice certainly doesn't help the situation. Anyone want to contribute other examples or thoughts? Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith'cause it is useful when you need it to get a quick job done. There is a lot to be said about premature optimisation and astronaut architects.
regards, Paul Watson Ireland & South Africa
Shog9 wrote:
And with that, Paul closed his browser, sipped his herbal tea, fixed the flower in his hair, and smiled brightly at the multitude of cute, furry animals flocking around the grassy hillside where he sat coding Ruby on his Mac...
-
Before .NET, there was a long running battle between Visual C++ and Visual Basic. The former had the power, but the latter had millions more users because it was easier to use and provided quicker development, at least for the first 80% of the system. It was an obvious attempt to make computer programming more accessible to people who might not be able to handle more complex languages and scenarios. And it worked. Before long, everybody and his kid sister was a software developer because they could kick out a few forms in VB, and this is the root of the prejudice against it. So, it's important to keep things in perspective. VB was not targeting the rocket science crowd who was completely immersed in "good programming." It was intended to empower anyone who could click on a mouse, and make them a programmer. Not a "good" programmer. Just a programmer. The fact that there are excellent programmers who happen to use VB is a testimony to the individual, not to the language. Oh, sorry, you were talking about .NET, right? Remember the initial comments about the battle between VB and VC++? Visual Basic won, hands down, and now it completely dominates the Windows development community. .NET was evolutionary, not revolutionary. It was simply the natural evolution of the Visual Basic IDE and development paradigm, extended to deal with the Internet. C# is nothing more than Visual Basic.NET with a different syntax, and C++ is now a completely second class citizen. The job postings in any city will verify this. In a world where the dominant development platform comes from a lineage of empowering non-programmers, I'm not sure why you would expect the languages to promote "good programming practices." It is what it is.
Author of The Career Programmer and Unite the Tribes www.PracticalStrategyConsulting.com
Now we all know why a democracy will never work. (before I get totally flamed anyone that thinks the U.S. was ever a democracy should have failed civics)
File Not Found
-
Does that even compile?
regards, Paul Watson Ireland & South Africa
Shog9 wrote:
And with that, Paul closed his browser, sipped his herbal tea, fixed the flower in his hair, and smiled brightly at the multitude of cute, furry animals flocking around the grassy hillside where he sat coding Ruby on his Mac...
He missed a couple right parentheses ')'. I dare you to put a message in the Suggestions forum asking Chris for a C++ syntax checker in the post submission gizmo :-D.
Software Zen:
delete this;
-
OK, this is a more language specific post, but the VB thread below got me thinking (a bad thing, I know). For example, being able to do:
switch(myString)
{
case "Hello":
...
case "Goodbye":
...
}is a perfect example of how the language promotes bad programming. The result is a case-sensitive, culture-specific, difficult to extend, and probably format specific within the context of the whole parser, implementation. Now, I personally don't feel a language should (or even can) enforce good programming practices. But a language that supports a bad practice certainly doesn't help the situation. Anyone want to contribute other examples or thoughts? Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh SmithMarc Clifton wrote:
The result is a case-sensitive, culture-specific, difficult to extend, and probably format specific within the context of the whole parser, implementation.
if the strings are limited to a small set of pre-defined values, none of that matters. in that case, the only thing separating that from a numeric switch is the cost of a string compare vs a simple numeric compare. and it might be acceptable to pay that price.
image processing toolkits | batch image processing | blogging
-
He missed a couple right parentheses ')'. I dare you to put a message in the Suggestions forum asking Chris for a C++ syntax checker in the post submission gizmo :-D.
Software Zen:
delete this;
Spent all the day crunching out specs... My coding skills deteriorate quickly.
Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
Linkify!|Fold With Us! -
OK, this is a more language specific post, but the VB thread below got me thinking (a bad thing, I know). For example, being able to do:
switch(myString)
{
case "Hello":
...
case "Goodbye":
...
}is a perfect example of how the language promotes bad programming. The result is a case-sensitive, culture-specific, difficult to extend, and probably format specific within the context of the whole parser, implementation. Now, I personally don't feel a language should (or even can) enforce good programming practices. But a language that supports a bad practice certainly doesn't help the situation. Anyone want to contribute other examples or thoughts? Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh SmithAlthough I tend to try to stay away form case statements that are on anything other than numbers (or enums as the case may also be) I can see where that would have it usefulness. It is difficult, as it is in any syntactical/grammatical base question, to say outright if it is 'wrong' without some surrounding context. I will admit that I have done somewhat similar things in cases where I asked the user to verify with an exact text string that they want to do something (IE: delete a record form a database). Of course once again you have to put it into context. My app was not going to be used outside the company so I had no language/culture problems to deal with that would have made string comparisons a problem. It was a simple solution to a very narrow problem.
-
OK, this is a more language specific post, but the VB thread below got me thinking (a bad thing, I know). For example, being able to do:
switch(myString)
{
case "Hello":
...
case "Goodbye":
...
}is a perfect example of how the language promotes bad programming. The result is a case-sensitive, culture-specific, difficult to extend, and probably format specific within the context of the whole parser, implementation. Now, I personally don't feel a language should (or even can) enforce good programming practices. But a language that supports a bad practice certainly doesn't help the situation. Anyone want to contribute other examples or thoughts? Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh SmithMarc Clifton wrote:
Now, I personally don't feel a language should (or even can) enforce good programming practices. But a language that supports a bad practice certainly doesn't help the situation.
Yeah, but I really like of the freedom of choice.
Marc Clifton wrote:
Anyone want to contribute other examples or thoughts?
Once I have suggested in MSDN Feedback Center this language improvement (in bold):
for(int i=0; i<10; i++) as externalFor
{
for(int j=0; j<10; j++) as middleFor
{
for(int k=0; j<10; k++) as innerFor
{
if (someCondition)
{
break middleFor;
}
if (anotherCondition)
{
continue externalFor;
}
}
}
}and they told me to use goto statements... X|
Engaged in learning of English grammar ;)
For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger: -
'cause it is useful when you need it to get a quick job done. There is a lot to be said about premature optimisation and astronaut architects.
regards, Paul Watson Ireland & South Africa
Shog9 wrote:
And with that, Paul closed his browser, sipped his herbal tea, fixed the flower in his hair, and smiled brightly at the multitude of cute, furry animals flocking around the grassy hillside where he sat coding Ruby on his Mac...
Phrase "astronaut architects" is not known to be, but I think I agree :) Using TryParse when dealing with user input is usually sufficient. If TryParse methods are not fast enough, by all means implement yours. edit: oops, didn't see you responded to original post and not TryParse debate above. Well, surprisingly my post didn't lost its meaning. :->
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
Interesting, this is what my Reflector shows: private static bool TryStringToNumber(string str, NumberStyles options, ref Number.NumberBuffer number, NumberFormatInfo numfmt, bool parseDecimal);
if (!Number.ParseNumber(ref chPtr2, options, ref number, numfmt, parseDecimal) || ((((long) ((chPtr2 - chPtr1) / 2)) < str.Length) && !Number.TrailingZeros(str, (int) ((long) ((chPtr2 - chPtr1) / 2))))) { return false; }
private static void StringToNumber(string str, NumberStyles options, ref Number.NumberBuffer number, NumberFormatInfo info, bool parseDecimal);
if (!Number.ParseNumber(ref chPtr2, options, ref number, info, parseDecimal) || ((((long) ((chPtr2 - chPtr1) / 2)) < str.Length) && !Number.TrailingZeros(str, (int) ((long) ((chPtr2 - chPtr1) / 2))))) { throw new FormatException(Environment.GetResourceString("Format_InvalidString")); }
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
OK, this is a more language specific post, but the VB thread below got me thinking (a bad thing, I know). For example, being able to do:
switch(myString)
{
case "Hello":
...
case "Goodbye":
...
}is a perfect example of how the language promotes bad programming. The result is a case-sensitive, culture-specific, difficult to extend, and probably format specific within the context of the whole parser, implementation. Now, I personally don't feel a language should (or even can) enforce good programming practices. But a language that supports a bad practice certainly doesn't help the situation. Anyone want to contribute other examples or thoughts? Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh SmithOf course, if case isn't an issue, you could just do the equivalent of MFC's
MakeUpper()
orMakeLower()
and then run through your switch statement. However, that does not address the issue of language unless you can put the strings into a resource file or something, but then you'd be able to run your switch on integers instead of text. Still, having a switch statement for text is the next best thing to stupid I've seen in a long time."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Of course, if case isn't an issue, you could just do the equivalent of MFC's
MakeUpper()
orMakeLower()
and then run through your switch statement. However, that does not address the issue of language unless you can put the strings into a resource file or something, but then you'd be able to run your switch on integers instead of text. Still, having a switch statement for text is the next best thing to stupid I've seen in a long time."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001John Simmons / outlaw programmer wrote:
unless you can put the strings into a resource file or something, but then you'd be able to run your switch on integers instead of text.
not if, for example, those strings are coming from database records, and there's no numeric equivalent in the record (so you can't just use the number instead of the string), and there's no way you can change the DB schema (because too many things would break). you can do a string compare on each record and generate a numeric code to carry around along with the recordset, to use that in your switch statements. but that just moves the string compare - it doesn't eliminate it. sometimes the best you can do is Make It Work.
image processing toolkits | batch image processing | blogging
-
OK, this is a more language specific post, but the VB thread below got me thinking (a bad thing, I know). For example, being able to do:
switch(myString)
{
case "Hello":
...
case "Goodbye":
...
}is a perfect example of how the language promotes bad programming. The result is a case-sensitive, culture-specific, difficult to extend, and probably format specific within the context of the whole parser, implementation. Now, I personally don't feel a language should (or even can) enforce good programming practices. But a language that supports a bad practice certainly doesn't help the situation. Anyone want to contribute other examples or thoughts? Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh SmithSince strings in .NET are unicode, the string "Hello" is basically a byte[10] array with fixed, compile-time known value. This is not a language issue or compiler issue at all because instead of 'case 0: case 1: case 234:', you have a string representative of the underlying values, which is much more readable. In terms of case-sensitive and culture issue, it really depends on where the given string comes from and what does it represent. For instance, in a localized app, regardless of the culture, you will need a culture-unspecific id to identify a control/component. That id could be a GUID, an integer, or a string. From your code sample, I will presume that the given 'myString' is properly normalized with correct cap and trimming. If this isn't the case, then it is the programmer's problem, never the tool.
-
OK, this is a more language specific post, but the VB thread below got me thinking (a bad thing, I know). For example, being able to do:
switch(myString)
{
case "Hello":
...
case "Goodbye":
...
}is a perfect example of how the language promotes bad programming. The result is a case-sensitive, culture-specific, difficult to extend, and probably format specific within the context of the whole parser, implementation. Now, I personally don't feel a language should (or even can) enforce good programming practices. But a language that supports a bad practice certainly doesn't help the situation. Anyone want to contribute other examples or thoughts? Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh SmithIt is very difficult to find the ballance between enforcing good practices and giving developers more control. The example you posted can be dangerous, but it is perfectly correct in certain situations (as Rocky Moore mentioned). Interesting extension to the swith control structure are Active patterns available in F#: http://blogs.msdn.com/dsyme/archive/2006/08/16/ActivePatterns.aspx[^]. In simple words, active patterns make it is possible to write patterns (rules) that are executed when "case" statement is reached. This rule can than decide whether case statement should be executed. Of course this can be misused, but I think that it would be interesting extension to main-stream programming languages.
// You could then use something like this..:
switch(str)
{
case CaseSensitiveMatch("something"):
... break;
case CaseInsensitiveMatch("SomeThinG"):
... break;
case CaseInsensitiveContains("thing"):
... break;
}Actually working with strings directly is always potential source of problems (with localization), but I think that active patterns could be useful in many other sitations.
Homepage: TomasP.net | Photo of the month: Calendar | C# and LINQ, F#, Phalanger: My Blog
Latest article: Phalanger, PHP for .NET: Introduction for .NET developers -
Interesting, this is what my Reflector shows: private static bool TryStringToNumber(string str, NumberStyles options, ref Number.NumberBuffer number, NumberFormatInfo numfmt, bool parseDecimal);
if (!Number.ParseNumber(ref chPtr2, options, ref number, numfmt, parseDecimal) || ((((long) ((chPtr2 - chPtr1) / 2)) < str.Length) && !Number.TrailingZeros(str, (int) ((long) ((chPtr2 - chPtr1) / 2))))) { return false; }
private static void StringToNumber(string str, NumberStyles options, ref Number.NumberBuffer number, NumberFormatInfo info, bool parseDecimal);
if (!Number.ParseNumber(ref chPtr2, options, ref number, info, parseDecimal) || ((((long) ((chPtr2 - chPtr1) / 2)) < str.Length) && !Number.TrailingZeros(str, (int) ((long) ((chPtr2 - chPtr1) / 2))))) { throw new FormatException(Environment.GetResourceString("Format_InvalidString")); }
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
'cause it is useful when you need it to get a quick job done. There is a lot to be said about premature optimisation and astronaut architects.
regards, Paul Watson Ireland & South Africa
Shog9 wrote:
And with that, Paul closed his browser, sipped his herbal tea, fixed the flower in his hair, and smiled brightly at the multitude of cute, furry animals flocking around the grassy hillside where he sat coding Ruby on his Mac...
And then the "quick job" gets used a lot and built upon and (before you know it) that quick hack is the corner stone of a dreadful big monolithic application...
-
Phrase "astronaut architects" is not known to be, but I think I agree :) Using TryParse when dealing with user input is usually sufficient. If TryParse methods are not fast enough, by all means implement yours. edit: oops, didn't see you responded to original post and not TryParse debate above. Well, surprisingly my post didn't lost its meaning. :->
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
I first heard about it from Joel[^].
regards, Paul Watson Ireland & South Africa
Shog9 wrote:
And with that, Paul closed his browser, sipped his herbal tea, fixed the flower in his hair, and smiled brightly at the multitude of cute, furry animals flocking around the grassy hillside where he sat coding Ruby on his Mac...
-
OK, this is a more language specific post, but the VB thread below got me thinking (a bad thing, I know). For example, being able to do:
switch(myString)
{
case "Hello":
...
case "Goodbye":
...
}is a perfect example of how the language promotes bad programming. The result is a case-sensitive, culture-specific, difficult to extend, and probably format specific within the context of the whole parser, implementation. Now, I personally don't feel a language should (or even can) enforce good programming practices. But a language that supports a bad practice certainly doesn't help the situation. Anyone want to contribute other examples or thoughts? Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh SmithSwitching on strings is a good feature, but to be used sparingly. Many years ago I had need of reading in some reports and processing the data, having only Vax-C as a tool. I found that because a character constant could actually hold two characters I could switch on the first two characters of each line, something like:
FILE* file ;
char* buffer ;
char firsttwocharacters ;...
readaline ( file , buffer ) ;
firsttwocharacters = *buffer ;
switch ( firsttwocharacters )
{
case 'AB' : ...
case 'CD' : ...
...
}
...And I don't use TryParse, I've read articles like this one: http://www.codeproject.com/dotnet/ExceptionPerformance.asp[^]
-
OK, this is a more language specific post, but the VB thread below got me thinking (a bad thing, I know). For example, being able to do:
switch(myString)
{
case "Hello":
...
case "Goodbye":
...
}is a perfect example of how the language promotes bad programming. The result is a case-sensitive, culture-specific, difficult to extend, and probably format specific within the context of the whole parser, implementation. Now, I personally don't feel a language should (or even can) enforce good programming practices. But a language that supports a bad practice certainly doesn't help the situation. Anyone want to contribute other examples or thoughts? Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh SmithFirst thing one learns in a Test Driven Development environment is: switch statements = bad bad bad.