Multiple calls !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Hi and I got trouble about my class.
No problem, no crash errors, but code style.
Maybe it's too long.
It contains lots of functions. (About 100)
I always work with it with jumble code.
Here, example :
1
2
3
4
//Class *A;
A->func0([...]);
A->func1([...]);
A->func2([...));

Here is the expected code (shorter, simpler, smarter)
1
2
 
A->func0([...])->func1([...])->func2([...])->...

That's it! Very clear!!!!
Does C++ support this trick?
..I want to apply this change into my project.
(Currently I'm not beginner but not advanced, please help...)
Last edited on
You can do it by having each of the functions return "this"...but honestly, that looks kind of odd unless you have a very good reason for that type of functionality.
Are all 100 functions written in a line such a way?:)
You can do it by having each of the functions return "this"...but honestly, that looks kind of odd unless you have a very good reason for that type of functionality.

Zhuge, thanks you so much!!! My class still works properly. Now my code is 2x simpler more.

Are all 100 functions written in a line such a way? :)

Thanks you for your funny question!!!


My Edit:
In short, all functions are certainly separated into many types. (GET() - SET() - OPERATE() - ADDITIONAL(),...)

GET() - Currently they are not affected by ("return this") but in future they will be replaced by alias or pointer. :)

SET() - Finished replacing (void -> this)

OPERATE() - "void" is redundant, so all OPERATE() are changed immediately now.

ADDITIONAL() - No need to want 'void' to be kept.

-> Stop using 'void'! It's so horrible!!!!!!!!!!!

But, only an exception : a few special functions will never be applied "this" returning value.

I can't list all functions (they are written on a page 1000 lines !!!!!). But I can show the structure of the class (Demonstration mode)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//Short version of Math structure
class Math{ //Math Manager
Math([...]){[...]}~Math([...]){[...]}
///////////////////////////////////////////////////////////////////////////////////////////
struct Data{ //Data Manager
public :
~Data([...]){[...]}Data([...]){[...]}
///////////////////////////////////////////////////////////////////////////////////////////
struct subData{ //Child Sata Manager
subData([...]){[...]}~subData([...]){[...]}
subData *Get(double &value){value = data;return this;}
subData *Set(double value){data = value;return this;}
Data *root(int Pos = -1){return (Pos == -1) ? pData : pData->Go(nPos);}
Math *Mroot(){return pData->root();}
subData *Go(int nPos){return &pData->subData[nPos];}

//[...] Many more functions...
private :
Data *pData;double data;
//[...] Many more variables...
}subdata[100]; //Size example
///////////////////////////////////////////////////////////////////////////////////////////
Data *Get(double &value){value = data;return this;}
Data *Set(double value){data = value;return this;}
Math *root(){return pMath;}
Data *Go(int nPos){return &pMath->data[nPos];}
Data *Calculate(){[...] return this;}

//[...] Many more functions...
private : 
Math *pMath;double data;
//[...] Many more variables...
}
///////////////////////////////////////////////////////////////////////////////////////////
data[100]; //Actually it's a pointer (no size)

Math *Calculate(int nCount){[...] return this;}

[...] Too many other functions.... ????!?!?!?!??!
private :
//Many variables
};

I only list some of the most popular and important functions which are using in my project. With simple calculation, I can write :

Here, very very awful code :
1
2
3
4
5
6
7
8
9
10
11
//Math * mObj 
mObj = new Math([...]);
mObj->data[0].Set(12.53);
mObj->data[0].subdata[0].Set(6.22);
mObj->data[0].subdata[1].Set(76.35);
mObj->data[1].Set(24.22);
mObj->data[1].subdata[0].Set(11.13);
mObj->data[1].subdata[1].Set(35.25);
mObj->data[1].subdata[2].Set(17.25);
mObj->Calculate(2);
//Many more commands...?!?!??!?!?!!?!?!??!?!?!?!?!??!?!?!?!?!?!?!??!?!?!?!?! 

No !!!!!!!!

And the solution (It's best !!!!!!!!!!!!!!!!!) :
1
2
mObj->data[0].Set(12.53)->subdata[0]->Set(6.22)->Go(1)->Set(76.35)->root(1)->
Set(24.22)->subdata[0].Set(11.13)->Go(1)->Set(35.25)->Go(2)->Set(17.25)->Mroot()->Calculate(2);


Oh, I'm very happy!!!!!! what do you think? :)

Last edited on
I think you should use less !

And I think that the allegedly "very very awful code" looks much nicer and you can tell what it does while "the solution" looks horrible.

Yes, it is one line, but shortness is not everything
Last edited on
And the solution (It's best !!!!!!!!!!!!!!!!!) :

1
2
mObj->data[0].Set(12.53)->subdata[0]->Set(6.22)->Go(1)->Set(76.35)->root(1)->
Set(24.22)->subdata[0].Set(11.13)->Go(1)->Set(35.25)->Go(2)->Set(17.25)->Mroot()->Calculate(2);


Good grief, that's terrible! It makes your code so much harder to read. It's totally at odds with the way people normally write code, and only serves to obscure what you're doing.

Cramming as much functionality as you can into one statement is rarely a good thing.
@MikeyBoy

Be wary of Jackson Marie, I am not sure whether she? is trolling or just has no idea .
Be wary of Jackson Marie, I am not sure whether she? is trolling or just has no idea .

Having read some of his/her other posts, I'm inclined to share your suspicions :)
Oh. So many answer.
Thanks everyone who replied me!


About unusual idea : This is only a tiny part. The demonstration example is modified a bit and show a code that "only modifies values by functions and funtions" is redundant and also it's horrible. I usually use it to make 'multiple functions calling' become simpler, because currently I using a lot of 'Math' classes also the source is very large and complex. (Maybe) It's the last method to solve this 'long code' problem. The example is limited, so 'the code looks horrible' is right, with 'limited functions, variables', and actually I have no excuse to do that, try the 'awful' code is better. No need to force all commands in a line, I only wrap several functions & commands into a group, which are used to do something. When I set a value, I use the simple one but when I do something which requires some other functions to complete (if convenient), I use the 'this' returning method. The important commands in the source are and will never be applied 'this' returning value now and future, because I know it will be more complex and horrible. My targets usually are the Long-name functions, easy to read and figure out, not very short and brief such as "Get() - Go()", it will give the source harder to view and understand. Odd idea but I have a very proper reason to continue using it without code looking or anything else... It's only the code example and certainly we never want a shortness command that looks awful to be added into the source or everything.... That's very silly!

Long story short, this is a powerful method to reduce the length of commands and maybe this may give everyone a better look when you know how to use it properly and put it to a proper place. More honestly, I'm trying tweaking and optimizing my class structure by many methods : Reduce number of functions, number of parameters in a function calling list, merge functions, function calling an another function, change algorithm, name hints, #enum, #define, child structures, custom functions calling, and much more... It's wonderful and I'm ready to take any new trick or tweaking method without any doubt because my code need to be improved a lot.

THANKS ! :)

Edit : Any suggestion, trick, tip or experience for any big project (Included me) ???
Last edited on
Topic archived. No new replies allowed.