SexScripts : Feature request: method/"function" support - https://ss.deviatenow.com:443/viewtopic.php?f=4&t=381 Page 1 of 1

Feature request: method/"function" support

FreedomOfRestriction [ Fri Feb 28, 2014 6:11 pm ]

Groovy supports defining "functions" (technically methods). This is a good feature. But SexScripts is designed in such a way that method definitions are impossible. This is a very bad design. Based on the error, it sounds to me like the Groovy scripts are being executed in a place where methods can't be defined, or something (I'm not very familiar with Java).

Instead of functional programming, scripts usually tend to use an infinite loop made of a switch statement, which is indexed by a variable called "block" or something like that. This is a very crappy system, very difficult to program in a way that isn't redundant, highly inflexible, and easily resulting in spaghetti code. I can't describe how much I hate this programming style.

Just look at this example using functional programming:

Code:
def foo() {
    instruction1()
    instruction2()
    instruction3()
    instruction4()
    instruction5()
    instruction6()
    instruction7()
    instruction8()
}
def bar(x, y, z) {
    instruction9()
    instruction10(x)
    instruction11()
    instruction12()
    instruction13()
    instruction14(y)
    instruction15(z)
    instruction16()
    instruction17()
}

if (myCondition == "value1") {
    foo()
    instruction18()
    instruction19()
}
else if (myCondition == "value2") {
    instruction20()
    bar(1, 2, 3)
}
else if (myCondition == "value3") {
    foo()
    bar(4, 5, 6)
    instruction21()
    foo()
}
else {
    instruction22()
}


And compare it to this awful mess:

Code:
def running = true
def block
def next_block
def x
def y
def z

if (myCondition == "value1") {
    block = "block1"
}
else if (myCondition == "value2") {
    block = "block2"
}
else if (myCondition == "value3") {
    block = "block3"
}
else {
    block = "block4"
}

while (running) {
    switch block {
        case "foo":
            instruction1()
            instruction2()
            instruction3()
            instruction4()
            instruction5()
            instruction6()
            instruction7()
            instruction8()
            block = next_block
            break

        case "bar":
            instruction9()
            instruction10(x)
            instruction11()
            instruction12()
            instruction13()
            instruction14(y)
            instruction15(z)
            instruction16()
            instruction17()
            block = next_block
            break

        case "block1":
            block = "foo"
            next_block = "block1_2"
            break

        case "block1_2":
            instruction18()
            instruction19()
            block = "end"
            break

        case "block2":
            instruction20()
            x = 1
            y = 2
            z = 3
            block = "bar"
            next_block = "end"
            break

        case "block3":
            block = "foo"
            next_block = "block3_2"
            break

        case "block3_2":
            x = 4
            y = 5
            z = 6
            block = "bar"
            next_block = "block3_3"
            break

        case "block3_3":
            instruction21()
            block = "foo"
            next_block = "end"
            break

        case "block4":
            instruction22()
            block = "end"
            break

        case "end":
        default:
            running = false
            break
    }
}


This example is actually pretty optimistic; in real applications, the number of special variables for these faux "functions" would increase dramatically, to the point where the reduction of redundancy doesn't seem worth it. So you're more likely to end up with results like this, using a larger number of simpler macros:

Code:
def running = true
def block
def next_block

if (myCondition == "value1") {
    block = "block1"
}
else if (myCondition == "value2") {
    block = "block2"
}
else if (myCondition == "value3") {
    block = "block3"
}
else {
    block = "block4"
}

while (running) {
    switch block {
        case "foo":
            instruction1()
            instruction2()
            instruction3()
            instruction4()
            instruction5()
            instruction6()
            instruction7()
            instruction8()
            block = next_block
            break

        case "bar1":
            instruction11()
            instruction12()
            instruction13()
            block = next_block
            break

        case "bar2":
            instruction16()
            instruction17()
            block = next_block
            break

        case "block1":
            block = "foo"
            next_block = "block1_2"
            break

        case "block1_2":
            instruction18()
            instruction19()
            block = "end"
            break

        case "block2":
            instruction20()
            instruction9()
            instruction10(1)
            block = "bar1"
            next_block = "block2_2"
            break

        case "block2_2":
            instruction14(2)
            instruction15(3)
            block = "bar2"
            next_block = "end"
            break

        case "block3":
            block = "foo"
            next_block = "block3_2"
            break

        case "block3_2":
            instruction9()
            instruction10(4)
            block = "bar1"
            next_block = "block3_3"
            break

        case "block3_3":
            instruction14(5)
            instruction15(6)
            block = "bar2"
            next_block = "block3_4"
            break

        case "block3_4":
            instruction21()
            block = "foo"
            next_block = "end"
            break

        case "block4":
            instruction22()
            block = "end"
            break

        case "end":
        default:
            running = false
            break
    }
}


I'm not speaking from speculation here, just to be clear. I have been frustrated the past couple of days trying to code just one complex script with some sanity. This is not fun programming. If I didn't have a strong personal desire for what I'm scripting, I would have given up as soon as I found out that I can't use functions.

Please, please, please fix this problem so I and probably other programmers can have our sanity back. (I would fix it myself, but I'm not very experienced in Java and wouldn't know where to look.)

Re: Feature request: method/"function" support

doti [ Fri Feb 28, 2014 9:53 pm ]

I'm very sorry you didn't found solutions here, most probably you missed this topic : how to write functions?. Also, the full manual have a chapter about it.

Re: Feature request: method/"function" support

FreedomOfRestriction [ Sat Mar 01, 2014 3:54 am ]

Ah, that's good to know. Thanks! That's an extremely weird way to do it, I gotta say, but it's a lot better than the methods I've been using.

Page 1 of 1 All times are UTC + 1 hour [ DST ]
https://ss.deviatenow.com:443/
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Maroon Fusion theme created by Oxydo
Software, theme modifications, phpBB modification by Doti 2010, 2011