Skip to content

Commit e159209

Browse files
cglacetahans
andauthored
Format Flower class methods indentation to limit line wrap (#1003)
Co-authored-by: Alexander Hans <ahans@users.noreply.github.com>
1 parent 36cb0b0 commit e159209

File tree

2 files changed

+142
-75
lines changed

2 files changed

+142
-75
lines changed

concepts/headers/about.md

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@ If you want to write a library called "quick_math" that offers a function "super
1818
```cpp
1919
// A file named quick_math.h
2020
#pragma once
21+
2122
namespace quick_math {
22-
double super_root(double x, int n);
23+
24+
double super_root(double x, int n);
25+
2326
}
2427
```
2528
2629
```cpp
2730
// A file named quick_math.cpp
28-
#include "quick_math.h"
2931
#include <cmath>
32+
33+
#include "quick_math.h"
34+
3035
double quick_math::super_root(double x, int n) {
31-
while(n) { x = std::sqrt(x), --n;}
36+
while (n) {
37+
x = std::sqrt(x), --n;
38+
}
3239
return x;
3340
}
3441
```
@@ -51,31 +58,45 @@ One possible layout is to keep all the implementation details in the source file
5158
// A file named robot_flower.h
5259
#if !defined(ROBOT_FLOWER_H)
5360
#define ROBOT_FLOWER_H
61+
5462
#include <string>
63+
5564
namespace robots {
56-
class Flower {
57-
private:
58-
bool needs_water{};
59-
int size{};
60-
std::string name{};
61-
public:
62-
Flower(std::string name, int size = 0);
63-
void give_water();
64-
std::string get_name();
65-
int get_size();
66-
void start_next_day();
67-
};
68-
}
65+
66+
class Flower {
67+
private:
68+
bool needs_water{};
69+
int size{};
70+
std::string name{};
71+
72+
public:
73+
Flower(std::string name, int size = 0);
74+
void give_water();
75+
std::string get_name();
76+
int get_size();
77+
void start_next_day();
78+
};
79+
} // namespace robots
6980
#endif
7081
```
7182

7283
```cpp
7384
// A file named robot_flower.cpp
7485
#include "robot_flower.h"
75-
robots::Flower::Flower(std::string name, int size) {this->name = "Robotica " + name; this->size = size;}
76-
void robots::Flower::start_next_day() {if (!needs_water) ++size; needs_water = true;}
77-
std::string robots::Flower::get_name() {return name;}
78-
int robots::Flower::get_size() {return size;}
86+
87+
robots::Flower::Flower(std::string name, int size) {
88+
this->name = "Robotica " + name;
89+
this->size = size;
90+
}
91+
92+
void robots::Flower::start_next_day() {
93+
if (!needs_water) ++size;
94+
needs_water = true;
95+
}
96+
97+
std::string robots::Flower::get_name() { return name; }
98+
99+
int robots::Flower::get_size() { return size; }
79100
```
80101
81102
When the header is used as an API overview, that is where a person would look for information like default values.
@@ -87,21 +108,31 @@ Another layout option is a _header only_ library, that does not have a `.cpp` fi
87108
```cpp
88109
// A file named robot_flower.h
89110
#pragma once
111+
90112
#include <string>
113+
91114
namespace robots {
92-
class Flower {
93-
private:
94-
bool needs_water{};
95-
int size{};
96-
std::string name{};
97-
public:
98-
Flower(std::string name, int size = 0) {this->name = "Robotica " + name; this->size = size;}
99-
void give_water() {needs_water = false;}
100-
std::string get_name() {return name;}
101-
int get_size() {return size;}
102-
void start_next_day() {if (!needs_water) ++size; needs_water = true;}
103-
};
104-
}
115+
116+
class Flower {
117+
private:
118+
bool needs_water{};
119+
int size{};
120+
std::string name{};
121+
122+
public:
123+
Flower(std::string name, int size = 0) {
124+
this->name = "Robotica " + name;
125+
this->size = size;
126+
}
127+
void give_water() { needs_water = false; }
128+
std::string get_name() { return name; }
129+
int get_size() { return size; }
130+
void start_next_day() {
131+
if (!needs_water) ++size;
132+
needs_water = true;
133+
}
134+
};
135+
} // namespace robots
105136
```
106137

107138
Projects might use combinations of these layouts and there is a lot of discussion as to what might be the best fit for each use case.
@@ -129,9 +160,7 @@ int myFunction(int n) {
129160
}
130161
}
131162
132-
int myOtherFunction(int m) {
133-
return myFunction(m / 2);
134-
}
163+
int myOtherFunction(int m) { return myFunction(m / 2); }
135164
```
136165
137166
When `myFunction` is defined, the compiler does not know about `myOtherFunction` yet.
@@ -142,8 +171,8 @@ The compiler assumes that the definition will follow at some later point after t
142171
The next example shows how a forward declaration is used for functions.
143172
144173
```cpp
145-
int myFunction(int n); // Forward declaration of myFunction
146-
int myOtherFunction(int m); // Forward declaration of myOtherFunction
174+
int myFunction(int n); // Forward declaration of myFunction
175+
int myOtherFunction(int m); // Forward declaration of myOtherFunction
147176
148177
// Definition of myFunction
149178
int myFunction(int n) {
@@ -155,9 +184,7 @@ int myFunction(int n) {
155184
}
156185
157186
// Definition of myOtherFunction
158-
int myOtherFunction(int m) {
159-
return myFunction(m / 2);
160-
}
187+
int myOtherFunction(int m) { return myFunction(m / 2); }
161188
```
162189
163190
## Include Guards via ifndef
@@ -178,7 +205,9 @@ The syntax can be seen below with `MY_HEADER_FILE_H` as a variable.
178205
```cpp
179206
#ifndef MY_HEADER_FILE_H /* any name uniquely mapped to file name */
180207
#define MY_HEADER_FILE_H
208+
181209
// file content
210+
182211
#endif
183212
```
184213

concepts/headers/introduction.md

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@ If you want to write a library called "quick_math" that offers a function "super
1818
```cpp
1919
// A file named quick_math.h
2020
#pragma once
21+
2122
namespace quick_math {
22-
double super_root(double x, int n);
23-
}
23+
24+
double super_root(double x, int n);
25+
26+
} // namespace quick_math
2427
```
2528
2629
```cpp
2730
// A file named quick_math.cpp
28-
#include "quick_math.h"
2931
#include <cmath>
32+
33+
#include "quick_math.h"
34+
3035
double quick_math::super_root(double x, int n) {
31-
while(n) { x = std::sqrt(x), --n;}
36+
while (n) {
37+
x = std::sqrt(x), --n;
38+
}
3239
return x;
3340
}
3441
```
@@ -51,31 +58,49 @@ One possible layout is to keep all the implementation details in the source file
5158
// A file named robot_flower.h
5259
#if !defined(ROBOT_FLOWER_H)
5360
#define ROBOT_FLOWER_H
61+
5462
#include <string>
63+
5564
namespace robots {
56-
class Flower {
57-
private:
58-
bool needs_water{};
59-
int size{};
60-
std::string name{};
61-
public:
62-
Flower(std::string name, int size = 0);
63-
void give_water();
64-
std::string get_name();
65-
int get_size();
66-
void start_next_day();
67-
};
68-
}
65+
66+
class Flower {
67+
private:
68+
bool needs_water{};
69+
int size{};
70+
std::string name{};
71+
72+
public:
73+
Flower(std::string name, int size = 0);
74+
void give_water();
75+
std::string get_name();
76+
int get_size();
77+
void start_next_day();
78+
};
79+
80+
} // namespace robots
81+
6982
#endif
7083
```
7184

7285
```cpp
7386
// A file named robot_flower.cpp
7487
#include "robot_flower.h"
75-
robots::Flower::Flower(std::string name, int size) {this->name = "Robotica " + name; this->size = size;}
76-
void robots::Flower::start_next_day() {if (!needs_water) ++size; needs_water = true;}
77-
std::string robots::Flower::get_name() {return name;}
78-
int robots::Flower::get_size() {return size;}
88+
89+
robots::Flower::Flower(std::string name, int size) {
90+
this->name = "Robotica " + name;
91+
this->size = size;
92+
}
93+
94+
void robots::Flower::start_next_day() {
95+
if (!needs_water) {
96+
++size;
97+
}
98+
needs_water = true;
99+
}
100+
101+
std::string robots::Flower::get_name() { return name; }
102+
103+
int robots::Flower::get_size() { return size; }
79104
```
80105
81106
When the header is used as an API overview, that is where a person would look for information like default values.
@@ -87,21 +112,34 @@ Another layout option is a _header only_ library, that does not have a `.cpp` fi
87112
```cpp
88113
// A file named robot_flower.h
89114
#pragma once
115+
90116
#include <string>
117+
91118
namespace robots {
92-
class Flower {
93-
private:
94-
bool needs_water{};
95-
int size{};
96-
std::string name{};
97-
public:
98-
Flower(std::string name, int size = 0) {this->name = "Robotica " + name; this->size = size;}
99-
void give_water() {needs_water = false;}
100-
std::string get_name() {return name;}
101-
int get_size() {return size;}
102-
void start_next_day() {if (!needs_water) ++size; needs_water = true;}
103-
};
104-
}
119+
120+
class Flower {
121+
private:
122+
bool needs_water{};
123+
int size{};
124+
std::string name{};
125+
126+
public:
127+
Flower(std::string name, int size = 0) {
128+
this->name = "Robotica " + name;
129+
this->size = size;
130+
}
131+
void give_water() { needs_water = false; }
132+
std::string get_name() { return name; }
133+
int get_size() { return size; }
134+
void start_next_day() {
135+
if (!needs_water) {
136+
++size;
137+
}
138+
needs_water = true;
139+
}
140+
};
141+
142+
} // namespace robots
105143
```
106144

107145
Projects might use combinations of these layouts and there is a lot of discussion as to what might be the best fit for each use case.

0 commit comments

Comments
 (0)