Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [method-chaining]

The tag has no usage guidance.

1
vote
4answers
410 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
1answer
918 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....
3
votes
2answers
575 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 ...
3
votes
1answer
2k 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'...
2
votes
1answer
152 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 ...
0
votes
1answer
130 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
votes
2answers
1k 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 "[...
2
votes
0answers
202 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
2answers
653 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 != ...
6
votes
8answers
802 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"); ...
3
votes
1answer
183 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 ...
2
votes
1answer
75 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 ...
3
votes
2answers
169 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 ...
2
votes
3answers
1k 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, ...
2
votes
1answer
65 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
1answer
302 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("...
2
votes
2answers
116 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 ...
17
votes
7answers
10k 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, ...
3
votes
2answers
802 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 ...
2
votes
0answers
508 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
3answers
482 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
7answers
3k 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) ...
8
votes
4answers
11k 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
1answer
172 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
1answer
249 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 ...
0
votes
0answers
113 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 ...
5
votes
1answer
9k 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: ...
5
votes
6answers
4k 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
1answer
724 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 ...
37
votes
8answers
3k 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 ...
21
votes
4answers
9k 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 ...
14
votes
4answers
1k 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 ...
44
votes
5answers
45k 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): ...