Amiga-Development

Please login or register.

Login with username, password and session length
Advanced search  

News:

Created for developers of all Amiga camps

Author Topic: Named Parameters in C/C++ ?  (Read 7677 times)

0 Members and 1 Guest are viewing this topic.

Team Chaos Leader

  • Administrator
  • Sr. Member
  • *****
  • Posts: 485
  • JC + Asm Coder
    • View Profile
Named Parameters in C/C++ ?
« on: July 04, 2013, 03:00:00 PM »

I have certain functions that are really huge and take a huge number of parameters.  It gets very confusing trying to figure out where I am at in my 500 char long lines of code.

I could simply break the lines down and have 1 parameter per line along with a comment to say which parameter this is.  I didn't do that because 1. I was in a giant hurry to bang out the code as quickly as possible and 2. I was afraid that at some point my lines would get desynchronized.  If not by me then by some future coding assistant.

So I had always "just assumed" that the correct answer was to implement TagList processing in my programs.  Then I could be kewl like AmigaOS!

But I just spent all day researching named parameters and now I think I have given up on TagLists and will either use the "Struct Method" or the "1 Parameter + 1 comment per line Method".
 

Here is an example of AmigaOS TagList style of Named Parameters:
http://en.wikipedia.org/wiki/Tag_(programming)
+ It works.
+ Parameters do not need to be in any particular order.
- No type checking at compile time!
- Uses CPU power at runtime to process the tags.
- I need to find a C/C++ version of Utility.library so that my code is fully portable to every platform forever and ever.  Is there such a thing?



Here is how everybody else does it:
http://en.wikipedia.org/wiki/Named_parameters

C Struct Method:
+ It works.
+ Parameters do not need to be in any particular order.
+ Type Checking at compile time
- 1:I must create a new struct.  2:Then CopyMem() the default values over from another previously setup struct.  3:Then fill in my parameters.  Maybe I can cook up a macro to do steps 1 & 2 for me automagically.


Here is how you can do it in C++ using Method Chaining
http://www.parashift.com/c%2B%2B-faq-lite/named-parameter-idiom.html
+ It works
+ Parameters do not need to be in any particular order
+ Type Checking at compile time
- A subroutine call for every parameter?!  I know it says "inline" but I don't trust it to generate efficient code in place of all those subroutine calls.
- Only works on C++



Here is the way Boost does it:
http://www.boost.org/doc/libs/1_53_0/libs/parameter/doc/html/index.html
+ It works?
+ Parameters do not need to be in any particular order.
+ Type Checking at compile time
- Only works on C++
- Seems to require an _ at the beginning of each parameter name?
- Melted my brain trying to read the docs.


Here is a C99 way to do named parameters:
http://rosettacode.org/wiki/Named_parameters#C
+ It works.
+ Parameters do not need to be in any particular order.
+ Type Checking at compile time
- Requires C99.  C99 seems more rare than C++ or C89.
- Won't work in SASC 6.58 according to my SASC 6.00 manual.

Why do I own a real SASC 6.0 manual and I have a .txt file for the v6.0 manual but no 6.58 manual?  Not even a 6.50 manual?  Did they add new features?  Or was it only bugfixes?  I am confused... What did they do for the last 0.58?



More examples of Named Parameters in C++
http://rosettacode.org/wiki/Named_parameters#C.2B.2B




Objective C has named parameters but they suck because they must be supplied in exactly the right order!
http://rosettacode.org/wiki/Named_parameters#Objective-C


Apparently C# v4.0 is the only C-oriented language with proper named parameters built right into the language in a LOGICAL FRIENDLY MANNER:
http://rosettacode.org/wiki/Named_parameters#C.23

Why can't C or C++ have named parameters like C# v4.0?


Visual Basic has proper named parameters also proving once again that BASIC is better than C or C++
http://rosettacode.org/wiki/Named_parameters#Visual_Basic



Which named parameter method do you use in your programs?
Logged

Matt Hey

  • Sr. Member
  • ****
  • Posts: 293
    • View Profile
Re: Named Parameters in C/C++ ?
« Reply #1 on: July 04, 2013, 05:35:01 PM »

I have certain functions that are really huge and take a huge number of parameters.  It gets very confusing trying to figure out where I am at in my 500 char long lines of code.

I don't like passing a lot of function parameters (or variable number of parameters). I prefer to make a context/struct of related data that is passed between functions. It's a very efficient method too. Tag lists are a variation that is more flexible but not quite as efficient. If you go to many named parameters and C++, pay attention to the code that is generated (the advantage of 68k assembler). I would be surprised if the code efficiency doesn't deteriorate.
Logged

Team Chaos Leader

  • Administrator
  • Sr. Member
  • *****
  • Posts: 485
  • JC + Asm Coder
    • View Profile
Re: Named Parameters in C/C++ ?
« Reply #2 on: July 04, 2013, 09:42:17 PM »

I have certain functions that are really huge and take a huge number of parameters.  It gets very confusing trying to figure out where I am at in my 500 char long lines of code.

I don't like passing a lot of function parameters (or variable number of parameters). I prefer to make a context/struct of related data that is passed between functions.
I wonder where the break-even point is?
Like if I pass 20 parameters then I can safely assume that it would be more efficient to pass a struct with the 20 parameters?

But what if there are only 12 parameters?

Or what if only 6?



Quote
It's a very efficient method too. Tag lists are a variation that is more flexible but not quite as efficient. If you go to many named parameters and C++, pay attention to the code that is generated (the advantage of 68k assembler). I would be surprised if the code efficiency doesn't deteriorate.
I don't like the C++ way of doing it simply because the whole thing feels like a scam.
1. They added OOP to a programming languge without first fixing all the flaws of their procedural programming model. (No named parameters)

2. Then they say "But look!  Now you can have named parameters by using OOP!  See how AWESOME OOP IS?"  Except simulating named parameters this way is silly.  Its like 20x the typing and you clutter your code with a bunch of extra lines of source.  This "work-around" method is better than nothing.  But proper support in the language would be much more beautiful.


So I can presently either list every single parameter on a line with a comment of the parameter name.  But:
- This way does not allow default parameters.  I want to have default settings for omitted parameters!


So now I am left with either doing the STRUCT method or adding more code to my preprocessor to add proper named parameter syntax to the C and C++ languages.
- I will need to spend a lot of time thinking up how to do it the best way.

There may be other minuses as the solution may cause the original pre-processed code to exist at a different line number than the post-processed code.  Syntax errors reported by the compiler would then all be off.
I wonder how Objective C handled such things?   The whole Objective C language was based on the same concept of preprocessing (compiling) a file down into C code which could then be compiled by any compiler.

+ If I spent months mastering transpiler technology then my script language could look any way I want it to look.  And it could do anything I want it to do!  And it would always work for me no matter what platform I wanted to compile for because every platform has a C-compiler, right?

I hate C.  So what I do, is I treat C as a kind of Assembly Language.  Why use Java ByteCodes?  Just use C.
Why use intel asm?  Just use C.

PortableE and Objective C and other languages do this too.
Logged

Veda

  • Hero Member
  • *****
  • Gender: Male
  • Posts: 1008
  • Sleep is overrated
    • View Profile
Re: Named Parameters in C/C++ ?
« Reply #3 on: July 04, 2013, 10:06:14 PM »

structs have some fun caveats.

For instance straight from the third edition C++ book, recursive.
Code: [Select]
struct List; //to be defined later

struct Link {
  Link* pre;
  Link* suc;
  List* member_of;
};

struct List {
  Link* head;
};
btw the semicolon is manditory after the curly braces this is something a lot of people forget about when working with structs.

And don't forget about type equivalence!
Logged

Team Chaos Leader

  • Administrator
  • Sr. Member
  • *****
  • Posts: 485
  • JC + Asm Coder
    • View Profile
Re: Named Parameters in C/C++ ?
« Reply #4 on: July 07, 2013, 09:27:16 PM »

Since I am going to be using structures to pass parameters to my "functions which take a huge number of parameters" I was thinking I should probably copy the structure over rather than just pass a pointer.  That way everything is 1000% safe.

1. Create new structure on stack
2. Copy the default parameters for this function into it.
3. Call the function with this structure (pass by value, not pass by reference). Woops!  What if I need to modify some of the values and return them?!

I was thinking if I just pass a pointer to the struct then the function might somehow someday be modified to pass on some of the parameters by reference to other functions and so forth and so on and then one of these functions might somehow make use of one of the values after the original function has long gone out of scope.  So I was thinking well I will just copy the whole struct over, problem solved.  But now I realize this prevents me from modifying the values and returning them.

So now I am back to passing a pointer to the struct?

That will probably be safe right?

Logged

Veda

  • Hero Member
  • *****
  • Gender: Male
  • Posts: 1008
  • Sleep is overrated
    • View Profile
Re: Named Parameters in C/C++ ?
« Reply #5 on: July 08, 2013, 02:42:56 AM »

TCl you beter use a private/public division in your code then.

Besides that it is beter to use structures to have memory ordend in a perticular manner since passing a pointer to a structure is as easy as struct.subtype in C++.
Logged

Matt Hey

  • Sr. Member
  • ****
  • Posts: 293
    • View Profile
Re: Named Parameters in C/C++ ?
« Reply #6 on: July 09, 2013, 01:48:55 AM »

I don't like passing a lot of function parameters (or variable number of parameters). I prefer to make a context/struct of related data that is passed between functions.
I wonder where the break-even point is?
Like if I pass 20 parameters then I can safely assume that it would be more efficient to pass a struct with the 20 parameters?

But what if there are only 12 parameters?

Or what if only 6?

I try to keep parameters to 4 or less but there are situations when this is not possible. It is probably a good idea to think of a better idea if there are 7 or 8 parameters. Processors that pass parameters in registers run out of registers and become less efficient. Amiga libraries that use more than 6 parameters can also become less efficient.

Since I am going to be using structures to pass parameters to my "functions which take a huge number of parameters" I was thinking I should probably copy the structure over rather than just pass a pointer.  That way everything is 1000% safe.

Copying the structure is not efficient. The advantage of a structure is that it can be reused in many functions and it's easy and fast to pass the pointer as a parameter. There is overhead in creating structures in memory though. I think you would be better off passing many parameters on the stack than creating and copying them every time. Tag lists would also be more efficient.

3. Call the function with this structure (pass by value, not pass by reference). Woops!  What if I need to modify some of the values and return them?!

The data in a structure/context can be modified. If you don't want to destroy the contained data, then that data can be copied before calling the function and restored or a whole new context created. For example, several Amiga functions operate on rasterports which are modified but a whole rasterport can be copied/cloned with completely different settings.

I was thinking if I just pass a pointer to the struct then the function might somehow someday be modified to pass on some of the parameters by reference to other functions and so forth and so on and then one of these functions might somehow make use of one of the values after the original function has long gone out of scope.  So I was thinking well I will just copy the whole struct over, problem solved.  But now I realize this prevents me from modifying the values and returning them.

So now I am back to passing a pointer to the struct?

That will probably be safe right?

Passing a pointer gets tricky if there is more than one task accessing the data but otherwise is safe enough. It's up to the programmer in C to decide on a system for accessing data structures. If you are worried about invalid pointers, then add conditional preprocessor debug statements that zero pointers when they are no longer valid and use private (static) and public (extern) global variables and functions as Veda mentions.
« Last Edit: July 09, 2013, 01:55:43 AM by Matt Hey »
Logged

Team Chaos Leader

  • Administrator
  • Sr. Member
  • *****
  • Posts: 485
  • JC + Asm Coder
    • View Profile
Re: Named Parameters in C/C++ ?
« Reply #7 on: July 09, 2013, 04:15:51 AM »

I don't like passing a lot of function parameters (or variable number of parameters). I prefer to make a context/struct of related data that is passed between functions.
I wonder where the break-even point is?
Like if I pass 20 parameters then I can safely assume that it would be more efficient to pass a struct with the 20 parameters?

But what if there are only 12 parameters?

Or what if only 6?

I try to keep parameters to 4 or less but there are situations when this is not possible. It is probably a good idea to think of a better idea if there are 7 or 8 parameters. Processors that pass parameters in registers run out of registers and become less efficient. Amiga libraries that use more than 6 parameters can also become less efficient.
I have megabajillions of functions.  Only 2-4 of them have these giant parameter lists.  But my code tends to grow larger each year.  So there will be more in the future.

Also the functions I am refering too are not called in a loop and their efficiency does not matter at all.  They spend most of their time waiting for the next vertical blank.





I was thinking if I just pass a pointer to the struct then the function might somehow someday be modified to pass on some of the parameters by reference to other functions and so forth and so on and then one of these functions might somehow make use of one of the values after the original function has long gone out of scope.  So I was thinking well I will just copy the whole struct over, problem solved.  But now I realize this prevents me from modifying the values and returning them.

So now I am back to passing a pointer to the struct?

That will probably be safe right?

Quote
Passing a pointer gets tricky if there is more than one task accessing the data but otherwise is safe enough. It's up to the programmer in C to decide on a system for accessing data structures.
Aha!  That is the only realistic potential problem.

I do have a lot of subtasks runing around doing this and that and I might accidentally forget to copy a var over.

I need to be really really careful to COPY THE VARS OVER before handing them to any subtasks (or threads as everyone calls them these days).

As long as I am not sending the parameters to another thread then its not even a problem and I will always be safe.


Thanx for helping me analyze my code.  Every team needs a MattHey friend function/object  ;D

Logged

EDanaII

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Named Parameters in C/C++ ?
« Reply #8 on: July 11, 2013, 04:57:01 PM »

TCL?

Have you considered something like this for your taglists?

http://stackoverflow.com/questions/5282779/c-dictionary-api

It's what I intend to try once I get back to my Amiga tinkering...

Something like:
Code: [Select]
#include <map>
map<LONG, LONG> dictionary;

// adding
dictionary.insert(make_pair(WA_CustomScreen, screen));

// searching
map<LONG, LONG>::iterator it = dictionary.find(WA_Title);
if(it != dictionary.end())
    cout << "Found! " << it->first << " is " << it->second << "\n";

// prints: Found! Foo is bar

Don't know if that's exactly right, as I'm nowhere near my trusty Amiga, but something like that should be possible. Then, when you are ready, you can convert the map back to a normal Amiga struct.

Just a thought...

Logged

SamuraiCrow

  • Administrator
  • Sr. Member
  • *****
  • Gender: Male
  • Posts: 376
  • Coolness is compiled
    • View Profile
Re: Named Parameters in C/C++ ?
« Reply #9 on: July 11, 2013, 07:26:12 PM »

@EDanaII

Why would you want to use a binary search tree (std::map) to do a search for a screen?  Also did you mean to open the screen before you made the pair?  Your code doesn't quite make sense in your example.
Logged

EDanaII

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Named Parameters in C/C++ ?
« Reply #10 on: November 06, 2013, 05:46:51 PM »

Sorry, SC, only just now saw this.

The example I gave was only meant to be illustrative. I wasn't trying to be technically accurate. IOW words, _it was a thought._ :)
 
I wouldn't mind trying it, not exactly as you see above, but I still have little time for Amiga related coding these daze...
Logged