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

    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
            • 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
              #36

              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
              • 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
                #37

                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 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
                  • P Paul Brower

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

                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #39

                    Paul Brower wrote:

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

                    Why? How do switch statements affect TDD? 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 1 Reply Last reply
                    0
                    • M Marc Clifton

                      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

                      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
                      Phillip Martin
                      wrote on last edited by
                      #40

                      Marc Clifton wrote:

                      Throwing an exception is expensive

                      I'm not familiar enough with the CLR to know, but just how expensive is an exception? My background is C++ (where exceptions are potentially very expensive) and Java (where exceptions are very cheap) I'm going to go out on alimb and guess the CLR is somewhere in the middle? - Phil

                      1 Reply Last reply
                      0
                      • T tgrt

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

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

                        Possibly, can't check since both my dev machines have been updated with it.

                        1 Reply Last reply
                        0
                        • M Marc Clifton

                          Paul Brower wrote:

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

                          Why? How do switch statements affect TDD? 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
                          #42

                          Switch statements are one of the most obvious 'code smells.' Typically switch statements are indicative of duplication. Most times when you see a switch statements, you should consider refactoring with polymorphism. Below are some common refactoring methods to deal with switch 'smells.' 1: If you're switching on some kind of code, you'll want a method or class that hosts the type code. (Extract case to new method then move it to the appropriate -- new if necessary -- class where the polymorphism is needed. At that point you can replace type code with State or Strategy pattern. Once that is done, replace conditional with polymorphism, possibly making original method abstract. 2: If you only have a few cases in a single method, and you don't expect change, then polymorphism is definately too much. In this case, you can use the 'Replace Parameter with Explicit Methods'. Create an explicit method for each value of the parameter, for each leg of the conditional, call the appropriate new method, replace each caller of the conditional method with a call to the appropriate new method, remove the conditional method. (Excerpts taken from REFACTORING - IMPROVING THE DESIGN OF EXISTING CODE -- Martin Fowler).

                          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

                            S Offline
                            S Offline
                            Scott Dorman
                            wrote on last edited by
                            #43

                            Not to continue beating this horse, but how would this really be different than using a sequence of if/else if statements to do the test? You would still run into potential issues with case-sensitivity.

                            switch (myString)
                            {
                            case "Hello":
                            ...
                            case "Goodbye":
                            ...
                            }

                            if (myString == "Hello")
                            {
                            }
                            else if (myString == "Goodbye")
                            {
                            }
                            else
                            {
                            }

                            All of these statements evaluate to call bool [mscorlib]System.String::op_Equality(string,string) in IL. To get around the case-sensitivity and culture issues, you can use myString.ToLowerInvariant(), which will still result in the same call to op_Equality. I know that switches are optimized for numeric comparison, but I think it is sometimes necessary to be able to do comparisons of this sort and I would much rather deal with reading a switch statement than a bunch of if/else if statements.

                            ----------------------------- In just two days, tomorrow will be yesterday.

                            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