method-chaining

Method chaining - why is it a good practice, or not?

限于喜欢 提交于 2019-12-27 10:35:30
问题 Method chaining is the practice of object methods returning the object itself in order for the result to be called for another method. Like this: participant.addSchedule(events[1]).addSchedule(events[2]).setStatus('attending').save() This seems to be considered a good practice, since it produces readable code, or a "fluent interface". However, to me it instead seems to break the object calling notation implied by the object orientation itself - the resulting code does not represent performing

Method chaining - why is it a good practice, or not?

Deadly 提交于 2019-12-27 10:30:14
问题 Method chaining is the practice of object methods returning the object itself in order for the result to be called for another method. Like this: participant.addSchedule(events[1]).addSchedule(events[2]).setStatus('attending').save() This seems to be considered a good practice, since it produces readable code, or a "fluent interface". However, to me it instead seems to break the object calling notation implied by the object orientation itself - the resulting code does not represent performing

Ruby curly braces vs. do-end method chaining

回眸只為那壹抹淺笑 提交于 2019-12-25 02:41:20
问题 I have found a question: Nested Loops Ruby and solved it, but the solution looks ugly (although works): puts (1..10).map { |i| (i..10).step(i).inject(0) { |memo, obj| memo = memo | 2**(obj-1) } }.inject(0) { |memo, obj| memo = memo ^ obj} Rewritten to multiline, but keeping curly braces: puts (1..10).map { |i| (i..10).step(i).inject(0) { |memo, obj| memo = memo | 2**(obj-1) } }.inject { |memo, obj| memo = memo ^ obj } I tried to rewrite it into multiline do-end blocks to make it more readable

How to avoid logic duplication when promisifying js methods?

丶灬走出姿态 提交于 2019-12-23 22:24:41
问题 Recently I embraced promises chaining pattern. It's very convenient to do like this: action1 .then(()=> action2()) .then(()=> action3()); But, in order to do it I changed all my methods like this (TypeScript): action1() : Promise<any>{ try{ // actual code return Promise.resolve(); } catch (err){ console.error(err); return Promise.reject(err); } } This looks like very repetitive pattern. What's the best way to avoid code duplication? 回答1: Write a function to wrap promise over a function and

Method Chaining in a Javascript Class

纵然是瞬间 提交于 2019-12-23 13:26:14
问题 I am attempting to implement method chaining inside my subclass "setBall" class Ball { constructor(name, size, power) { this.name = name; this.size = size; this.power = power; } } let Ball1 = new Ball('Bomb',5,2); console.log(Ball1); class setBall extends Ball{ constructor(name, size, power) { super(name, size, power); } setBall.setName(name) { this.name = name; } setBall.setSize(size) { this.size = size; } setBall.setPower(power) { this.power = power; } get getthrowSpeed() { return this.size

Using HtmlTextWriter to Render Server Controls?

青春壹個敷衍的年華 提交于 2019-12-23 10:22:36
问题 I'm writing the RenderContents() method of my ASP.NET server control. The method uses an HtmlTextWriter object to render the output content. For the control I'm writing, using the HtmlTextWriter 's methods seems like it will require a lot of lines of code to open and close every tag and add every attribute to the stream. In the end I feel like I'm going to end up with code that is a lot longer than it needs to be. I was thinking that if I used a chainable class such as StringBuilder , my code

How to attach an event to onSubmit event of form with chaining earlier attached methods as well?

旧街凉风 提交于 2019-12-22 08:10:09
问题 Actaully my application is having hundreds of pages. Now i have to attach an event 'disablePage' on onSubmit of form. I don't want to go to each and every page and write: <form name="frmname" onSubmit="disablePage();"> What i am doing right now is:- excerpt from common.js file; [ included in all pages ] /* we have to attach 'attachFormSubmit' method on onLoad event, otherwise forms[0] will always be null. If there is any alternative, then please suggest one */ if (window.addEventListener){

How to chain call functions by using a string containing that chain in PHP

若如初见. 提交于 2019-12-22 05:37:56
问题 I have a chain call like so: $object->getUser()->getName(); I know that I can use a string to call a function on an object: $functionName = 'getUser'; $object->$functionName() or call_user_func(array($object, functionName)) I was wondering if it was possible to do the same for a chain call? I tried to do: $functionName = 'getUser()->getName'; $object->functionName(); But I get an error Method name must be a string I guess this is because the () and -> cannot be interpreted since they are part

Create a new jquery chained method

跟風遠走 提交于 2019-12-22 00:55:02
问题 How do you write new chained methods in jQuery? I have a very procedural style in my jQuery: $("#SaveButton").click(function () { Foo($("#SubTotal")); Foo($("#TaxTotal")); Foo($("#Total")); Bar($("#SubTotal")); Bar($("#TaxTotal")); Bar($("#Total")); }); How do I create a .foo() method in jQuery so that I can then write: $("#SaveButton").click(function () { $("#SubTotal,#TaxTotal,#Total").foo().bar(); }); And in a related point - is there an easy way (in Visual Studio, or Notepad++ or

Chain functions in different way

夙愿已清 提交于 2019-12-21 04:49:09
问题 Scala functions has following methods for chaining: fn1.andThen(fn2) fn1.compose(fn2) But how can be written this case: I have function cleanUp() which has to be called always as last step. And I have a bunch of other functions, like that: class Helper { private[this] val umsHelper = new UmsHelper() private[this] val user = umsHelper.createUser() def cleanUp = ... // delete user/ and other entities def prepareModel(model: TestModel) = { // create model on behalf of the user } def commitModel(