We ran out of function calls, but we have plenty of if statements.



  • All modern programming practices nowdays will state that having many nested if statements usually means that some of the logic needs to be refactored out of a function and put in it's own independent function. While not always true, if designing well the programmer will notice that at some point there is simply too much logic going on in one function to a point where it becomes too hard to follow. Especially if each if block is some convoluted logic that is using global variables left and right.

     

    So now apparently when writing the application I am working on maintaining the developer figured that writing multiple entry points was bad... you know:

    ep1: edit component 1

    ep2: new component 1

    ep3: view component 1

    ep4: edit component 2

    ep5: new component 2

    ep6: view component 2

    etc...

    Well all I have is

    ep1: new/edit component 1,2,3, and 4

    ep2: view component 2,3

    ep3: view component 1,4

    Note: 1,2,3 and 4 are really all separate things that share only a bit of code, which should have been removed into it's own shared function (even if that much was necessary)

     

    And of course there is a ton of if blocks and request paramaters which indicate how to navigate though these convoluted entry points. And naturally fixing one bug breaks something in a completely independent part of the application. Oh and to make things worse there is a ton of code duplication and dead code so figuring out what is actualy trying to be done and making sure another chunk of the application does not break is not a trivial task. Test cases are also something to be desired... as in they don't exist.

     

    Which brings me to now: The code is deciding to use a non-existing global object, and I have no f-ing clue why that object should exist to begin with (there is no business reason for that object to be used anyways, but knowing this system there is no reason for half of the work to be done anyways)

     



  •  Maybe my brain has went dead, but what do you mean by multiple function entry points? goto? call-with-current-continuation?



  • It's too late. The convoluted logic has infected your brain. Or maybe just too much anonymization was applied to the code example.



  • @HypocriteWorld said:

     Maybe my brain has went dead, but what do you mean by multiple function entry points? goto? call-with-current-continuation?

    if(foo) doSomething(); else if(bar) doSomethingElse(); else if(baz) doAnotherThing(); etc



  • As in things like web applications having struts actions.

    You know form submits, or links, or w/e...

     

    But it does not matter where the entry points to execute the code are, what is important is how unsegregated the functionality is.

    Example: (in psuedo code)

    function query(params){

    if(params.save?){

    do save action

    }

    execute database query

    do view action

    render html

    }

     

     Note:

    1) execute database query = sql in the code

    2) do view action = html in the code

    3) the function name would be something like doQuery, findRecord and yes it will save, edit, query, and more fun stuff

    The funny thing is the code I was thinking of when I wrote the post was written by a jr dev... then i saw some senior. code...



  •  I have a bit of a WTF project at the moment and a function that does about 10 things (well, two "things" in about 5 different ways), with heaps of if statements to figure out what needs to happen. This was because the requirements kept changing through, and instead of spending time rewriting all that code, just add in more variables and if statements!



  •  I'm thinking it was something like this:

    Rather than

    DogfoodbowlNew()

    DogfoodbowlEdit()

    DogfoodbowlView()

    RedflowerNew()

    ...

    they had instead

    New(TypeOfThing)

    EditOrViewColoredThings(TypeOfThing, EditOrView)

    EditOrViewContainerLikeThings(TypeOfThing, EditOrView)



  • @TwelveBaud said:

    ...

     

    in a perfect workd it would be as simple as you stated...

     

    public void query(Map params){

    if(params.get("isEdit") == "true"){

    check session object1

    check session object2

     ... some work including getting some properties from params...

    modify session object1

    modify session object2

    delete session object3

    }

    check session object2

    ... some work including getting some properties from params...

    set session object1 to session object 2

    no return statement since all output is in some convoluted representation using session objects.

    }

     



  • @lolwtf said:

    @HypocriteWorld said:

     Maybe my brain has went dead, but what do you mean by multiple function entry points? goto? call-with-current-continuation?

    if(foo) doSomething(); else if(bar) doSomethingElse(); else if(baz) doAnotherThing(); etc
     

    Isn't that the whole point of functions? This is normal in a dispatch function which calls other functions based on something.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.