Problems in naming variables.

Pages: 12
Feb 4, 2012 at 5:58am
huh?

1
2
3
4
5
6
7
8
if(foo){
    bar();  // <- press enter here
    // <- indents to here
}

if(foo)
    bar();  // <- press enter here
// <- indents to here 


That's what I meant.
Feb 4, 2012 at 6:33am
http://www.gnu.org/fun/jokes/declarations.html

My favorites are :

#define answer_universe 42

unsigned long long contract;

1
2
3
4
class clown {
!friend class teacher;
friend class mate;
};


1
2
3
typedef int iq;
const iq of_steve_ballmer = -4, of_steve_jobs = MENSA_MINIMUM - 1,
         of_linus_torvalds = MENSA_MINIMUM, of_bill_gates = UNDEFINED;
Feb 4, 2012 at 7:47am
1
2
3
4
class clown {
!friend class teacher;
friend class mate;
};
What does the ! operator do to the friend declaration?
Feb 4, 2012 at 1:09pm
1
2
3
if(foo)
{  // smarter, immediately obvious there's a brace
    bar();


I can go either way, but I prefer to always have the braces to make code coverage output more useful. For example, a line like
if( foo ) return false; will always appear to be covered but I want to know that the return was taken as part of my unit testing.
Last edited on Feb 4, 2012 at 1:09pm
Feb 4, 2012 at 1:34pm
I also use the one-brace-per-line-style (when I'm writing C++ code anyways), but honestly if you're writing it like this:

1
2
3
if(...) {
   ...
}


There's ALWAYS a brace there, so the question whether it's obvious or not doesn't even come up.

Feb 4, 2012 at 8:29pm
It gets very, very, very confusing when you go even a few brace levels deep.
Feb 4, 2012 at 8:32pm
If you have to go more than 3 braces deep that's usually a sign your code is badly structured.
Feb 4, 2012 at 9:08pm
How else do I iterate through a std::map<std::string, std::map<std::string, float> > with conditional code? ;p
Feb 4, 2012 at 10:25pm
Well, why would have a std::map<std::string, std::map<std::string, float> > to begin with?
Feb 4, 2012 at 10:37pm
@L B,
I don't think the inside of a function counts as a level.

Contrived example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int CountsSomeValuesForSomeReason(int array[][][], int search, int x, int y, int z)
{
        int count;
        for (int i = 0; i < x; ++i) {
                // This is level 1
                for (int j = 0; j < y; ++j) {
                        // This is level 2
                        for (int k = 0; k < z; ++k) {
                                // This is level 3
                                std::cout << "array[" << i << "][" << j << "][" << k << "] = " << array[i][j][k] << '\n';
                                if (array[i][j][k] == search)
                                        ++count; // Too many levels!
                        }
                }
        }
        return count;
}
Feb 4, 2012 at 11:43pm
It's hard to see the matching opening braces.

@hanst99: How else would you set up a system where you have named groups of named values?
Feb 4, 2012 at 11:48pm
Extra braces and putting braces on a new line looks horrible... it's practically illegible to me. I don't understand how anyone can think that:
1
2
3
4
5
6
7
8
9
10
if (x)
{
    while (y) 
    {
        if (z) 
        {
            do(something);
        }
    }
}

Looks cleaner than:
1
2
3
4
if (x)
    while (y)
        if (z)
            do(something);

Or even:
1
2
3
4
5
6
7
if (x) {
    while (y) {
        if (z) {
            do(something);
        }
    }
}


I find that less is more, and the brace is just as obvious even when it's on the same line.
Last edited on Feb 4, 2012 at 11:48pm
Feb 4, 2012 at 11:54pm
Single-statement stuff obviously doesn't need braces, but when you have a lot of stuff going on it is best to use the first style you mentioned for clarity, otherwise finding matching braces is a NIGHTMARE

I used the third for so long and then I just gave up on it because it was so confusing to try and read
Last edited on Feb 4, 2012 at 11:54pm
Feb 4, 2012 at 11:58pm
closed account (3hM2Nwbp)
I tend to mix and match.

1
2
3
4
5
6
7
if (x)
    while (y) {
        if (z)
        {
            do(something);
        }
    }



*Runs from lynch mob of angry programmers.*


Seriously though, I do prefer the braces to be on a new line.
Feb 5, 2012 at 2:06am
There is no One True Brace Style.

Oh, wait...
Feb 5, 2012 at 4:56am
THE brace style:
9223372036854775803
9223372036854775804
9223372036854775805
9223372036854775806
if(condition)
{{{{{{{{{{{{{
    //code
}}}}}}}}}}}}}
Feb 5, 2012 at 10:21am
I find that less is more, and the brace is just as obvious even when it's on the same line.


Putting the braces on the same line is fine to me, leaving them out completely isn't.

@hanst99: How else would you set up a system where you have named groups of named values?


Wrap the logic in classes. Doing something with one dictionary is something else than doing the same thing to many dictionaries.
Last edited on Feb 5, 2012 at 10:23am
Feb 5, 2012 at 2:43pm
What did you think I was writing hanst? Lol, I am of course putting it in a class. There's no way to avoid the deep brace levels.
Feb 5, 2012 at 2:51pm
While I of course don't actually know what you want to write, I'd assume there is some logic connected to each map<string,float>. If so, it deserves it's own class, which is already at least one brace level less.
Feb 5, 2012 at 2:53pm
There is no 'logic' attached; it's quite literally just a way for me to have named floats in named groups. It's not for my application's logic, its for user usage.
Topic archived. No new replies allowed.
Pages: 12