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 7595265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:37:53+00:00 2026-05-30T21:37:53+00:00

Basically I found myself today writing lots of code like below: #define VAL_1 0

  • 0

Basically I found myself today writing lots of code like below:

#define VAL_1 0
#define VAL_2 1

class A {

public:
  void memberA();
  void memberB();

  ...

  void write(uin32_t address);

}

void
A::
memberA() {

  this->write(VAL_1);

}

void
A::
memberB() {

  this->write(VAL_2);

}

...

So basically, I have “pretty” names memberA, memberB for some task that really only does call the same function write with a different argument. The values VAL_0 and VAL_1 are not necessarily known to code using my class. Neither is the implementation detail behind memberA or memberB, although writemight be going public at some point.

Basically, now I’m repeating the same line of code this->write(...) over and over again. I’m looking for a solution that bypasses this step and calls the respective write immediately. A kind of passing on of the function arguments somewhat like a C++ constructor from a base class, possibly with matching arguments:

#define VAL_1 0
#define VAL_2 1

class A {

public:
  bool memberA() : write(VAL_1);
  bool memberB() : write(VAL_2);

  ...

  bool write(uin32_t address);

}

I’m wondering whether there might be something in Boost.Bind or some clever template-coding that lets me achieve this kind or thing?

Thanks,

FRob

  • 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-30T21:37:54+00:00Added an answer on May 30, 2026 at 9:37 pm

    Without fully understanding what you are after (I got confused about the last part involving constructors), I think you might be trying a little too hard to make the syntax easier on your clients.

    … how about simply:

    class A {
    public:
        enum Value {val1, val2, val3, val4, etc};
        bool write(Value val);
    };
    

    It’s good that you are trying to establish an easier syntax, but you also have to avoid the danger of monolithism. Monolithic classes are something that I strongly believe is one of the most common mistakes in object-oriented design. Sutter goes into this in detail in C++ Coding Standards and on gotw: http://www.gotw.ca/gotw/084.htm.

    If you have a class with 100 member functions, you probably have about 80 too many.

    ^ Think about this statement for a while. When you have so many functions, your classes tend to become increasingly hard to manage. It also invites other developers to just keep adding more and more to your class so that its design is never finalized. The result is the never-ending class that just grows and grows with each development cycle with no end in sight. That can easily become a source of bugs, inefficiencies, constant public interface revisions, unit test breakages, and it can go against the general reuse and flexibility of your class. When you have a separate function per value you can pass to another function, you’re dangerously treading into that territory.

    Trying too hard to avoid syntactical redundancy is usually a mistake. Unfortunately C++ just requires more lengthy syntax in some cases (getting much better with C++11). What you should try to optimize away is logical redundancy. Here calling write method with various values involves no logical redundancy, and the syntactical overhead is barely more than calling different functions for each value you can possibly pass to write.

    If these functions do nothing more than simplify the syntax of passing various values, you have to come to the realization that you are also bloating the class’s public interface with a whole lot more functions that, if you are in a production environment, you will likely have to document and teach individually. Strive for doing more with less and I think you’ll be much better off.

    Try to keep this aspect of monolithism in mind as a priority. You might even go so far as to hoist the named constants out of the class definition as non-members, like so:

    class A {
        bool write(uint32_t address);
    };
    
    // elsewhere
    static const uint32_t address_val1 = ...;
    static const uint32_t address_val2 = ...;
    static const uint32_t address_val3 = ...;
    

    There’s actually nothing wrong with this design and, in fact, it has more desirable engineering characteristics than the one where you have more class members as it’s completely decoupled from your class, making the maintenance of that class easier, its interface simpler to teach and document, and more likely to meet a state of reasonable completion.

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

Sidebar

Related Questions

I have found myself writing the same view over and over. It is basically
Basically my problem is that i've adapted a piece of code found here http://social.msdn.microsoft.com/Forums/en-US/vemapcontroldev/thread/62e70670-f306-4bb7-8684-549979af91c1
I've found myself using the following idiom lately in clojure code. (def *some-global-var* (ref
I found the following C++ code (comments added myself): // frame_name is a char
I'm not sure the title adequately describes the problem I've found myself with... Basically,
I have something that looks like the following document structure: public class Document {
I'm basically trying to teach myself how to code and I want to follow
Perhaps this is a duplicate question, but I havn't found something by myself. Basically
I recently found myself using the same pattern over and over in my code.
I have found many people with simliar issues but no soultions...basically I have two

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.