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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:47:32+00:00 2026-06-08T12:47:32+00:00

Possible Duplicate: How many lines of code should a function/procedure/method have? Out team has

  • 0

Possible Duplicate:
How many lines of code should a function/procedure/method have?

Out team has a project of not well structured ansi-c code. I would like to use some CC techniques to tidy up the code base.

As for C code, we have a lot of pointers and a lot of NULL-pointer pitfalls to catch. Therefore there are lot of fragments which look alike. It’s like

if (pointer == NULL)
{
  function1();
  function2();
}

all over the place. Then there are an awful lot of functions that will be called after each other in the same fashion only with a few variations, like

function1();
function2a();
function3();

and

function1();
function2b();
function3();

all over the place.

I would like to extract those blocks as a single function to reduce LOC and copy-pasting. But that will create not only a (somewhat) orthogonal layer but also a handfull of function doing more or less the same, except for some details. And even worse, it will create functions that do a lot of things at once.

So, what’s a good strategy? What’s more important, lean code on high level, lean functions on low level or lean architecture? Which principle trumps the other? Seperation of concern or DRY?

I would like to refactor that beast but don’t know where to start.

To exploid the example below and put same names in. Lets assume we have

morningBath();
drinkCoffee();
if (checkMail())
{
  answerMail();
}

and put that into morningRoutine(). Now we have

drinkTea();
morningBath();
if (checkMail())
{
  answerMail();
}

and call it sundayMorningRoutine(). But then there is duplicated code. Or expand morningRoutine(day) as

if (day == sunday){
  drinkTea();
  morningBath();
} else {
  morningBath();
  drinkCoffee();
}    
if (checkMail())
{
  answerMail();
}

or maybe

if (day == sunday){
  drink(Tea);
  morningBath();
} else {
  morningBath();
  drink(Coffee);
}    
if (checkMail())
{
  answerMail();
}

I wonder if that is good style.. maybe.. Thanks for that hint!

  • 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-06-08T12:47:34+00:00Added an answer on June 8, 2026 at 12:47 pm

    Regarding C code, it’s perfectly normal to frequently encounter NULL pointer checks, especially when it comes to function arguments. Personally, I prefer to let the caller resolve the situation, as in:

    if (p == NULL) {
        /* maybe do some cleanup and then: */
        return errcode;
    }
    

    Public functions, i.e. functions that are part of the API, should always check for NULL pointers. Functions that are designated static may IMO drop those checks. And finally, there’s always assert(). Those checks can be suppressed by the compiler flag -NDEBUG. I use assert() in static functions instead of if-statements and in “public” functions for tests that reveal that the caller didn’t actually understand the API as a whole, e.g. in a linked list lib:

    void list_print(list **l)
    {
        assert(l != NULL);    /* no valid list passed by reference can ever be NULL */
    
        if (*l == NULL)       /* but it can be empty */
            return;
    
        /* print list */
    }
    

    As for your second concern, I can see three options:

    1) leave everything as it is – after all, it’s working.

    2) introduce new functions:

    int function_1_2a_3();
    int function_1_2b_3();
    

    3) introduce new parametrized functions:

    int function_1_2_3(int type);
    

    Personally, I prefer the latter approach, but that is really just a matter of style.

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

Sidebar

Related Questions

Possible Duplicate: How many lines of code should a function/procedure/method have? I would like
Possible Duplicate: Define a method that has many (or infinite) arguments I have the
Possible Duplicate: How many Python classes should I put in one file? Coming from
Possible Duplicate: Properties vs Methods For many situations it is obvious whether something should
Possible Duplicate: Non repeating random number array I have a String array with many
Possible duplicate of: should-i-link-to-google-apis-cloud-for-js-libraries also many other discussions, including: Where do you include the
Possible Duplicate: Interfaces: Why can't I seem to grasp them? I have seen many
Possible Duplicate: NSNumber retain count issue Hello, I have the following code: NSNumber *number
Possible Duplicate: Why is such a function definition not allowed in haskell? I'm a
Possible Duplicate: Have you used any of the C++ interpreters (not compilers)? Hi, I

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.