Go Down

Topic: How to avoid code duplication in a function? (Read 1 time) previous topic - next topic

scswift

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. :)

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

johncc


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

dhenry

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).

scswift


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: [Select]

          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. :)  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.

Go Up