Starting Up Another Year

Pages: 12
As a reminder, virtually all assertions about the relative qualities of different bracket styles are either opinion or outright false.

That said, the correct response by the professor would have been "because you are giving your code to someone that requires it that way", with maybe an explanation thrown in about how almost every non-personal project has a code style that needs to be adhered to, even if it doesn't line up with personal preference.

-Albatross
A suggestion: try to get used to look at code formatted differently than how you'd format it. I generally write in K&R (if (){) and I inherited a codebase in Allman (if ()\n{) and I eventually learned to tolerate writing in it. Reformatting it was out of the question, obviously.
You should take this chance to acclimate to an unfamiliar style.
It hurt me to read. I wonder if there's a version of me in a parallel universe that likes coding with the brackets on the previous line.

I'll keep that in mind but I don't want to give it up yet </3
I am quite happy to have 2 formatters when there is a requirement. One to put in the required submitted form and one for working on it / reading it. Currently I get to define my own format, within reason. (No one cares about the brackets, but it still needs to be neat / good names / comments / etc).
Last edited on
Speaking of, I just put the pedal to the metal with my application. I tested it with some code I'd already written and it didn't seem to mess anything up, hope it never will. If it sees that an opening bracket (except for initializers) it'll move it the the previous line. If there's a comment on that previous line (which I tend to do and would make the bracket get commented out), it'll take that comment and move it an empty line above - with the correct spacing.

If anyone can test it out, see if it ruins their code or not, that would be great ! It'll make a new .cpp file - not alter the one given. Enter the directory or the file name (if working directory).

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
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>

int main()
{
	std::string name;
	std::cout << "Enter File Name: ";
	std::cin >> name;

	std::ifstream inFile;
	inFile.open(name);

	if (!inFile.is_open())
	{
		std::cout << "You Fucked Up!";
		return 0;
	}
	std::string code;
	std::string temp;

	while (std::getline(inFile, temp))
	{
		bool check = false;
		for (int i = 0; i < temp.size(); i++)
		{
			if (temp[i] == '{' && (temp[i+1] == NULL || temp[i + 1] == '\n'))
			{
				bool see = true;

				for (int j = 0; j < i; j++)
				{
					if (temp[j] != '\t' || (temp[j] > 32 && temp[j] < 127))
						see = false;
				}


				if(i == 0 || see)
				check = true;
			}
		}
		if (check)
		{
			for (int i = code.size()-2; code[i] != '\n' ; i--)
			{
				if (code[i] == '/' && code[i - 1] == '/')// && code[i + 1] != '\n')
				{
					std::cout << "GETTING HERE";
					std::string get;
					int j;
					for (j = i-1; code[j] != '\n'; j++)
					{
						get += code[j];
					}

					std::cout << get << '\n';
					code.erase(i-1, j-1);

					std::string gt;
					for (int k = i - 1; ; k--)
					{
						if (code[k] == '\t')
							gt+= '\t';

						if (code[k] == '\n')
						{
							gt += get;
							code.insert(k, "\n");
							code.insert(k+1, gt);
							break;
						}
					}

					break;
				}
				
			}

			code.insert(code.size()-1, " {\n");
		}
		else
		{
			code += temp;
			code += '\n';
		}
	}

	std::string n = "Code";
	std::ifstream File;
	std::string open = n;
	open += ".cpp";
	File.open(open);
	bool c = false;
	if (File.is_open())
	{
		c = true;
	}
	File.close();

	if (c)
	{
		n += '1';
		for (int i = 2; ; i++)
		{
			std::ifstream File;
			std::string open = n;
			open += ".cpp";
			File.open(open);
			if (File.is_open())
			{
				File.close();
				n.erase(n.size() - 1);
				n += std::to_string(i);
			}
			else
			{
				break;
			}
		}
	}
	n += ".cpp";
	std::cout << '\n' << n << '\n';
	std::ofstream outFile;
	outFile.open(n);
	outFile << code;

	_getch();
}
Last edited on
Two words (alas):

    “string literals”
I hope it's sufficient for your needs, because I can spot some ways that it would break in general use (also the empty lines it generates are an ugly giveaway that something happened). I'm not sure what level of feedback you'd like, so I'll leave it at that.

Personally I use clang-format, and am kinda surprised that nobody's recommended it yet.

-Albatross
Common problematic cases:
1
2
3
4
5
6
7
8
9
10
if (foo) /*this is
{ a comment*/
    single_line_if();

if (foo) //this is\
{ a comment
    single_line_if();

if (foo) now_theyre_just_messing_with_you("hehehe\
{");
Personally I use clang-format, and am kinda surprised that nobody's recommended it yet.
clang-format handles corner cases better than any other tool I've used.

I've never had it break my code, but I do need to turn it off (i.e., disable formatting in a region) occasionally.
Common problematic cases:

My program wouldn't break in any of those cases. Not that I tested, but it wont even touch multiline comments. But if there's something AFTER the "{" then it'll leave it alone since it'll be an initializer.


Two words (alas):

“string literals”

Is that bad? ;-;


I'll look up clang though. I don't know if my code is unbreakable, but at the very least I don't code in a way that would break it. The ugly empty lines are for the professor's viewing pleasure.
Last edited on
I'll look up clang

It appears you are using Visual Studio? If you are you can use Clang instead of the usual VS C++ compiler.

https://devblogs.microsoft.com/cppblog/clang-llvm-support-in-visual-studio/
My program wouldn't break in any of those cases.
Are you sure? Case #2 is especially problematic.
1
2
3
4
5
{
    if (x) //\
    {
        not_what_you_think();
}
Are you sure? Case #2 is especially problematic.

Holy Hell. I didn't even know that was a thing. I didn't see it because the formatting on this site didn't make the next line green, but I guess //\ makes that line and the following one a comment. Never used that before. A simple solution would be an extra line under the comment - just implemented it.

My Professor will love my code's layout :)


It appears you are using Visual Studio? If you are you can use Clang instead of the usual VS C++ compiler.

I don't know why I'd want to switch, VS's compiler has been great, don't know what Clang would do and I don't have much incentive to change it out.
I am not talking about Clang format, as was a large chunk of the previous discussion, I'm talking about having two different compiler tool chains available to use at your discretion.

Installing Clang in VS would give you an alternate compiler to use. You'd still have MSBuild to use the built-in VS compiler, or CMake scripts editable in the VS IDE to use Clang.
\
lets you extend a line. its really 1 line, but visually broken in the editor. It is common in complex macros.
mine would fail on that too. I handled comments, nested comments, strings, and more but not the multi-line thing.

I wasn't going to sell it or anything, its just something that can reformat most code stolen from online into the format I use.... a full featured one takes a lot of thought and time.

Mine basically finds /* and sets a boolean to do nothing (just dump input to output) until */ or whatever ending symbol(s) is found. Same for // and "" and so on.
Last edited on
I'm talking about having two different compiler tool chains available to use at your discretion.

I suppose that might come in handy! Probably will end up being too lazy to do it though.


a full featured one takes a lot of thought and time.

Yea, seems not worth the trouble because most cases wouldn't even be realistic. So far, the only case I know of that my program would fail is this one:

1
2
3
4
if() /*So Much
Stuff!*/ {

}



And so I just fixed that too since I was bored.
Used it on our first assignment, worked like a charm. The program I had to code was pretty pretentious though.
Topic archived. No new replies allowed.
Pages: 12