Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I recently suggested a method of chaining be implemented for a certain class in a certain project so readability of the code could be improved. I got a "fluent interfaces should not be implemented just for convenience, but for semantics" answer and had my suggestion shot down. I answered that I was not suggesting a fluent interface but method chaining itself (both can be confused with each other, read at bottom) to improve readability and coding comfort, suggestion was shot down again.

Anyhow, this got me thinking that maybe I could be incurring in a bad practice by always returning "this" in methods that are not supposed to return anything (e.g. setters).

My question is: can applying the previous convention be regarded as bad practice or abuse?, why?. I don't think there are any performance drawbacks, or are there?.

share|improve this question
1  
Look at this question for a nice discussion programmers.stackexchange.com/questions/48419/… –  KeesDijk May 30 '11 at 8:40
    
@KeesDijk Thanks, I edited my question a little so its not to similar to that one, as I'm more interested in the case of chaining methods from the same class –  dukeofgaming May 30 '11 at 9:01
1  
at SO, they told me, that chaining is not for C++ - stackoverflow.com/questions/5760551/… - what do you think about? –  kagali-san May 30 '11 at 16:05
1  
@mhambra It looks like you would only need to be careful when implementing method chaining and define clear standards for that. –  dukeofgaming May 30 '11 at 21:49

1 Answer 1

up vote 5 down vote accepted

No

As Kent Beck points out, code is read far more often than it is written.

If method chaining makes your code more readable, then use method chaining.

share|improve this answer
1  
Great book.. +1 –  Rein Henrichs May 30 '11 at 18:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.