Example: Why does .NET promote bad programming practices?
-
I understand that the below is more efficient but using Exceptions as a point of logic is not acceptable! Exceptions are for unexpected things that may happen but shouldn't happen. Don't get me started about the underlying method in .NET for TryParse catching an exception. When did laziness and poor practice become a valid point of view? int userInputId; try{ userInputId = Int64.Parse(txtUser.Text); } catch{ //Not set use default userInputId = 0; }
File Not Found
Ennis Ray Lynch, Jr. wrote:
but using Exceptions as a point of logic is not acceptable!
That was one of my beefs with the Parse methods, that I couldn't get access to the underlying validator to test the string without having Parse throw an exception. I raised (harhar) this issue at one of the "meet the developers" meetings and they said it was being addressed in .NET 2.0. I felt that was a good thing. TryParse addresses that issue, IMO. Throwing an exception is expensive, and in some cases I'd rather:
int userInputId=0;
bool success=Int64.TryParse(someText);Similarly, I don't like writing code like:
if (container.Contains(myKey))
but prefer:
bool success=container.TryGetValue(key, out val);
Because there are many cases when it's OK if the container doesn't contain the key. 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 -
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 SmithI don't think bad programming is language or framework specific. Also your example is contrived and I'm sure if I put my mind to it I could contrive an example of a perfectly acceptible and required use of a string in a switch statement. Sure I'd use an enum or something and I've learned to use strings properly over the years, but I have the benefit of experience from making mistakes like that back in the day.
-
I understand that the below is more efficient but using Exceptions as a point of logic is not acceptable! Exceptions are for unexpected things that may happen but shouldn't happen. Don't get me started about the underlying method in .NET for TryParse catching an exception. When did laziness and poor practice become a valid point of view? int userInputId; try{ userInputId = Int64.Parse(txtUser.Text); } catch{ //Not set use default userInputId = 0; }
File Not Found
-
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 SmithI guess, in the example, we do not know the state of the switch string (myString). However, if myString was checked for formatting prior to the switch (such as a result from a .Split or RegEx, then it would already be in the pattern to be used. In this example, it would be case sensitive and that may be what is desired (only time I would think a person would use such code). If the coder was checking keywords and did not want case sensitivity, they would simply use lower case 'case' parameters and do a ToLower on the string prior to the switch. Personally, I do not see switching on strings as a bad thing as long as proper thought goes into the desired results.
Rocky <>< Latest Code Blog Post: New enhancements to VS WYSIWYG! Latest Tech Blog Post: Scratch: fun for all ages for free!
-
I disagree, tryparse is very valuable in user interface programming. Or are you saying that tryparse is internally coded just to parse and catch an exception instead of validating it before parsing it without an exception.
Internally it throws an exception, I can do that! (or so I have heard)
File Not Found
-
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 SmithBefore .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
-
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 Smithif (_tcscmp(s, _T("Hello") == 0)
{
...
}
else if (_tcscmp(s, _T("Goodbye") == 0)
{
...
}A language should make good practice easy, but not stop at what is generalyl perceived a bad practice.
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! -
Internally it throws an exception, I can do that! (or so I have heard)
File Not Found
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.
-
I understand that the below is more efficient but using Exceptions as a point of logic is not acceptable! Exceptions are for unexpected things that may happen but shouldn't happen. Don't get me started about the underlying method in .NET for TryParse catching an exception. When did laziness and poor practice become a valid point of view? int userInputId; try{ userInputId = Int64.Parse(txtUser.Text); } catch{ //Not set use default userInputId = 0; }
File Not Found
This annoys me greatly as well--it one app I was working on, it caused a noticeable performance drop. I ended up rolling my own parsers that never threw exceptions (and ended up being faster with the non-exception cases as well.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
if (_tcscmp(s, _T("Hello") == 0)
{
...
}
else if (_tcscmp(s, _T("Goodbye") == 0)
{
...
}A language should make good practice easy, but not stop at what is generalyl perceived a bad practice.
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!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...
-
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