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. General Programming
  3. C#
  4. Code refactoring..

Code refactoring..

Scheduled Pinned Locked Moved C#
helpquestioncode-review
4 Posts 4 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.
  • S Offline
    S Offline
    Simon_Whale
    wrote on last edited by
    #1

    hello in the middle of trying to refactor out a nightmare that I have inherited and there is no documentation. The source looked something similar to this

    var outcome = MethodCall(param1, param2, param3) ? AnotherMethod(MethodCall(param1, param2, param3), Enum.Value) :
    MethodCall(param2, param3, param4) ? AnotherMethod(MethodCall(param2, param3, param4), Enum.Value) :
    MethodCall(param3, param4, param5) ? AnotherMethod(MethodCall(param3, param4, param5), Enum.Value)

    I have refactored this down to a series of If else but now I am at the following stage which I want to refactor because of the following. Apart from refactoring this into a series of nested if statements so that I dont have to call the same method twice in an if statement I am stuck.

    if(methodcall(param1, param2, param3))
    {
    outcome = AnotherMethod(MethodCall(param1, param2, param3), someintValue);
    }
    else if (methocall(param2, param3, param4))
    {
    outcome = AnotherMethod(MethodCall(param2, param3, param4), someintValue);
    }
    else if (Methodcall(param3, param4, param5))
    {
    outcome = AnotherMethod(MethodCall(param3, param4, param5), someintValue);
    }

    Any advice would be greatfully recieved. Thanks Simon

    Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

    D L L 3 Replies Last reply
    0
    • S Simon_Whale

      hello in the middle of trying to refactor out a nightmare that I have inherited and there is no documentation. The source looked something similar to this

      var outcome = MethodCall(param1, param2, param3) ? AnotherMethod(MethodCall(param1, param2, param3), Enum.Value) :
      MethodCall(param2, param3, param4) ? AnotherMethod(MethodCall(param2, param3, param4), Enum.Value) :
      MethodCall(param3, param4, param5) ? AnotherMethod(MethodCall(param3, param4, param5), Enum.Value)

      I have refactored this down to a series of If else but now I am at the following stage which I want to refactor because of the following. Apart from refactoring this into a series of nested if statements so that I dont have to call the same method twice in an if statement I am stuck.

      if(methodcall(param1, param2, param3))
      {
      outcome = AnotherMethod(MethodCall(param1, param2, param3), someintValue);
      }
      else if (methocall(param2, param3, param4))
      {
      outcome = AnotherMethod(MethodCall(param2, param3, param4), someintValue);
      }
      else if (Methodcall(param3, param4, param5))
      {
      outcome = AnotherMethod(MethodCall(param3, param4, param5), someintValue);
      }

      Any advice would be greatfully recieved. Thanks Simon

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      How about this:

      if(MethodCall(p1,p2,p3)||(MethodCall(p2,p3,p4) || MethodCall(p3,p4,p5)){
      outcome = AnotherMethodCall(true, someIntValue);
      }

      From your code, I understand that MethodCall is of bool return type and if any one of three calls is true, we call AnotherMethodCall with true and a number. So, here is how it can be broken down. If I could, I would refactor method one to take in more arguments so only one call will tell me if it returns true for various input sets.

      "It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[^]

      1 Reply Last reply
      0
      • S Simon_Whale

        hello in the middle of trying to refactor out a nightmare that I have inherited and there is no documentation. The source looked something similar to this

        var outcome = MethodCall(param1, param2, param3) ? AnotherMethod(MethodCall(param1, param2, param3), Enum.Value) :
        MethodCall(param2, param3, param4) ? AnotherMethod(MethodCall(param2, param3, param4), Enum.Value) :
        MethodCall(param3, param4, param5) ? AnotherMethod(MethodCall(param3, param4, param5), Enum.Value)

        I have refactored this down to a series of If else but now I am at the following stage which I want to refactor because of the following. Apart from refactoring this into a series of nested if statements so that I dont have to call the same method twice in an if statement I am stuck.

        if(methodcall(param1, param2, param3))
        {
        outcome = AnotherMethod(MethodCall(param1, param2, param3), someintValue);
        }
        else if (methocall(param2, param3, param4))
        {
        outcome = AnotherMethod(MethodCall(param2, param3, param4), someintValue);
        }
        else if (Methodcall(param3, param4, param5))
        {
        outcome = AnotherMethod(MethodCall(param3, param4, param5), someintValue);
        }

        Any advice would be greatfully recieved. Thanks Simon

        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I make no apologies:

        if(methodcall(param1, param2, param3)) {
        outcome = AnotherMethod(MethodCall(param1, param2, param3), someintValue);
        return;
        }

        if (methocall(param2, param3, param4)) {
        outcome = AnotherMethod(MethodCall(param2, param3, param4), someintValue);
        return;
        }

        if (Methodcall(param3, param4, param5)) {
        outcome = AnotherMethod(MethodCall(param3, param4, param5), someintValue);
        return;
        }

        throw new exception( "Logic error" );

        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

        1 Reply Last reply
        0
        • S Simon_Whale

          hello in the middle of trying to refactor out a nightmare that I have inherited and there is no documentation. The source looked something similar to this

          var outcome = MethodCall(param1, param2, param3) ? AnotherMethod(MethodCall(param1, param2, param3), Enum.Value) :
          MethodCall(param2, param3, param4) ? AnotherMethod(MethodCall(param2, param3, param4), Enum.Value) :
          MethodCall(param3, param4, param5) ? AnotherMethod(MethodCall(param3, param4, param5), Enum.Value)

          I have refactored this down to a series of If else but now I am at the following stage which I want to refactor because of the following. Apart from refactoring this into a series of nested if statements so that I dont have to call the same method twice in an if statement I am stuck.

          if(methodcall(param1, param2, param3))
          {
          outcome = AnotherMethod(MethodCall(param1, param2, param3), someintValue);
          }
          else if (methocall(param2, param3, param4))
          {
          outcome = AnotherMethod(MethodCall(param2, param3, param4), someintValue);
          }
          else if (Methodcall(param3, param4, param5))
          {
          outcome = AnotherMethod(MethodCall(param3, param4, param5), someintValue);
          }

          Any advice would be greatfully recieved. Thanks Simon

          Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, as others already noted, every second call to MethodCall(param1, param2, param3), I mean the ones after the question marks, is likely to return true, as you just have tested for true (that is what the question mark does). I say likely, it might be ugly code that creates or uses nasty side effects. If so, what follows needs some adjusting. it seems all your params have the same type, and all your someIntValues have different values, so I would suggest you: 1. stuff all the params in an array "paramsArray" 2. stuff all the someIntValues in an array "someIntValuesArray" 3. modify your MethodCall() to accept paramsArray and return an integer, being the first index in the paramsArray for which the original MethodCall() did return true; this basically means surrounding existing code with a for loop, adjusting the params (now array elements); also return a special value if no match found, say -1. 4. then simply do

          int index=MethodCall(paramsArray);
          if (index>=0) outcome=AnotherMethod(true, someIntValuesArray[index]);

          :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          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