Questions tagged [method-chaining]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
7 votes
4 answers
809 views

Is the fluent interface pattern suitable in casual scenarios?

In a recent PR, a developer, whom I will call Alice, came across a lot of resistance by a coworker (Bob) because she wrote a utility code unit in a fluent style rather than in a classical style. In ...
22 votes
11 answers
5k views

In a fluent interface with "with", is cloning expected?

In an object oriented language like Java or PHP (other perspectives welcome as well) if I use a fluent interface like this: my_pizza = (new Pizza).withTopping("pineapple"); another_pizza = ...
  • 577
4 votes
1 answer
80 views

Chain of responsibility for a single instance?

I have legacy code and it has a function called initialize and this function calls N amount of methods of same object and these methods are responsible for validating identity of personas. At each ...
  • 167
0 votes
1 answer
62 views

What's a proper way to call a chain of methods that modify an instance attribute?

I've got an Algorithm class whose responsibility is to find if a given word is in a list of words. As part of doing that, the algorithm first has to lowercase the words, remove punctuation, and ...
1 vote
4 answers
1k views

How to return fluently?

I'm becoming addicted to writing fluently (perhaps incorrectly originally said "declaratively") and I'm taking it to possibly unwise extremes, but the more I write this way the more one thing stands ...
0 votes
1 answer
3k views

Should I avoid long method chaining simply to get a variable? [duplicate]

I am working on a game, and I have many instances where I have to do something like this: Game.getInstance().getEnergyDropManager().getDrops() Or: this.dimensionSelector.selectDimension(Game....
  • 163
3 votes
2 answers
1k views

Demeter's law vs method chaining: when to use which?

Given this code from the Symfony framework: use Symfony\Component\HttpFoundation\Request; public function indexAction(Request $request) { $request->isXmlHttpRequest(); // is it an Ajax ...
  • 189
7 votes
1 answer
4k views

Is there a better term for "functional method chaining"?

I'm writing a C# style guide for my team and I'm trying to describe the benefits of side-effect-free functional-style methods. I want to include online references to back up the suggestions, but I can'...
  • 186
2 votes
1 answer
233 views

Any rules of thumb when to choose method chaining [duplicate]

That question bothers me for years, so, imagine some code that in 99% looks like a non-sequential (order does not matter) list of some specific objects method calls - and those calls are independent ...
  • 2,404
0 votes
1 answer
195 views

Method pattern and method coupling

Disclaimer: I am going to write in Python and in the context of web development with Django, but this question is not language nor framework specific. Let's say I have a PizzaManager class that ...
  • 2,176
2 votes
2 answers
2k views

Long chained method calls contained within an if statement shown on a sequence diagram

Just wondering if there is a good way to do this? Currently i'm performing the method calls as if they were happening prior to the conditional block, then comparing what would be the result in "[...
3 votes
0 answers
203 views

Is Nested Structure with Spread Responsibilities Not Logical?

Given the following code snippet in a Laravel Controller: $this->userRepository->saveByProject( $this->fileRepository->saveByProject( $this->metricRepository->...
2 votes
2 answers
1k views

Law of Demeter and its applicability

Let's say I'd like to perform the following command: house.getFloor(0).getWall(WEST).getDoor().getDoorknob(); To avoid a NullPointerException, I'd have to do the following if: if (house != ...
  • 1,713
8 votes
8 answers
1k views

Can "return this" pattern be optimized to no cost performance?

return this (or similar construct) allows method chaining. Lack of it is painful, because you have to write such code (C#): var list = new List<string>(); list.Add("hello"); list.Add("world"); ...
  • 1,494
3 votes
1 answer
196 views

Reusing an interface or method chaining

I am learning about coding to an interface in OOP programming. I understand you should code to an interface so you can 'swap out' functionality. However, I have a situation where I feel I can reuse ...
  • 235
2 votes
1 answer
86 views

Modern frameworks method conventions

I've been noticing that modern frameworks tend to have this kind of code style: expect(6 - 4).toBe(2) this can be rephrased as: assert(6-4, 2) Yet the former is much more readable. I would like to ...
  • 131
3 votes
2 answers
192 views

What is a proper way to chain instructions if previous one is ok [duplicate]

I'm currently working on a big file I have to parse and process and each step needs to be done in an order as I do SQL queries and need inserted IDs to make other insertions ... The problem is I often ...
3 votes
3 answers
2k views

Why python function programming functions are not collection methods? [duplicate]

In other words, is there a Python design related reason for it to be so? Functions like map, filter, reduce etc. are just plain functions. Is it just a poor design choice (if it is a good one, ...
  • 141
2 votes
1 answer
66 views

Returning the object/$this [duplicate]

A very general question. I was thinking about method chaining, which seems useful, and began wondering if there is a reason to not return the object in a class member, as a matter of course. For ...
2 votes
1 answer
325 views

C# OOP Class Usage [duplicate]

I am just being picky and really wanted some of my code to look, feel, and be used a specific way but am not sure if its possible. I have a class I created that can be used like this Query("...
  • 47
2 votes
2 answers
135 views

Method for all objects for checking conditions which also includes method chaining and avoiding variables

(I am currently using groovy but it should apply to most OO languages so I also put the langauge-agnostic tag) I try to program in a function style which also includes method chaining and avoiding ...
  • 2,419
26 votes
7 answers
18k views

Coding style issue: Should we have functions which take a parameter, modify it, and then RETURN that parameter?

I'm having a bit of a debate with my friend over whether these two practices are merely two sides of the same coin, or whether one is genuinely better. We have a function which takes a parameter, ...
  • 369
3 votes
2 answers
1k views

How to avoid long call chains?

I have a hierarchy of objects, mostly connected by composition. I.e. (not showing the class methods for readability): class A {} class B {A a;} class C {B b;} etc... class Z {Y y;} class Z provides ...
  • 7,005
3 votes
0 answers
552 views

Do thin fluent builder method chains have any need to be "extensible"?

In an environment where refactoring is difficult extensibility can be a life saver. However, this is no excuse to over engineer code. Given that a thin fluent builder method chain is backed by well ...
1 vote
3 answers
1k views

How to signal that a method chain should not continue?

When doing method chaining, you have methods that return the object allowing you to continue the chain. However, you need to get values out of the object somehow. Usually I've added a method that ...
15 votes
7 answers
4k views

Function only returns unchanged parameter, useless?

I just found this function in the project I'm working at: -- Just returns the text unchanged. -- Note: <text> may be nil, function must return nil in that case! function Widget:wtr(text) ...
  • 270
11 votes
4 answers
25k views

When should a method of a class return the same instance after modifying itself?

I have a class that has three methods A(), B() and C(). Those methods modify the own instance. While the methods have to return an instance when the instance is a separate copy (just as Clone()), I ...
1 vote
1 answer
178 views

Object Chain through an Interface

Say I have 3+ types of objects: function Sea() { var logs = [Logs]; this.getLog = function(ind){return logs[ind]} } function Log() { var bumps = [Bumps]; this.getBump = function(ind){...
3 votes
1 answer
270 views

For instance methods, would always returning self or this, instead of using a void return type, be a clean coding style? [closed]

I just came across this article, and in particular, this answer. Essentially they're talking about returning self from instance methods to allow for method chaining. That being said, one of the ...
  • 3,115
0 votes
0 answers
121 views

Check some value between each function call

Can you recommend a nice way of checking a particular value between calls to a set of functions? E.g. something like this in Python (might not be terribly 'Pythonic'): self.error_code = 0 # this ...
  • 169
5 votes
1 answer
10k views

nodejs chaining with async

I'm trying to chain a series of methods that are async. I have heard of promises and futures but what I'm looking for is: obj.setup() .do_something() .do_another_thing() .end() and not: ...
  • 151
7 votes
6 answers
6k views

Coding style for chained function calls

A common thing you need to do is to take a value, do something with it by passing it to a function, and then do some more with the return value, in a chain. Whenever I run into this type of scenario, ...
15 votes
1 answer
982 views

Are chained methods that require only one parameter per method equivalent to currying?

I've been toying around with Ruby lately and I found myself wondering if in pure object oriented languages (and even those that are not pure) making methods that take only one parameter and then get ...
user avatar
38 votes
8 answers
4k views

When using method chaining, do I reuse the object or create one?

When using method chaining like: var car = new Car().OfBrand(Brand.Ford).OfModel(12345).PaintedIn(Color.Silver).Create(); there may be two approaches: Reuse the same object, like this: public Car ...
29 votes
3 answers
12k views

What is the name for the idiom using method chaining to build an object?

I frequently use a pattern where I using method chaining to setup an object, similar to a Builder or Prototype pattern, but not creating new objects with each method call, instead modifying the ...
  • 2,182
14 votes
4 answers
3k views

Are there any actual drawbacks to self-referential method chaining?

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 ...
  • 13.9k
49 votes
5 answers
73k views

Purpose of `return self` from a class method?

I came across something like this in an open-source project. Methods that modify instance attributes return a reference to the instance. What is the purpose of this construct? class Foo(object): ...
  • 787