Skip to main content

When all the chainable functions of the object in stake are - in essence - just setters for individual parameters (like in your example), then the semantic differencedifferences between the two aproaches isapproaches are almost negliblenegligible. If that's the case, using optional parameters is more simplersimple, more idiomatic, and therefore preferrable.

However, function chaining (or method chaining) makes more sense forin the context of builder objects where the methods have more complex semantics. For example, think about a ControlBuilder with a fluent interfacefluent interface, where you can add label text subsequently by calling a method like AppendLine more than once. Or, think about methods where the specific order of calls makes a difference. There are plenty of examples for this in the Wikipedia article about Fluent interfacefluent interfaces. That is something you cannot achieve with optional parameters.

When all the chainable functions of the object in stake are - in essence - just setters for individual parameters (like in your example), then the semantic difference between the two aproaches is almost neglible. If that's the case, using optional parameters is more simpler, more idiomatic, therefore preferrable.

However, function chaining (or method chaining) makes more sense for builder objects where the methods have more complex semantics. For example, think about a ControlBuilder with a fluent interface, where you can add label text subsequently by calling a method like AppendLine more than once. Or think about methods where the specific order of calls makes a difference. There are plenty of examples for this in the Wikipedia article about Fluent interface. That is something you cannot achieve with optional parameters.

When all the chainable functions of the object are - in essence - just setters for individual parameters (like in your example), then the semantic differences between the two approaches are almost negligible. If that's the case, using optional parameters is more simple, more idiomatic and therefore preferrable.

However, function chaining (or method chaining) makes more sense in the context of builder objects where the methods have more complex semantics. For example, think about a ControlBuilder with a fluent interface, where you can add label text subsequently by calling a method like AppendLine more than once. Or, think about methods where the specific order of calls makes a difference. There are plenty of examples for this in the Wikipedia article about fluent interfaces. That is something you cannot achieve with optional parameters.

added 1 character in body
Source Link
cbojar
  • 4.3k
  • 1
  • 20
  • 19

When all the chainable functions of the object in stake are - in essence - just setters for individual parameters (like in your example), then the semantic difference between the two aproaches is almost neglible. If that's the case, using optional parameters is more simpler, more idiomatic, therefore preferrable.

However, function chaining (or method chaining) makes more sense for builder objects where the methods have a complexermore complex semantics. For example, think about a ControlBuilder with a fluent interface, where you can add label text subsequently by calling a method like AppendLine more than once. Or think about methods where the specific order of calls makes a difference. There are plenty of examples for this in the Wikipedia article about Fluent interface. That is something you cannot achieve with optional parameters.

When all the chainable functions of the object in stake are - in essence - just setters for individual parameters (like in your example), then the semantic difference between the two aproaches is almost neglible. If that's the case, using optional parameters is more simpler, more idiomatic, therefore preferrable.

However, function chaining (or method chaining) makes more sense for builder objects where the methods have a complexer semantics. For example, think about a ControlBuilder with a fluent interface, where you can add label text subsequently by calling a method like AppendLine more than once. Or think about methods where the specific order of calls makes a difference. There are plenty of examples for this in the Wikipedia article about Fluent interface. That is something you cannot achieve with optional parameters.

When all the chainable functions of the object in stake are - in essence - just setters for individual parameters (like in your example), then the semantic difference between the two aproaches is almost neglible. If that's the case, using optional parameters is more simpler, more idiomatic, therefore preferrable.

However, function chaining (or method chaining) makes more sense for builder objects where the methods have more complex semantics. For example, think about a ControlBuilder with a fluent interface, where you can add label text subsequently by calling a method like AppendLine more than once. Or think about methods where the specific order of calls makes a difference. There are plenty of examples for this in the Wikipedia article about Fluent interface. That is something you cannot achieve with optional parameters.

added 253 characters in body
Source Link
Doc Brown
  • 217.6k
  • 34
  • 402
  • 612

When all the chainable functions of the builder object in stake are - in essence - just setters for individual parameters (like in your example), then the semantic difference between the two aproaches is almost neglible. If that's the case, and using optional parameters would beis more simpler, more idiomatic, thustherefore preferrable.

However, function chaining (or method chaining) makes more sense whenfor builder objects where the methods have a complexer semantics where calling the same method twice. For example, think about a (with different parameters) makesControlBuilder with a differencefluent interface, orwhere you can add label text subsequently by calling the methods in a method like AppendLine more than once. Or think about methods where the specific order of calls makes a difference. There are plenty of examples for this in the Wikipedia article about Fluent interface. That is something you cannot achieve with optional parameters.

When all the chainable functions of the builder object are just setters for individual parameters, then the difference between the two aproaches is almost neglible, and using optional parameters would be more simpler, more idiomatic, thus preferrable.

However, function chaining (or method chaining) makes more sense when the methods have a complexer semantics where calling the same method twice (with different parameters) makes a difference, or calling the methods in a specific order. There are plenty of examples for this in the Wikipedia article about Fluent interface.

When all the chainable functions of the object in stake are - in essence - just setters for individual parameters (like in your example), then the semantic difference between the two aproaches is almost neglible. If that's the case, using optional parameters is more simpler, more idiomatic, therefore preferrable.

However, function chaining (or method chaining) makes more sense for builder objects where the methods have a complexer semantics. For example, think about a ControlBuilder with a fluent interface, where you can add label text subsequently by calling a method like AppendLine more than once. Or think about methods where the specific order of calls makes a difference. There are plenty of examples for this in the Wikipedia article about Fluent interface. That is something you cannot achieve with optional parameters.

Source Link
Doc Brown
  • 217.6k
  • 34
  • 402
  • 612
Loading