Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you cleanly follow the Stepdown rule in C/C++?

The Stepdown rule encourages to read the code like a top-down narrative. It suggests every class/function to be followed by those at the next level of abstraction so we can read the code descending in the level of abstraction.

In C/C++ you need to declare classes/functions before you use them. So how would cleanly apply the Stepdown rule here? What are some pros and cons of the following approach? Any better one?

void makeBreakfast();
void addEggs();
void cook();
void serve();

int main()
{
    makeBreakfast();
}

void makeBreakfast()
{
    addEggs();
    cook();
    serve();
}

void addEggs()
{
    // Add eggs.
}

void cook()
{
    // Cook.
}

void serve()
{
    // Serve.
}  
like image 843
Carlos Perez-Lopez Avatar asked Sep 16 '25 04:09

Carlos Perez-Lopez


1 Answers

My approach is like yours but with either making a class so the declaration can come after use, or put declarations in a header file.

like image 97
Dani Avatar answered Sep 18 '25 18:09

Dani