• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
TheComet

Chained functions in C

6 posts in this topic

(This is C btw, not C++)

 

I just came across the following. Regulator_t is a struct holding various parameters.

/* only one of these */
Regulator_t theRegulator;

int main()
{

    /* initialise regulator */
    set_coil_value( 0.0001f,                   /* 100uH coil */
    set_maximum_coil_current( 1.5f,            /* allow 1.5 amps */
    set_maximum_output_voltage( 200.0f,        /* cap output voltage to 200 volts */
    set_switching_voltage( 325.0f,         /* voltage from rectifier is 230*sqrt(2) = 325V */
    set_output_voltage_divider_ratio( 100.0f,  /* 1Meg to 10k is a ratio of 100 */
    initialise_regulator( theRegulator ))))));

    /* ---SNIP--- */
}

I'm not sure how to feel about this. Someone was obviously trying to be a smartass by defining all "set" functions with the pattern:

Regulator_t* set_whatever( float someValue, Regulator_t* regulator )
{
    regulator->someValue = someValue;
    /* do other important stuff */
    return regulator;
}

On one hand, it's a little confusing because the first section of code is executed "backwards", i.e. initialise_regulator is called first and then the chain is executed from the inside out.

 

On the other hand, it might actually be easier to read than doing it the traditional way:

initialise_regulator( theRegulator );
set_coil_value( theRegulator, 0.0001f );
set_maximum_coil_current( theRegulator, 1.5f );
/* etc */

Any thoughts?

Edited by TheComet
1

Share this post


Link to post
Share on other sites

I think it's nonidiomatic, hard to read, and far too Klever™.
 
The idiomatic way to chain functions in a C-based language is to return a pointer or reference to the object.

initialise_regulator(&theRegulator)->set_coil_value(0.0001f)->set_maximum_coil_current(1.5f)->...;

That way, the order of operations and the textual ordering are the same.  In C proper, the traditional way accomplishes the same thing.
 
You write programs for human readers.  If you try to make it harder for them (and "them" is more likely "you" a few months later), you're just being an ass.

Edited by Bregma
2

Share this post


Link to post
Share on other sites

I'm more interested in

    set_coil_value( 0.0001f,                   /* 100uH coil */
    set_maximum_coil_current( 1.5f,            /* allow 1.5 amps */
    set_maximum_output_voltage( 200.0f,        /* cap output voltage to 200 volts */
    set_switching_voltage( 325.0f,         /* voltage from rectifier is 230*sqrt(2) = 325V */
    set_output_voltage_divider_ratio( 100.0f,  /* 1Meg to 10k is a ratio of 100 */
    initialise_regulator( theRegulator ))))));

 Do you know what this code is attached to ?

 That looks like the settings for a transformer . *shrugs *

Seems much easier to use my HART communicator to soft set those values, than hard set them VIA code injection.

0

Share this post


Link to post
Share on other sites


I think it's nonidiomatic, hard to read, and far too Klever™


Yes, I guess it makes sense from a theoretical perspective as composition of functions, but I find it quite unreadable. I imagine he was inspired by method chaining in e.g. Java where methods mutate their instance and then return a reference to it, so you can write object.DoThis().ThenThat().ThisToo(), but due to the syntactic sugar it's actually executed left to right in that case. You could do the same thing in C using function pointers in the struct but as someone mentioned in your other thread this is hard to maintain, and you still need to pass "this" explicitly (excluding dirty hacks).



Would not recommend.

My thoughts exactly.




Bregma, on 12 Mar 2014 - 1:52 PM, said:
[quote name='Bregma' timestamp='1394628765' post='5138384']
The idiomatic way to chain functions in a C-based language is to return a pointer or reference to the object.
initialise_regulator(&theRegulator)->set_coil_value(0.0001f)->set_maximum_coil_current(1.5f)->...;

That way, the order of operations and the textual ordering are the same. In C proper, the traditional way accomplishes the same thing.
[/quote]

I'm pretty sure the function needs a pointer to the struct instance to be able to act on the data, so it gets tedious real fast if the variable has a longish name (though you can add newlines to tidy it up a bit, but then why don't you just use a proper initializer instead of abusing getters and setters for initialization purposes).

 

As Bacterius pointed out, the struct would have to hold a reference to every function to make that work, and you'd additionally have to pass the instantiated variable along with it:

initialise_regulator(&theRegulator)->set_coil_value(&theRegulator, 0.0001f)->set_maximum_coil_current(&theRegulator, 1.5f)->...;

You may as well spray camel's diarrhea in my eyes.

 

I'm more interested in

    set_coil_value( 0.0001f,                   /* 100uH coil */
    set_maximum_coil_current( 1.5f,            /* allow 1.5 amps */
    set_maximum_output_voltage( 200.0f,        /* cap output voltage to 200 volts */
    set_switching_voltage( 325.0f,         /* voltage from rectifier is 230*sqrt(2) = 325V */
    set_output_voltage_divider_ratio( 100.0f,  /* 1Meg to 10k is a ratio of 100 */
    initialise_regulator( theRegulator ))))));

 Do you know what this code is attached to ?

 That looks like the settings for a transformer . *shrugs *

Seems much easier to use my HART communicator to soft set those values, than hard set them VIA code injection.

You're pretty much spot on. It's part of a high power buck regulator (step down transformer). The values are hard-coded simply because there is only one version of hardware, and dynamically programming them would introduce additional sources of potential errors, since false values have disastrous effects like the power MOSFETs blowing up.

 

Much of the control logic was offloaded to a micro controller to save space, components, and price . The micro controller is only 0.79$ and yet is essentially a miniature oscilloscope. It can do what 50$ of hardware could have done, only faster and more accurately. The only downside is now you're more error-prone to software bugs, so I'm heavily relying on unit testing.

0

Share this post


Link to post
Share on other sites

Is it doing some magic under the hood with those set functions?  It seems like a simple function like, "InitDefaults(reg*) would do the same thing and be a bit clearer.  Granted it obscures the default values out of it, but if you are truly hardcoding those values anyway, does that matter?  Or, to be clearer, a specific function like InitToHardwareXValues()?

 

Hell, to be honest, unless you have a lot of programmers touching that structure, I'd be tempted to just make those values public.

Edited by ferrous
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0