Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A How to create an object, call one of it's methods and pass it as an argument as a oneliner?

It seems to me that you are hobbling yourself by making design constraints more absolute than they need to be. For instance: You don't want to change the code under test, not even a tiny little ...

posted 2y ago by meriton‭

Answer
#1: Initial revision by user avatar meriton‭ · 2022-02-23T17:31:37Z (over 2 years ago)
It seems to me that you are hobbling yourself by making design constraints more absolute than they need to be. For instance:

* You don't want to change the code under test, not even a tiny little bit.
* You don't want to introduce a helper function, because helper functions clutter the code (is that always the case? No way to mitigate that?)
* You want your test cases to fit on one screen, not scroll even a single line.

Now, all those goals are sensible, but they are not boolean: The *more* you have to change the code under test, the *more* helper functions you have, the *more* you have to scroll the worse it gets.

It can make sense to comprise a goal a tiny little bit if doing so markedly improves another. That is, good design involves making sensible trade offs.

With that said, let's investigate a few trade offs:

If you add
```
public Foo(int x) {
    this.x = x;
}
```

to Foo, all your tests can read:

    assertEquals(87, bar.fun(new Foo(42)), "Test failed because blabla");

which fits on a single line and it quite readable.

Or if you added to your test class:

```
private fun(int x) {
    var foo = new Foo();
    foo.setX(x);
    return bar.fun(foo);
}
```

your tests would read

    assertEquals(87, fun(42), "Test failed because blabla");

Notice how a descriptive name instead of `wrapper` can improve the readability of your code. In addition, you can reduce clutter by moving the helper function out of sight, for instance by declaring it at the end of the file, or in a class of its own.

Overall, the main objective should be to reduce code duplication in your tests and improve their readability by using descriptive names. If you have many tests performing the same cumbersome steps, factoring that out into another method makes perfect sense.