Renato Dinhani
Renato Dinhani

Reputation: 36706

Using methods without returning results is a good thing?

Is a good thing use methods that receive an object and set the attributes of that object inside the method instead of returning the result?

There are situations where returning the results will need two similar methods with a few differences.

Technically, on way will don't need "code duplication" and will do the things faster, but I think the code isn't clear when there aren't returning results.

Example of an HTML analyser:

void parseLinks(Page page){
    //processing
    page.setLinks(links);
    page.setEmails(emails);
    //page.set...;
}

or

List<Link> getLinks(SomeParameter parameter){
    //same processing
    return links;
}

List<Email> getEmails(SomeParameter parameter){
    //same processing
    return emails;
}

page.setLinks(getLinks(parameter));
page.setEmails(getEmails(parameter));
//page.set...

Upvotes: 1

Views: 109

Answers (4)

shem
shem

Reputation: 4712

Your example looks like a function programming, and not OOP. parseLinks should be a method of Page Class on should work on "this" instance, furthermore- if you doing it any way on any page- I would recommend doing it in the constructor. after doing it- the getLinks and getEmails looks like regular getters methods for this object.

This is how I'm seeing it in this example, but it's really depend on what you're want to do.

OK, now I understand it more after you add some context to the question: I would implement it something like this- in the parsing method I would return a new Page instance (using clone or add the right parameters to the method building a new one), and set its field on by one. something like this:

Page parse(String content){
    Page page = new Page()
    //processing
    page.setLinks(getLinks(content));
    page.setEmails(getEmails(content));
    //page.set...;
}

Upvotes: 0

omnomnom
omnomnom

Reputation: 9149

The good practice is to clearly separate methods modifying domain objects somehow and methods which do not modify them in any way. It's easier to work within complex system when you know that some methods you use do not produce side effects (generally speaking, in computer science, all methods changing state of some object are non side-effect-free).

Place as much of the logic of the program as possible into functions, operations that return results with no observable side effects. Strictly segregate commands (methods which result in modifications to observable state) into very simple operations that do not return domain information. Further control side effects by moving complex logic into VALUE OBJECTS when a concept fitting the responsibility presents itself.

(Domain-Driven Design: Tackling Complexity in the Heart of Software, Eric Evans)

Upvotes: 3

Balaswamy Vaddeman
Balaswamy Vaddeman

Reputation: 8530

do you strongly feel caller of the method require some information once callee has done some work?

if yes then you should return something.

*unit testing will be easy if callee returns something*

Upvotes: 1

Argiropoulos Stavros
Argiropoulos Stavros

Reputation: 9533

The object data are encapsulated.It doesn't matter where in your code you interact with an object.

Cheers

Upvotes: 0

Related Questions