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

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

It's not clear from the question if the requirement is based on lines or statements. If what you care about is lines of code, you can put all three statements on one line. This doesn't do much for...

posted 2y ago by deleted user

Answer
#1: Initial revision by (deleted user) · 2022-02-23T15:54:26Z (over 2 years ago)
It's not clear from the question if the requirement is based on *lines* or *statements*.

If what you care about is lines of code, you can put all three statements on one line. This doesn't do much for readability, but then playing Code Golf rarely does.

```
void test() {
    Foo foo = new Foo(); foo.setX(42); assertEquals(13, bar.fun(foo).getY(), "Test failed because blabla");
}
```

If the requirement is for a single _statement_, then I don't see any way to achieve this without either writing a wrapper or changing the code under test. `assertEquals()` requires two values to compare, `setX()` does not return a value so either you need to change it so that it does, or put it behind some other function that will (directly or indirectly) return the value you are going to assert.

My own suggestion would be to give up on the goal of "avoiding the need for scrolling". Packing source code into fewer lines will not increase the quality or readability of your code, and even if you succeed in making every test a single line, a requirement to avoid scrolling will still limit the number of tests you can have, which potentially _decreases_ code quality by reducing test coverage. But of course it's your choice how to style your code.