6 ms·
I did say scripty code. My typical scripting session from scratch starts with opening a simple editor and starting to type in code. The structure is loose, perh
by vorg 15y ago
I did say scripty code. My typical scripting session from scratch starts with opening a simple editor and starting to type in code. The structure is loose, perhaps it's testing something I've written in another file in a statically-typed language. As the code evolves, I slowly give it more structure by reworking it. At some stage I MIGHT put some code into methods.
But before that stage, nested scopes are useful because I'm far less likely to pass the wrongly-named variable into a function when I'm cutting and pasting code. An example coding snippet...
def out= new File("blah")
try{
def testdata= "abcdefg"
def result= SomethingToTest(testdata, out)
assert result == result.getSth()
}
try{
def testdata= "hijk"
try{
def result= SomethingToTest(testdata, out)
assert result == result.getSth()
}
try{
def result= SomethingToTest(testdata)
assert result == result.getSth()
}
}
If I use testdata1, testdata2, result1, result2, etc, I might forget to rename a variable after cutting and pasting, and think a test works when it doesn't.
- Torn 15y agoWhy not just use braces? Take all your 'try' keywords out and the scope limitations are the same. Unless you're relying on the try {} to avoid bombing when there are assertion errors? That just seems a bad way of doing unit testing.
- vorg 15y agoYou can do that in Java but not in Groovy unless you write tr:{ //... }
- scott_s 15y agoIt sounds like you want support for a bad practice. Each of those code blocks are logically distinct tasks, and it would serve readability to factor them into functions. Further, you're abusing a language construct to do something other than its purpose. I've never programming in Scala, so I'm going to write Python code that represents what I think is a better way of doing that kind of testing: def test1(data): result = SomethingToTest(data) assert result == result.getSth() def test2(data, out): result = SomethingToTest(data, out) assert result == result.getSth() out = open("blah") test2(out, "abcdefg") test2(out, "hijk") test1("hijk") The problem is not that copy-pasted code may be incorrect. The problem is that you're copying and pasting code and expecting it to be correct. The way you're nesting scopes is confusing.