Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 266129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:49:46+00:00 2026-05-11T22:49:46+00:00

I consider fluent interfaces very convenient for many tasks. But I feel uneasy when

  • 0

I consider fluent interfaces very convenient for many tasks. But I feel uneasy when I end up mixing fluent methods and modifying methods in one class.

Just an example (it’s a little contrived, please bear with me):

Assuming a string utility class, trimming seems good for chaining:

Str & Str::Trim() { return TrimLeft().TrimRight(); }

Other methods would naturally return a new object:

Str Str::GetFirstToken() const
{ 
   // result = first token;
   return result;
}

And there’sa 3rd type, that – by itself – would logically mutate the object and return a new one:

Str Str::SplitFirstToken() 
{ 
   result = GetFirstToken();
   // this = remainder
   return result;
}

When I use the signature that is most obvious for each method individually, I end up with these three types, and I am afraid it’s not very intuitive for consuming the class which is which, especially since the return type is mroe or less the same.


I have already decided against making Str immutable – since methods like SplitToken provide core functionality. My main issue is mixing fluent methods What would you do?

  • do not use fluent methods in that interface

  • move them to a sub-interface (see below)

  • “If one is fluent, all modifying methods should be fluent”?

  • use a seocific prefix for fluent methods?

  • don’t worry?

  • ???

sub interface idea:

void CStr::Trim() { TrimLeft(); TrimRight(); }
CStrFluent & Str::Fluent() { return CStrFluent(*this); }
....
str.Fluent().TrimLeft().TrimRight();

I am undecided on this, I don’t really like the extra “fluent” – especially that it’s a method call in C++

What do you think?

[edit] I am using “fluent” here in the basic meaning of chaining method calls on a single instance, not in the advanced sense of creating english sentences in code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:49:46+00:00Added an answer on May 11, 2026 at 10:49 pm

    I’ve not done much work with fluent interfaces (although I’ve played around with DSLs in general), but it seems to me that while the class may lend itself to this approach, it’s not particularly necessary in this case. Maybe I’m missing something, but it seems to me that you are not likely to be doing a bunch of actions on a single string with no reference to anything else, which is what you’ve ended up with here. In addition, you’re transitioning to new objects in the middle of the chain, which seems again to violate the purpose of a fluent interface, particularly when you consider what happens in this chain:

    Str String = OtherString.GetFirstToken().SplitFirstToken().Trim();
    

    I think perhaps that this type of relatively low-level utility class is the wrong place to try out a fluent interface. Fluency seems to me much more important when your objects are subject to persistent, focused attention than when they are transient and ancillary to the core logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I consider myself quite fluent in PHP and am rather familiar with nearly all
Consider this: One mySQL database that has tables and rows and data within it.
Consider the following function, which does not work in Python, but I will use
Consider an article inside a div that has many <p> tag. Is there any
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
Consider the following setup: A windows PC with a LAN interface and a WiFi
Consider the need to develop a lightweight desktop DB application on the Microsoft platforms.
Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider these two function definitions: void foo() { } void foo(void) { } Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.