extract method for better readability

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cm9kY2hhcg==?=

    #1

    extract method for better readability

    hey all,
    i have about a 40 or more lines in my switch statement and was just
    wondering if it was worth extracting out the individual case statement which
    would involve passing a Table object, 2 ints and a string?

    What's the rule of thumb?

    thanks,
    rodchar
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: extract method for better readability

    You are going to have to provide more information than that. In what
    way do you want to extract out that case statement?


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "rodchar" <rodchar@discus sions.microsoft .comwrote in message
    news:24DBB9E5-D56D-4A73-9581-D20618814694@mi crosoft.com...
    hey all,
    i have about a 40 or more lines in my switch statement and was just
    wondering if it was worth extracting out the individual case statement
    which
    would involve passing a Table object, 2 ints and a string?
    >
    What's the rule of thumb?
    >
    thanks,
    rodchar

    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: extract method for better readability


      "rodchar" <rodchar@discus sions.microsoft .comwrote in message
      news:24DBB9E5-D56D-4A73-9581-D20618814694@mi crosoft.com...
      hey all,
      i have about a 40 or more lines in my switch statement and was just
      wondering if it was worth extracting out the individual case statement
      which
      would involve passing a Table object, 2 ints and a string?
      >

      I think I would in that case.
      What's the rule of thumb?
      If naming the block is more helpful than seeing the code, then make it a
      helper function.
      >
      thanks,
      rodchar

      Comment

      • Larry Smith

        #4
        Re: extract method for better readability

        hey all,
        i have about a 40 or more lines in my switch statement and was just
        wondering if it was worth extracting out the individual case statement
        which
        would involve passing a Table object, 2 ints and a string?
        >
        What's the rule of thumb?
        Your question isn't very clear. Presumably you mean invoking a function for
        each case statement or some particularly long case statement (or some
        variation of this). If so then you should normally do that anyway IMHO
        unless (perhaps) there's one (trivial) line per case. Note BTW that a case
        statement (or equivalent "if" statements) often indicates a design problem
        in OOP. I have no idea if that applies in your own case but testing
        multiples values in a switch statement is often better replaced with objects
        that have virtual functions (all derivatives of some common base class). It
        depends on the situation of course since there's always a judgment call to
        make and not everyone will always agree. It's typically better however to
        invoke a virtual function on some object in a collection then to test a type
        in a switch statement. You should always consider this whenever writing a
        switch statement. For instance, switching on employee type such as
        "manager", "architect" , "developer" , etc., and then calling a
        "SalaryRang e()" function for each employe type is poor design. It's better
        to provide an abstract "Employee" class and an abstract "SalaryRang e()"
        function. You then create one derivative for each employee type and override
        this function accordingly.


        Comment

        • Rene

          #5
          Re: extract method for better readability

          What's the rule of thumb?

          I would say that the rule of thumb is: "If it makes your code easier to
          understand and more maintainable do it".


          Comment

          • =?Utf-8?B?cm9kY2hhcg==?=

            #6
            RE: extract method for better readability

            sorry for the brevity of the OP but the feedback was helpful nonetheless.
            thanks,
            rod.

            "rodchar" wrote:
            hey all,
            i have about a 40 or more lines in my switch statement and was just
            wondering if it was worth extracting out the individual case statement which
            would involve passing a Table object, 2 ints and a string?
            >
            What's the rule of thumb?
            >
            thanks,
            rodchar

            Comment

            Working...