IO arguments versus side effects arguments

Is it better to write methods that take no arguments and have no side effects, change the state of an object, or methods with one argument, taking an argument and processing it? If the second alternative is preferred, is it better to return the input argument explicitly or just handle it, since the caller must have a reference to it.

More precisely: I process XML and read the first chapters of Clean Code book. I try to break Big into many smaller processing methods, so this method can be read as a story in lines:

cleanHeader();

extractMetaInfo();

appendStuff();

etc. where these methods work on an XML document stored as a member.

IMHO, best practices for reducing the number of parameters versus no side effects seem to contradict each other. It would be better to write the following:

doc = cleanHeader(doc);

doc = extractMetaInfo(doc);

doc = appendStuff(doc);

Is there a specific “right” to this issue? How much more specific question depends on a specific answer? Or is there a third alternative that I have not thought about?

EDIT: Found a related question with conflicting answers. Learn to develop?

+3
source share
2 answers

If your algotimen can be used by several threads in parallel, then the method recommended in a clean codebook (statfull, but without arguments) will not work. In this case, you should use the parameter !!

+1
source

OO. , , doc, , , , bigProcessingMethod(), , .

DocUtil {   private String doc;

public DocUtil(String doc)
{
     this.doc = doc;
}

public bigProcessingMethod()
{
     cleanHeader(doc);

 extractMetaInfo(doc);

 appendStuff(doc);

}

// ... }

. , -, DocUtil.

doc , . , , - , , doc.

.

0

Source: https://habr.com/ru/post/1785377/


All Articles