Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Example: Why does .NET promote bad programming practices?

Example: Why does .NET promote bad programming practices?

Scheduled Pinned Locked Moved The Lounge
questioncsharpcomhelptutorial
43 Posts 25 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    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

    Thyme In The Country

    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

    R Offline
    R Offline
    Ray Cassick
    wrote on last edited by
    #19

    Although 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.


    My Blog[^]
    FFRF[^]


    1 Reply Last reply
    0
    • M Marc Clifton

      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

      Thyme In The Country

      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

      C Offline
      C Offline
      Clickok
      wrote on last edited by
      #20

      Marc 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:

      1 Reply Last reply
      0
      • P Paul Watson

        '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...

        D Offline
        D Offline
        DavidNohejl
        wrote on last edited by
        #21

        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

        P 1 Reply Last reply
        0
        • E Ed Poore

          Tis true I'm afraid, I was  X| when I looked at the source with Reflector.

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #22

          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

          E 1 Reply Last reply
          0
          • M Marc Clifton

            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

            Thyme In The Country

            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

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #23

            Of course, if case isn't an issue, you could just do the equivalent of MFC's MakeUpper() or MakeLower() 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

            C 1 Reply Last reply
            0
            • realJSOPR realJSOP

              Of course, if case isn't an issue, you could just do the equivalent of MFC's MakeUpper() or MakeLower() 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

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #24

              John 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

              1 Reply Last reply
              0
              • M Marc Clifton

                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

                Thyme In The Country

                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

                J Offline
                J Offline
                jpg 0
                wrote on last edited by
                #25

                Since 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.

                1 Reply Last reply
                0
                • M Marc Clifton

                  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

                  Thyme In The Country

                  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

                  T Offline
                  T Offline
                  Tomas Petricek
                  wrote on last edited by
                  #26

                  It 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

                  1 Reply Last reply
                  0
                  • D DavidNohejl

                    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

                    E Offline
                    E Offline
                    Ed Poore
                    wrote on last edited by
                    #27

                    Hmm, must have been mistaken then, where did I see that code then... :suss:

                    T D 2 Replies Last reply
                    0
                    • E Ed Poore

                      Hmm, must have been mistaken then, where did I see that code then... :suss:

                      T Offline
                      T Offline
                      tgrt
                      wrote on last edited by
                      #28

                      I remember seeing it in Reflector too, so don't feel bad. Maybe it changed with SP1?

                      E 1 Reply Last reply
                      0
                      • P Paul Watson

                        '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...

                        S Offline
                        S Offline
                        Stuart Dootson
                        wrote on last edited by
                        #29

                        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...

                        P 1 Reply Last reply
                        0
                        • D DavidNohejl

                          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

                          P Offline
                          P Offline
                          Paul Watson
                          wrote on last edited by
                          #30

                          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...

                          1 Reply Last reply
                          0
                          • M Marc Clifton

                            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

                            Thyme In The Country

                            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

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #31

                            Switching 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[^]

                            1 Reply Last reply
                            0
                            • M Marc Clifton

                              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

                              Thyme In The Country

                              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

                              P Offline
                              P Offline
                              Paul Brower
                              wrote on last edited by
                              #32

                              First thing one learns in a Test Driven Development environment is: switch statements = bad bad bad.

                              M 1 Reply Last reply
                              0
                              • E Ed Poore

                                Hmm, must have been mistaken then, where did I see that code then... :suss:

                                D Offline
                                D Offline
                                Dan Neely
                                wrote on last edited by
                                #33

                                I don't have VS05, what's inside Number.ParseNumber?

                                -- Rules of thumb should not be taken for the whole hand.

                                D 1 Reply Last reply
                                0
                                • S Stuart Dootson

                                  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...

                                  P Offline
                                  P Offline
                                  Paul Watson
                                  wrote on last edited by
                                  #34

                                  Well obviously that shouldn't happen. People just need to use their heads :)

                                  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...

                                  1 Reply Last reply
                                  0
                                  • P Paul Watson

                                    '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...

                                    C Offline
                                    C Offline
                                    Chris Maunder
                                    wrote on last edited by
                                    #35

                                    Paul Watson wrote:

                                    premature optimisation

                                    You can get medication for that these days.

                                    cheers, Chris Maunder

                                    CodeProject.com : C++ MVP

                                    P 1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      Paul Watson wrote:

                                      premature optimisation

                                      You can get medication for that these days.

                                      cheers, Chris Maunder

                                      CodeProject.com : C++ MVP

                                      P Offline
                                      P Offline
                                      Paul Watson
                                      wrote on last edited by
                                      #36

                                      Sadly it can make you go blind.

                                      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...

                                      1 Reply Last reply
                                      0
                                      • D Dan Neely

                                        I don't have VS05, what's inside Number.ParseNumber?

                                        -- Rules of thumb should not be taken for the whole hand.

                                        D Offline
                                        D Offline
                                        DavidNohejl
                                        wrote on last edited by
                                        #37

                                        You don't need VS2005, but Reflector[^] and .NET framework assemblies, obviously. Well, in Number.ParseNumber is lot of code, but no exception throwing.


                                        "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

                                        D 1 Reply Last reply
                                        0
                                        • D DavidNohejl

                                          You don't need VS2005, but Reflector[^] and .NET framework assemblies, obviously. Well, in Number.ParseNumber is lot of code, but no exception throwing.


                                          "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

                                          D Offline
                                          D Offline
                                          Dan Neely
                                          wrote on last edited by
                                          #38

                                          True, but since I'm only doing 1.1 dev work, I don't have, or need, the 2.0 framework installed.

                                          -- Rules of thumb should not be taken for the whole hand.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups