5 ms·
The code is not garbage, it's just your highfalutin python opinion makes it so you only ever use list comprehensions or return generators. For loops in python
by byby 3y ago
The code is not garbage, it's just your highfalutin python opinion makes it so you only ever use list comprehensions or return generators.
For loops in python that return non lazy evaluated lists are fine. Python was never suppose to be an efficient language anyways, grading python based off of this criteria is pointless.
It doesn't matter how snobbish you are on language syntax though. I fed it code and regardless of whether you think it's garbage it did what I asked it to do and nothing else.
Would you prefer the AI say, "this code is garbage, here's not only how to make it unit testable but how to improve your garbage code." Actually we can make the output more unpredictable as LLMs do have a non deterministic seed that can increase the creativity of the answer.
- Marazan 3y agoIt has wrapped range() with useless code. It has added no functionality, it has not improved testability in any way. . Please, take the code it has produced and integrate it into the original function. All it does is replace the range call. That's it. It has absolutely and totally failed at the given task whilst outputting plausible garbage about why it has succeeded.
- moomoo3000 3y agoIt removed the print and returns a list instead.
- Marazan 3y agoWhich is useless because _it has changed the semantics of the function_
- moomoo3000 3y agoIt was a bad function
- byby 3y agoYou have to change the semantics of the function to make it unit testable. Literally tell me how else can you test that function with a unit test? By definition a unit test can only test functions that return data. So there's no other option here.
- Marazan 3y agoLet me tell you your mind is going to be blown once you learn about Monads. A mutable object is functionally identical to a return value if you control the initial state and lifetime of the object. Like you can do in a unit test. And as I demonstrated in my other comment I 100% retained the semantic structure of the function whilst making it 100% unit testable.
- byby 3y agoI think you don't understand what unit testability means. It means removing IO and side effects from your code. How the hell do I test a print function? I take the print function and match it with what? It has no output so how can I test it printed the correct thing? I can't. I can test a list. I just match it with another list. Making your code unit testable is about segregating IO from logic. Write pure logic where all functions have inputs and outputs and those things can be tested. Your io prints should be small because all functions that do io cannot be fully tested. IO is pollution. Any output to IO is the program exiting the logical mathematical universe of the program and that output can be verified only by an external entity. Either your eyes for stdout or another process or files or a bunch of other ways. Unit tests are about internal local tests that touch local functionality and logic. If you want something unit testable it needs a local output and an input and it shouldn't rely on io in it's data path. I think your complaint here is an example of chatGPT superiority. It understood something you didn't. Well now you know. Removing the print function from the logic and returning the data is 100 percent the correct move. Do you understand?
- wizofaus 3y agoThat would be fine if the core thing needing unit testing was the data generation/ transformation logic, but just as often as not it's the output formatting too. Did you try asking ChatGPT to write a unit test to confirm that the output is displayed as expected?
- byby 3y ago>That would be fine if the core thing needing unit testing was the data generation/ transformation logic, but just as often as not it's the output formatting too. Output formatting touches io. In this case it is no longer a unit test that touches these things. Unit tests by definition test ONLY internal logic and transformations. It is literally the definition of unit tests. When you test things like stdout that becomes an integration test and Not a unit test. It requires some external thing or some global black magic monkey patch that changes what print does to do integration testing. (Btw making print formatting unit testable means segregating the formatting from the print. Produce the string first, test that, then print, because print can never be unit tested by definition) Typically programmers segregate these levels of testing because unit tests are easier to write. But to write unit tests your code has to be written in a way to cater to it. Often this style of coding actually improves your code it makes it much more modular. The reason is because pure functions that output data can be composed with all kinds of io functions. You can move it all over the place and to different platforms with different forms of IO. Print has no meaning in certain embedded systems so it can't be moved... By segregating the logic out it makes it so I can move the logic without the io baggage. Chatgpt 100 percent gets the difference that's why it did what it did. I think you and the OP don't fully understand the meaning of unit testing. Don't take this the wrong way, but just because you don't know this doesn't say anything about your skills as a programmer. But just recognize that this concept is basic and is pretty much something universal among testing.