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

  • Home
  • SEARCH
  • 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 6537219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:35:27+00:00 2026-05-25T10:35:27+00:00

This is difficult for me to put into words, but say I have some

  • 0

This is difficult for me to put into words, but say I have some function:

a = a + b;
printf("HI THERE");
b = a + c;

(pretend this is 30 lines instead of 3 lines). Now say I want to do the exact same thing again later on, except instead of printing “HI THERE” I print “HO THERE”. What I currently do is copy paste all the lines of this function just to change HI THERE to HO THERE. This isn’t very elegant for large code blocks.

I know one solution might be like:

adder_function(1);
adder_function(2);

void adder_fuction(int input) {
   a = a + b;
   printer_function(input);
   b = a + c;
}

void printer_function(int input) {
   if (input == 1) printf("HI THERE");
   if (input == 2) printf("HO THERE");
}

But this seems to be inelegant as well for more complex blocks of code. Any ideas for better solutions?

EDIT: Just to show what it is I’m doing, here’s the code in question (you can see that almost nothing changes between the blocks except .input and .output and the printf statements):

found=line.find("INPUTS");
if (found == 0) {
    inputfound = true;
    found = line.find_first_of(":");

    if (found == string::npos) { 
        printf("BAD NETLIST INPUT DECLARATION\n\r"); 
        exit(1);
    }

    found = line.find_first_not_of("\n\t ",found+1);

    if (found == string::npos) {
        printf("BAD NETLIST INPUT DECLARATION\n\r");
        exit(1);
    }
    else {
        temp_node_name += line[found];
        for (i = found+1; i < line.size(); i++) {
            if ( isalnum(line[i]) || isspace(line[i]) ) {
                if ( isalnum(line[i]) )
                    temp_node_name += line[i];
                if ( isspace(line[i]) || i == line.size() - 1 ) {
                    if (!temp_node_name.empty()) {
                        if (determine_uniqueness(temp_node_name)) {
                            nodes.push_back(dummy_node);
                            nodes.at(id_counter).name_in_netlist = temp_node_name;
                            nodes.at(id_counter).input = true;
                            temp_node_name.erase();
                            id_counter++;
                        }
                    }
                }
            }
            else {
                printf("BAD NETLIST INPUT DECLARATION\n\r");
                exit(1);
            }
        }
    }
    printf("NETLIST INPUT DECLARATION OK\n\r");
    continue;
}

SEPARATE CODE BLOCK THAT IS COPY PASTED

found=line.find("OUTPUTS");
if (found == 0){
    outputfound = true;
    found = line.find_first_of(":");

    if (found == string::npos) {
        printf("BAD NETLIST OUTPUT DECLARATION\n\r"); 
        exit(1);
    }

    found = line.find_first_not_of("\n\t ",found+1);
    if (found == string::npos) {
        printf("BAD NETLIST OUTPUT DECLARATION\n\r");
        exit(1);
    }
    else {
        temp_node_name += line[found];
        for (i = found+1; i < line.size(); i++) {
            if ( isalnum(line[i]) || isspace(line[i]) ) {
                if ( isalnum(line[i]) )
                    temp_node_name += line[i];
                if ( isspace(line[i]) || i == line.size() - 1 ) {
                    if (!temp_node_name.empty()) {
                        if (determine_uniqueness(temp_node_name)) {
                            nodes.push_back(dummy_node);
                            nodes.at(id_counter).name_in_netlist = temp_node_name;
                            **nodes.at(id_counter).output = true;**
                            temp_node_name.erase();
                            id_counter++;
                        }
                    }
                }
            }
            else {
                printf("BAD NETLIST OUTPUT DECLARATION\n\r");
                exit(1);
            }
        }
    }
    printf("NETLIST OUTPUT DECLARATION OK\n\r");
    continue;
}
  • 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-25T10:35:27+00:00Added an answer on May 25, 2026 at 10:35 am

    I’m late to the party so I’ll only be addressing your updated code.

    First off, I see you printing error messages to stdout (via printf) and then calling exit. Why? You should print error (and perhaps debugging) messages to stderr (with fprintf(stderr, ...) or with perror, though perror is better if a library routine fails and sets errno). Also, since this is C++ (not plain C), you might want to use iostreams instead of *printf. It’s more C++-ish (though, as predominantly a C programmer, I prefer *printf myself).

    Since the only difference between your two blocks of code is the string containing INPUT in the first and OUTPUT in the second, I recommend stuffing all of this into a function that, among other necessary parameters (whatever line is is probably one of them), takes a parameter called bool is_input. The first time you call this, is_input should be true, and the second time, it should be false.

    Then, in both of your blocks of code, you can change the printf lines to:

    fprintf(stderr, "BAD NETLIST %s DECLARATION\n", is_input ? "INPUT" : "OUTPUT");
    

    or

    fprintf(stderr, "NETLIST %s DECLARATION OK\n", is_input ? "INPUT" : "OUTPUT");
    

    And then for the member modification write:

    (is_input ? nodes[id_counter].input : nodes[id_counter]) = true
    

    (Note that there’s no need to use .at twice in a row with the same index – if it threw the exception once, it won’t reach the second call, and if it didn’t throw the first time it won’t throw the second. It probably won’t be a huge speedup, but it’s the thought that counts?)

    Last, if you need the external variables inputfound and outputfound to be set, add a bool &found parameter to your function, and set it to be true inside the function. The first time you call it, pass inputfound, and the second, outputfound.

    Now your two code blocks are identical, and can be put together into one function, and called twice (once with "INPUT", and once with "OUTPUT"). Easy.

    In the future, whenever you find yourself writing a block of code that’s strikingly similar to another block of code, stop rewriting (or copy-and-pasting). Copy the block of code into a new function all by itself, and replace the original block with a call to the new function. Now you can reuse that block of code as many times as you want.

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

Sidebar

Related Questions

I've tried searching for this but it's pretty difficult to put into words. Basically,
I'm finding it difficult to put the exact question into words, so I'll just
This is a difficult and open-ended question I know, but I thought I'd throw
I usually do not have difficulty to read JavaScript code but for this one
I have been trying to tackle this problem , but I am having difficulty
I have run into a difficult situation. I do not want to do my
I've posted a couple other questions during this process, but I have a better
I realize there's already been several questions like this, but I think my case
I have asked this question in Radiant CMS google group, but no one answered
This might be a bit difficult to explain in writing, so please bear with

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.