General Question about format

I've been told a lot of different "proper" ways to format one's code. I was curious to you who are professional coders, what you thought about something like this?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <iomanip>
#include <fstream> 

using namespace std;

ofstream out("Overload.out");

class point
{
public:
     // Default Constructor
     point(double initial_x = 0, double initial_y = 0);

     // Modification Member Functions
     void set(double x, double y);

     // Constant Member Functions
     double get_x() const{return x_axis;}
     double get_y() const{return y_axis;}

private:
     double x_axis;
     double y_axis;
 };

// Prototypes
point operator + (const point& p1, const point& p2);
point operator - (const point& p1, const point& p2);
bool operator == (const point& p1, const point& p2);
bool operator != (const point& p1, const point& p2);
bool operator > (const point& p1, const point& p2);
bool operator < (const point& p1, const point& p2);
ostream& operator << (ostream& outs, const point& source);

point::point(double initial_x, double initial_y)
{  x_axis = initial_x, y_axis = initial_y; }

void point::set(double x, double y)
{ x_axis = x,  y_axis = y; }

point operator + (const point& p1, const point& p2) 
{    // Sum of p1 and p2 is returned
     double x_sum = (p1.get_x() + p2.get_x());
     double y_sum = (p1.get_y() + p2.get_y());
     point sum(x_sum, y_sum);
     return sum;
}

point operator - (const point& p1, const point& p2)  
{
    double x_diff = (p1.get_x() - p2.get_x());
    double y_diff = (p1.get_y() - p2.get_y());
     point diff(x_diff, y_diff);
     return diff;
}

bool operator == (const point& p1, const point& p2) 
{
     return
          (p1.get_x() == p2.get_x())
     &&
          (p1.get_y() == p2.get_y());
}

bool operator != (const point& p1, const point& p2) 
{
     return !(p1 == p2);
}

bool operator > (const point& p1, const point& p2)   
{
     return
          (p1.get_x() > p2.get_x())
          &&
          (p1.get_y() > p2.get_y());
}

bool operator < (const point& p1, const point& p2)   
{
     return
          (p1.get_x() < p2.get_x())
          &&
          (p1.get_y() < p2.get_y());
}

bool operator >= (const point& p1, const point& p2)   
{
     return
          (p1.get_x() >= p2.get_x())
          &&
          (p1.get_y() >= p2.get_y());
}

ostream& operator << (ostream& outs, const point& source)    
{
     outs << "(" << source.get_x() << "," << source.get_y() << ")";
     return outs;
}

void header()
{
     out << "\t\t\tPoints Class Overload Program Output\n";
}

void Equal()
{
     point a(6, 4), b(6, 4);
     out << "EQUAL\n";
     if (a == b)
          out << "True! Points " << a << " and " << b << " are equal.\n" ;
     else
          out << "False! Points " << a << " and " << b << " are not equal.\n";
}

void NotEqual()
{
     point a(2, 3), b(3, 2);
     out << "\nNOT EQUAL\n";
     if (a != b)
          out << "True! Points " << a << " and " << b << " are NOT equal.\n";
     else
          out << "False! Points " << a << " and " << b << " are equal.\n";
}

void Sum()
{
     point a(4,6), b(9,7), c;
     out << "\nAddition\n";
     c = a + b;
     out << "Sum of " << a << " and " << b << " = ";
     out << c << endl;
}

void Subtract()
{
     point a(2, 2), b(8, 9), c;
     out << "\nSubtraction\n";
     c = a - b;
     out << "Subtract " << a << " from" 
             << b << " = " << c << endl;
}

void Format()
{
     point a(1, 8);
     out << "\nOutputting a point in the ( , ) format.\n";
     out << "Point A: " << a << endl;
}

void CheckFurther()
{
     point a(5, 8), b(2, 4);
     out << "\nGreater Than\n"
             << "Point A(5,8) or Point B(2,4)\n" ;
     if (a > b)
          out << "True! Point " << a << " is LARGEST.\n";
     else   
          out << "False! Point " << b << " is LARGEST.\n";
}

void CheckCloser()
{
     point a(-4, 2), b(-3, 1);
     out << "\nLess Than\n"
             << "Point A(-4,2) or B(-3,1)\n";
     if (a < b)
          out << "True! Point " << a << " is SMALLER.\n";
     else
          out << "False! Point " << b << " is SMALLER.\n";
}

void GreaterThan()
{
     point a(7, 1), b(5, 8);
     out << "\nGreater Than or Equal to\n";
     if (a >= b)
          out << "True! Point " << a << " is equal or greater than Point " << b << ".\n";
     else
          out << "False! Point " << a << " is NOT equal nor greater than Point " << b << ".\n";
}

int main()
{
     header();
     Equal();
     NotEqual();
     Sum();
     Subtract();
     Format();
     CheckFurther();
     CheckCloser();
     GreaterThan();
     out.close();
     return 0;
}
I don't particularly like that style but everyone is different. I will say that your operators are not overloaded as part of the class and many of your functions have predetermined outcomes which basically make them useless. If i write

1
2
3
4
5
6
7
8
9
10
11
12

void fun(){

int a=1,b=2;

If (a>b) {cout<<"a";}
else{ cout<<"b" ;}
}

// you might as well just write 
Void fun(){ cout<<"b";}


Also your not equals operator is more than likely broken.
Last edited on
I've been told a lot of different "proper" ways to format one's code.


Yep. However if you become a professional employed programmer then your company will almost certainly have 'code style' guides to which you will be expected to follow. These usually cover such things as naming conventions, indentations, braces white-space etc etc etc. Some are more detailed than others.
1
2
point::point(double initial_x, double initial_y)
{  x_axis = initial_x, y_axis = initial_y; }


No! Use:

 
point::point(double initial_x, double initial_y) : x_axis(initial_x), y_axis(initial_y) {}



1
2
void point::set(double x, double y)
{ x_axis = x,  y_axis = y; }


I don't like the use of , operator in this context. I'd use:

1
2
3
4
5
void point::set(double x, double y)
{
    x_axis = x;
    y_axis = y;
}


1
2
3
4
5
6
7
point operator + (const point& p1, const point& p2) 
{    // Sum of p1 and p2 is returned
     double x_sum = (p1.get_x() + p2.get_x());
     double y_sum = (p1.get_y() + p2.get_y());
     point sum(x_sum, y_sum);
     return sum;
}


Why not simply:

1
2
3
4
point operator + (const point& p1, const point& p2) 
{    // Sum of p1 and p2 is returned
    return point(p1.get_x() + p2.get_x(), p1.get_y() + p2.get_y());
}


same for operator-

For operator >=, this is simply !operator<




Your indent style is adequate, however you need to work on your consistency.

If your operator overloads were part of the class it would, IMO, clean up the code a little.

I also dislike the use of global variables.

I generally like your style (with the concerns mentioned above).

I think the indentation of your operator functions might be considered slightly wonky, but not egregiously so. I would probably go with this instead.

1
2
3
4
5
bool operator > (const point& p1, const point& p2)   
{
     return ((p1.get_x() > p2.get_x()) &&
             (p1.get_y() > p2.get_y()));
}


But your version is easily understood. And that's most of the reason for following a consistent style.

One thing to consider (and there will be others on this forum who disagree with me). I recommend always putting if and else statements inside "{}". I have worked for 2 large companies with coding standards requiring this, so it's not simply the ramblings of a mad man suggesting this (although whether I am a mad man is still up for debate).

The reasoning is that sometime down the road you will need to add a statement to an existing if or else block, and forget to add the curly braces. Consider this addition to your Equal() function.

1
2
3
4
5
6
7
8
9
10
void Equal()
{
     point a(6, 4), b(6, 4);
     out << "EQUAL\n";
     if (a == b)
          out << "True! Points " << a << " and " << b << " are equal.\n" ;
     else
          my_global_not_equal_counter++;
          out << "False! Points " << a << " and " << b << " are not equal.\n";
}


At a quick glance, your code looks fine, and it compiles, but "False! Points..." will be printed every time Equal is called. If you had put the curlies into the code in the first place, you would not run into this problem.
Few things invoke more passion and waste more time than formatting wars. In my opinion, here's what matters:
- be consistent.
- take advantage of any automatic formatting that your editor provides.
- take advantage of tools that fully format code.

Some comments:
1
2
3
4
     return
          (p1.get_x() == p2.get_x())
     &&
          (p1.get_y() == p2.get_y());
This is certainly clear, but a little unusual and I worry that it will get complicated when you have a more complex expression.

To me, braces are there because the compiler requires them. Humans derive the block structure from the indentation. Seen this way, braces are an inconvenient necessity and formatting like this:
1
2
3
4
5
6
7
8
if (condition)
{
    true_statement;
}
else
{
    false_statement;
}
looks like this to a person
1
2
3
4
5
6
7
if (condition)

    true_statement;

else

    false_statement;
It is literally just double spacing the code. So I prefer this:
1
2
3
4
5
if (condition) {
    true_statement;
} else {
    false_statement;
}

One comment on your coding style (as opposed to the formatting style):
1
2
3
4
     if (a == b)
          out << "True! Points " << a << " and " << b << " are equal.\n" ;
     else
          out << "False! Points " << a << " and " << b << " are not equal.\n";
This is extremely dangerous. You should always use braces. The reason is that it's too easy to come back later and make this mistake:

1
2
3
4
5
     if (a == b)
          out << "True! Points " << a << " and " << b << " are equal.\n" ;
     else
          out << "False! Points " << a << " and " << b;
          out << " are not equal.\n";  // That line was too long 

Line 5 is actually outside the if/then/else block now so it always executes.

There's one exception: it's okay if the entire construct fits on one line:
if (a == b) doSomething();
@OP,

Obviously @dhayden and I agree about the return statements in your operator functions and the use of curly braces on if and else blocks.

We don't agree on placement of curly braces and what code looks like to a person.

All that so say, there are sharp disagreements in the coding community about where to place curly braces. Despite @dhayden's strident urging to not have opening curlies on separate lines, that's a decision you need to make for yourself. There are valid reasons for both ways. When you work for a company, they will probably tell you which way to do it.

The important thing here should not be to tell you where to put opening braces, but to tell you to choose one of the common styles in use today and use it consistently. If you are consistent, experienced programmers will be able to understand your code. That's what's important.
The important thing here should not be to tell you where to put opening braces, but to tell you to choose one of the common styles in use today and use it consistently.
I agree 100%. I didn't mean to say "use my way." I just wanted to show how I do it and why.
Topic archived. No new replies allowed.