Shortening lines of declarations to one line.

Sep 12, 2019 at 7:30am
closed account (E8A4Nwbp)
how may I minimize the ensuing header file declarations, just like how in many IDEs, function curly brackets can be shut to one line

I can't simple put curly brackets around these unfortunately

1
2
3
4
5
6
7
8
9
10
    bool Millie = false;
    bool Al = false;
    bool Rube = false;
    bool Meg = false;
    bool Luch = false;
    bool Much = false;
    bool Gordon = false;
    bool Ed = false;
    bool Joy = false;
    bool Val = false;


IDE: VS2017 W10
Last edited on Sep 12, 2019 at 7:33am
Sep 12, 2019 at 8:04am
I can name that code in two lines:
1
2
bool Millie = false; bool Al = false; bool Rube = false; bool Meg = false; bool Luch = false;
bool Much = false; bool Gordon = false; bool Ed = false; bool Joy = false; bool Val = false;

The only realistic restriction is how far across the screen do you want the code to go.

In one line you get code that goes to almost 200 characters in length.
Sep 12, 2019 at 8:05am
I can't simple put curly brackets around these unfortunately

Well, you could put them inside a struct.
Last edited on Sep 12, 2019 at 8:05am
Sep 12, 2019 at 8:10am
Give more context. It is highly unlikely that these would be the NAMES of variables. It is more likely that they would be VALUES, probably in an array of objects:
1
2
3
4
5
6
7
struct Person
{
    string name;
    bool hasRegistered = false;
};

Person people[100];
Sep 12, 2019 at 11:42am
Is this a funny quiz of sort?

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    bool Al, Rube, Meg, Luch, Much, Gordon, Ed, Joy, Val = false;
    bool Millie = Al = Rube = Meg = Luch = Much = Gordon = Ed = Joy = false;
    std::cout << std::boolalpha
              << Millie << ' ' << Al   << ' ' << Rube   << ' ' << Meg << ' '
              << Luch   << ' ' << Much << ' ' << Gordon << ' ' << Ed  << ' '
              << Joy    << ' ' <<  Val << '\n';
}

Sep 12, 2019 at 12:03pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
using namespace std;

struct Person
{
    string name;
    bool hasRegistered = false;
    Person( const char * p ) : name(p){}
};

int main()
{
   vector<Person> people = { "Millie", "Al", "Rube", "Meg", "Luch", "Much", "Gordon", "Ed", "Joy", "Val" };
   for ( auto p : people ) cout << p.name << " " << boolalpha << p.hasRegistered << '\n';
}
Millie false
Al false
Rube false
Meg false
Luch false
Much false
Gordon false
Ed false
Joy false
Val false
Sep 12, 2019 at 8:12pm
do you need text versions of their names?
enum the names as offsets into a vector of bool.
honestly, after your early schoolwork, this kind of thing is the type of data you would put into a file and read in, more often than not. Hard coded piles of data in serious programs usually end up being constants / lookup tables only, rarely are data/names 'variables' (meaning, opposite of constant) hard coded in bulk (you sometimes do this in test code, but that is to the side of the real code).
Last edited on Sep 12, 2019 at 8:33pm
Sep 12, 2019 at 8:31pm
While I agree with the other suggestions, such as putting the values in a struct, or having an array, here is one way you can force the Visual Studio IDE to provide code folding:

1
2
3
4
5
6
7
8
9
10
11
12
#pragma region optional comment
bool Millie = false;
bool Al = false;
bool Rube = false;
bool Meg = false;
bool Luch = false;
bool Much = false;
bool Gordon = false;
bool Ed = false;
bool Joy = false;
bool Val = false;
#pragma endregion 


Probably won't work with other IDEs.

Personally, I think excessive code folding (beyond just folding a function or struct) is generally a bad practice. If your code is becoming too unruly to read, then split it into multiple files or refactor it. Exceptions exist of course, perhaps a really long machine-generated file, or in C# where you can use regions to separate interface implementations from other methods.
Last edited on Sep 12, 2019 at 8:34pm
Sep 13, 2019 at 3:29pm
closed account (E8A4Nwbp)
Thank you ever so much @Ganado ,I'll take your comment as well, I suppose it can suggest I code in a manner which is un-commendable. Certainly somewhere to improve.

Thats been of great assistance, it's utterly useful.

Last edited on Sep 13, 2019 at 3:31pm
Topic archived. No new replies allowed.