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. Other Discussions
  3. The Weird and The Wonderful
  4. How to Train Your Programmer

How to Train Your Programmer

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorial
17 Posts 13 Posters 0 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.
  • B bosedk

    Not a shame IMO. Having a bunch of if-else or a switch-case setup is the simplest and most straight forward to do this for five to six cases. The conditionals could be moved inside the constructor to make the code more readable though.

    M Offline
    M Offline
    Manfred Rudolf Bihy
    wrote on last edited by
    #7

    bosedk wrote:

    The conditionals could be moved inside the constructor

    I hope not! This would only be advisable if the differentiation between the 5 strings were something inherent to that class, but then OP should have used an enumeration instead of strings. Regards, Manfred

    "With sufficient thrust, pigs fly just fine."

    Ross Callon, The Twelve Networking Truths, RFC1925

    B 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      And if I prefix that with

      Name = "Hello, this is a lot of rubbish!";
      IngGenInvPs post = new IngGenInvPs(taxBO, Name);

      Does your code give the same result as the original?

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #8

      It probably does in the context of the requirement. If you require fixing to a small set, firstly it should be an enum not a string input, and secondly if there is a good reason to allow free text input it's still better to have a simple check along the lines of

      if(!ValidNames.Contains(Name)) throw ...;

      ... and then just construct the object from the name.

      1 Reply Last reply
      0
      • M Manfred Rudolf Bihy

        bosedk wrote:

        The conditionals could be moved inside the constructor

        I hope not! This would only be advisable if the differentiation between the 5 strings were something inherent to that class, but then OP should have used an enumeration instead of strings. Regards, Manfred

        "With sufficient thrust, pigs fly just fine."

        Ross Callon, The Twelve Networking Truths, RFC1925

        B Offline
        B Offline
        bosedk
        wrote on last edited by
        #9

        Manfred R. Bihy wrote:

        I hope not!

        Aah, yes. I now see why not!

        Manfred R. Bihy wrote:

        but then OP should have used an enumeration instead of strings.

        the text might be coming from user, who knows, so converting text to enums and checking against the enums would be additional no value added steps for the simple scenario. But without proper context, the code seems alright.

        1 Reply Last reply
        0
        • R Rajesh_Francis

          I just found the following code if (Name == "Ticket") { IngGenInvPs post = new IngGenInvPs(taxBO, "Ticket"); } else if (Name == "Advertisement") { IngGenInvPs post = new IngGenInvPs(taxBO, "Advertisement"); } else if (Name == "Asset") { IngGenInvPs post = new IngGenInvPs(taxBO, "Asset"); } else if (Name == "Telecom") { IngGenInvPs post = new IngGenInvPs(taxBO, "Telecom"); } else { IngGenInvPs post = new IngGenInvPs(taxBO, "Inventory"); } :cool:

          0 Offline
          0 Offline
          0bx
          wrote on last edited by
          #10

          "Training" is usually something you can only do in your free time, during the times you should be sleeping. At work you have to deliver stuff. If you have a limited amount of time to deliver something that works and you're not yet quite experienced (and without any formal education on software development), then this sort of stuff happens. It's not pretty but it works... for now... :^)

          Giraffes are not real.

          1 Reply Last reply
          0
          • R Rajesh_Francis

            I just found the following code if (Name == "Ticket") { IngGenInvPs post = new IngGenInvPs(taxBO, "Ticket"); } else if (Name == "Advertisement") { IngGenInvPs post = new IngGenInvPs(taxBO, "Advertisement"); } else if (Name == "Asset") { IngGenInvPs post = new IngGenInvPs(taxBO, "Asset"); } else if (Name == "Telecom") { IngGenInvPs post = new IngGenInvPs(taxBO, "Telecom"); } else { IngGenInvPs post = new IngGenInvPs(taxBO, "Inventory"); } :cool:

            L Offline
            L Offline
            Lutoslaw
            wrote on last edited by
            #11

            So.. is the actual logic implemented in a costructor? post is out of scope for the rest of the world unless the code you have posted is incomplete.

            Greetings - Jacek

            1 Reply Last reply
            0
            • R Rajesh_Francis

              I just found the following code if (Name == "Ticket") { IngGenInvPs post = new IngGenInvPs(taxBO, "Ticket"); } else if (Name == "Advertisement") { IngGenInvPs post = new IngGenInvPs(taxBO, "Advertisement"); } else if (Name == "Asset") { IngGenInvPs post = new IngGenInvPs(taxBO, "Asset"); } else if (Name == "Telecom") { IngGenInvPs post = new IngGenInvPs(taxBO, "Telecom"); } else { IngGenInvPs post = new IngGenInvPs(taxBO, "Inventory"); } :cool:

              R Offline
              R Offline
              RobCroll
              wrote on last edited by
              #12

              Switch statements in Java didn't support strings as arguments until recently. Objective C still doesn't. So no training required :laugh:

              "You get that on the big jobs."

              N 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                It's not that bad! Well, yes, you could do it with a switch:

                IngGenInvPs post;
                switch (Name)
                {
                case "Ticket":
                case "Advertisement":
                case "Asset":
                case "Telecom":
                post = new IngGenInvPs(taxBO, Name);
                break;
                default:
                post = new IngGenInvPs(taxBO, "Inventory");
                break;
                }

                Or preferably, just remove it as all the created variables go out of scope at the end of the block - unless they are being sneaky in the constructor!

                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                R Offline
                R Offline
                Rajesh Anuhya
                wrote on last edited by
                #13

                :thumbsup: +5

                My Tip/Trick[^]

                1 Reply Last reply
                0
                • R RobCroll

                  Switch statements in Java didn't support strings as arguments until recently. Objective C still doesn't. So no training required :laugh:

                  "You get that on the big jobs."

                  N Offline
                  N Offline
                  Nagy Vilmos
                  wrote on last edited by
                  #14

                  In Java, I'd use an Enum[^].


                  Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                  1 Reply Last reply
                  0
                  • R Rajesh_Francis

                    I just found the following code if (Name == "Ticket") { IngGenInvPs post = new IngGenInvPs(taxBO, "Ticket"); } else if (Name == "Advertisement") { IngGenInvPs post = new IngGenInvPs(taxBO, "Advertisement"); } else if (Name == "Asset") { IngGenInvPs post = new IngGenInvPs(taxBO, "Asset"); } else if (Name == "Telecom") { IngGenInvPs post = new IngGenInvPs(taxBO, "Telecom"); } else { IngGenInvPs post = new IngGenInvPs(taxBO, "Inventory"); } :cool:

                    B Offline
                    B Offline
                    Bernhard Hiller
                    wrote on last edited by
                    #15

                    Below that if-else if-else block, there must be some more lines like:

                    ResultObject res = post.DoSomething();

                    And someone complains then: "Why does it not compile!!!"

                    1 Reply Last reply
                    0
                    • R Rajesh_Francis

                      I just found the following code if (Name == "Ticket") { IngGenInvPs post = new IngGenInvPs(taxBO, "Ticket"); } else if (Name == "Advertisement") { IngGenInvPs post = new IngGenInvPs(taxBO, "Advertisement"); } else if (Name == "Asset") { IngGenInvPs post = new IngGenInvPs(taxBO, "Asset"); } else if (Name == "Telecom") { IngGenInvPs post = new IngGenInvPs(taxBO, "Telecom"); } else { IngGenInvPs post = new IngGenInvPs(taxBO, "Inventory"); } :cool:

                      J Offline
                      J Offline
                      Jeroen De Dauw
                      wrote on last edited by
                      #16

                      He forgot to wrap each line in a try catch block! o_O

                      Jeroen De Dauw (blog | Twitter | Identi.ca)

                      1 Reply Last reply
                      0
                      • R Rajesh_Francis

                        I just found the following code if (Name == "Ticket") { IngGenInvPs post = new IngGenInvPs(taxBO, "Ticket"); } else if (Name == "Advertisement") { IngGenInvPs post = new IngGenInvPs(taxBO, "Advertisement"); } else if (Name == "Asset") { IngGenInvPs post = new IngGenInvPs(taxBO, "Asset"); } else if (Name == "Telecom") { IngGenInvPs post = new IngGenInvPs(taxBO, "Telecom"); } else { IngGenInvPs post = new IngGenInvPs(taxBO, "Inventory"); } :cool:

                        J Offline
                        J Offline
                        johannesnestler
                        wrote on last edited by
                        #17

                        I have done things like this in the past, when I wanted to give me or other developers a hint for a possible later change. E.g. The "Name" to "2nd Parameter" correspondence could be just by chance or a quick first implementation. What I mean is: maybe "2nd Parameter" has to be localized or mapped later. If you would shorten the code with a direct assignment, I'd assume as a maintenance developer that "Name" always holds a valid "2nd Parameter to IngGenInvPs constructor. And this is maybe not true. This code is not good for other reasons (use switch, use enum, others mentioned it), but not necessarily for the obvious redundance.

                        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