Pages: 1 [2]   Go Down
Author Topic: How to avoid code duplication in a function?  (Read 1216 times)
0 Members and 1 Guest are viewing this topic.
North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2048
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I also can't use an inline function, because although the code in those is supposed to just replace the function call, the compiler can decide not to do that which means trying to call variables local to the function holding my switch statement is a no-no.

You are also using GCC in the Arduino environment, so you can use GCC function attribute 'always inline':

I would prefer not to use compiler specific things.  I'm trying to get into good coding practices in C, not GCC specifically.  

Well use a C compiler, you are in C++ land my friend. Even without using attributes, I have managed to get every function that I need in lined to be so, maybe a reorganisation of how you declare your functions is in order, wildbill points out a major player. The only place you will have trouble is with template functions ( newer GCC does not have problem ).

Static on a global variable makes it visible to the compilation unit it is defined in, other units cannot see it, if they can see the declaration ( if in header ) then they get their own copy.
Logged


United Kingdom
Offline Offline
Tesla Member
***
Karma: 223
Posts: 6592
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Problem: You want certain data to be available to more than one function, but you don't want to make that data global.

Solution 1: Declare a class, making both the data and the functions members of that class.

Solution 2: Wrap the data and functions in a namespace.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 472
Posts: 18693
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This check is always the same.  So I end up with duplicate code.

Back to the original question, why not pass arguments?

I can't think why you need to duplicate code like you describe. Either pass arguments, or use a function object if you need to retain state. Functions can recurse (even outside Lua) so I don't see what the big issue is there.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 472
Posts: 18693
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Classes, like dc42 suggests, are another way. Slightly different but related.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3632
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know why none of the usual suspects have asked this yet: could you post the offending code?
Logged

Manchester, New Hampshire
Offline Offline
Edison Member
*
Karma: 4
Posts: 1352
Propmaker
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The offending code is really long, and it wasn't really necessary to explain the problem.  Anyway I've gotten lots of good suggestions, thanks guys. smiley
Logged

UK
Offline Offline
Shannon Member
****
Karma: 219
Posts: 12364
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,

I've got a state machine in my code (a big switch statement), and in that state machine in several states I need to check to see if a switch has been flipped, and perform an action.. namely light an LED, play sound, and change a mod variable.  This check is always the same.  So I end up with duplicate code.

In other programming languages I've used, I'd just declare a function within a function, and that function would have access to all the parent function's variables, but I can't do that in C.

I'm not seeing the problem yet. The final paragraph is the only place that you've hinted the behaviour of the 'duplicate code' needs to be different in the different places it occurs. If it's actually the same behaviour, just stick it in a function. If it's similar behaviour and the only difference is the data involved then parameterise the parts of the behaviour that need to vary and pass the parameters in as function arguments. So far I don't see anything unusual about what you're trying to achieve. If I'm missing the point then perhaps you'd better post some sample code to demonstrate your current approach and see if any improvements can be suggested.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Problem: You want certain data to be available to more than one function, but you don't want to make that data global.

Solution 1: Declare a class, making both the data and the functions members of that class.


+1

This is exactly what OOP is for
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So any suggestions?

I don't see why you cannot use a function to group those tasks together.

To communicate with the function, you can use parameters, or global variables, or a "payload": a collection of dissimilar things in a struct. So before executing that function, you load the payload with the right parameters / pointers, and then execute the function (passing the payload to it).
Logged

Manchester, New Hampshire
Offline Offline
Edison Member
*
Karma: 4
Posts: 1352
Propmaker
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So any suggestions?

I don't see why you cannot use a function to group those tasks together.

To communicate with the function, you can use parameters, or global variables, or a "payload": a collection of dissimilar things in a struct. So before executing that function, you load the payload with the right parameters / pointers, and then execute the function (passing the payload to it).


Maybe some example code will help explain:

Code:
          if (port::state[PORT_TOGGLE_MODE] == LOW) { // If mode switch is on...
           
            // Play sound here?
           
            led::set(16, 1); // Turn on slo-blo LED.
            mode = MODE_SLOBLO;
           
          } else {
           
            led::set(16, 0); // Turn off slo-blo LED.
            mode = MODE_FULLSTREAM; 
           
          }

This is the block of code I want to duplicate.  If I were to just copy this into a function and pass the 'mode' variable, the constants PORT_TOGGLE_MODE, MODE_SLOBLO, and MODE_FULLSTREAM would no longer be accessible.  So I would have to duplicate those in my function, or make them global.  Neither being a very pretty solution.

Of course what some of the others have suggested, using a namespace, as I have done with led and port there, is probably the right answer.  I'm not sure why some have suggested using a class though.  When I wanted to know how to set up my leds I'd asked about using classes and I got yelled at because the leds are not objects, just elements in an array. smiley  And since all this code is not an object which I would ever have more than one of, it doesn't seem to make sense to use a class for it either.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2048
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When I wanted to know how to set up my leds I'd asked about using classes and I got yelled at because the leds are not objects, just elements in an array.   And since all this code is not an object which I would ever have more than one of, it doesn't seem to make sense to use a class for it either.

It makes enough sense to be useful.
A class can be an object or an interface, if you only have static members you can treat it like a namespace. The advantage being the protected and private areas.
Logged


Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


This is the block of code I want to duplicate.  If I were to just copy this into a function and pass the 'mode' variable, the constants PORT_TOGGLE_MODE, MODE_SLOBLO, and MODE_FULLSTREAM would no longer be accessible.  So I would have to duplicate those in my function, or make them global.  Neither being a very pretty solution.
Static class members (of the appropriate class).  Or globals passed as references to the objects upon construction.
Quote
Of course what some of the others have suggested, using a namespace, as I have done with led and port there, is probably the right answer.  I'm not sure why some have suggested using a class though.  When I wanted to know how to set up my leds I'd asked about using classes and I got yelled at because the leds are not objects, just elements in an array. smiley
Not by me smiley
Quote
And since all this code is not an object which I would ever have more than one of, it doesn't seem to make sense to use a class for it either.
Singleton.

The only difference (from a logical design standpoint) between (1) namespace (2) Class with all static members and (3) Class with static and non-static members, is that with 1 and 2 if you change your mind later and want "more than one of them", you have to edit a bunch of code, both "client" ( the code that is calling it) and "server" (the code that is being called).

IMO smiley 

I come from mostly a large-scale-project environement, and such approaches are often not "worth it" in the uC-- especially hobbiest or prototype -- environment.  But the concerns you raise about your project make these approaches more relevant.

Cheers,
John

ref
"GOF Design Patterns"
"Lakos Large-Scale C++ Software Design"
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 472
Posts: 18693
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the block of code I want to duplicate.  If I were to just copy this into a function and pass the 'mode' variable, the constants PORT_TOGGLE_MODE, MODE_SLOBLO, and MODE_FULLSTREAM would no longer be accessible.

You've already got a class, have you? Or a namespace? Can't this extra "code I want to duplicate" be another function inside the class and have access to those constants?

Can you post enough code to demonstrate the whole problem? Like, the class/namespace, and two blocks of code that you want to duplicate (are duplicates of each other).

From what you've posted I don't see the problem.
Logged

Manchester, New Hampshire
Offline Offline
Edison Member
*
Karma: 4
Posts: 1352
Propmaker
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the block of code I want to duplicate.  If I were to just copy this into a function and pass the 'mode' variable, the constants PORT_TOGGLE_MODE, MODE_SLOBLO, and MODE_FULLSTREAM would no longer be accessible.

You've already got a class, have you? Or a namespace? Can't this extra "code I want to duplicate" be another function inside the class and have access to those constants?

No, the functon that code is inside of is neither in a class or namespace.


Quote
Can you post enough code to demonstrate the whole problem? Like, the class/namespace, and two blocks of code that you want to duplicate (are duplicates of each other).

From what you've posted I don't see the problem.

I'll try to explain it a little better.

I have my main program.  I have my main loop in my main program.  My main loop handles timing and input and puts that data in globals so functions can access it.  And it calls a function, protonpack():

Code:
/* ---
Main loop - Executes continuously while the system is powered.
--- */

  void loop() {

    // Constants:
    // Statics: (These are initialized once and their value is maintained on each subsequent loop.)
    // Temporary variables:
       
    // Timing:
   
      lastTime = time;                           // Store start time of last update.
      time = millis();                           // Get current system time.
      timeDelta = time - lastTime;               // Calculate how long last update took, in milliseconds.
      timeDeltaSec = float(timeDelta) / 1000.0;  // Convert last update period into seconds.
      //Serial1.println(timeDelta);

  // Read all switch states:
    readIOPorts();
     
  // This is where all the magic happens:
    protonpack();
     
  // Update LEDs:
    updateLEDs();
 
  // Play any queued sound effects once the current sound effect ends:
    updateSound();
 
  // Calculate time left in this frame and delay if needed.
  //  timeDelta = millis()-time;
  //  if (timeDelta < 16) { delay(16-timeDelta); }
 
}


That function contains all the code that makes the board do all this:


And the contents of that function are basically a large switch statement that performs whatever actions are needed based on the current state.

But let's say I want to reconfigure the board to do this:


In that case, I would like to replace the call to protonpack() with a call to ghosttrap() and not have to mess with any of the code in my main program.  And sticking a bunch of globals in my main program which are specific to protonpack() is a big no-no, because then I'm altering my main program and I, or whomever else is modifying the code, might accidentally delete something they shouldn't, or do something else to break everything.

I'm basically trying to keep the code that drives everything behind the scenes, and everything protonpack() does, completely separate from eachother.

And it seems like sticking it in a namespace or class might be the best solution, although I am not sure if the IDE will allow me to put that code in a tab by itself rather than sticking it in my main program where I don't want it.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 223
Posts: 6592
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm basically trying to keep the code that drives everything behind the scenes, and everything protonpack() does, completely separate from eachother.

And it seems like sticking it in a namespace or class might be the best solution, although I am not sure if the IDE will allow me to put that code in a tab by itself rather than sticking it in my main program where I don't want it.

When defining a new class, typically you split it into a declaration, which goes in a .h file in one tab, and its member definitions, which go in a .cpp file in another tab. Then you #include the .h file in your main sketch.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: 1 [2]   Go Up
Jump to: