I'm wondering why coders do not format their code...

Is there valid reason why not to format your code?

I’t clear to me, that those two code snippets are same for compiler

int main() {
	// input
	int arr[100];
	for ( int i = 0; i < 100; ++i ) {
		scanf( "%d", &arr[i] );
	}

	sort( arr, arr+100 );

	// output
	for ( int i = 0; i < 100; ++i ) {
		printf( "%d ", arr[i] );
	}
	printf("\n");

	return 0;
}

int main() {
int arr[100];
for ( int i = 0; i < 100; ++i ) scanf( "%d", &arr[i] );
sort( arr, arr+100 );
for ( int i = 0; i < 100; ++i )	printf( "%d ", arr[i] ); printf("\n");
return 0;
}

but (especially when you ask for help) why not to format it for others to understand it easier?

Ok, maybe you like writting the code like a pig, but at least for others, please use some formatting tool, f.e. http://www.prettyprinter.de/ (not my favourite, first result from google)…

Better link about formatters on SO - http://stackoverflow.com/questions/206441/online-code-beautifier-and-formatter

1 Like

On programming contests and recreative coding situations like on CC, CodeForces, etc, the code that it is written is usually not written with good programming practices in mind (regarding formatting, naming conventions, etc), but I would say it is written with both efficiency and algorithm exposure idea in mind…

If to solve a problem you need a binary search,for instance, and you are writing a very fancy code with a linear search, it won’t help you in achieving the result you want (that is to get accepted); Most coders around here are already very familiar with all these basic algorithms and write them in a very “quick-and-dirty” way, to get the job done.

In short:

Formatting helps and is encouraged, but sometimes, efficiency can overcome the formatting when it comes to be efficient. Exceptions to this are:

  • Begginer’s solutions;
  • Editorials solutions which are always very well organized and commented;

Don’t let the lack of formatting scare you away of the heart of the solution that lies on an underlying pattern that is hard to spot or on an incredible clever use of a Gaussian Elimination, or using FFT when you see no valid reasoning for it… These are the reasons that make recreative coding so appealing… And I can say that being (literally) forced to document all the code according to the standards for university is a pain in the ass, so, being able to write it how I want here is quite awesome to me :slight_smile:

Regards,
Bruno

3 Likes

I will write this a new answer, as I am posting some code snippets here.

I also think this is very, very language dependent…

For instance, if you start coding in C/C++ coming from a Python background, you might find the { } to be quite annoying and can grow to hate them and their use, as in Python all is more… visual, to an extent (Mandatory indentation)…

However, it also depends heavily on how the compiler parses the code you have written and the parser CAN ARGUE with the way you format your code…

Consider the Go programming language, and assume you are writing a for loop to sum from 1 to 10 in it:

sum := 0
for i := 0; i < 10; i++ {
    sum += i
}

In C++, it could be like:

int sum = 0;
for(int i = 0; i <= 10; i++){
    sum += i;
}

Parenthesis in C++ are mandatory, in Go they can be ommited…

In C++ you can place the { right after the for loop is written, or you can place it in a new line (for being more visually appealing) like this:

int sum = 0;
for(int i = 0; i <= 10; i++)
{
    sum += i;
}

However, almost nobody uses the second way… And why? Because all the usage of for loops was presented like the first way and coders assumed that form as the correct and natural way of writing a for loop, with the first curly brace right after the loop declaration…

This can be quite a dangerous thing to do, to make a standard out of something that it is NOT intended to be a standard… In fact, in Go, writing the curly brace right after the for loop declaration is mandatory or you will get a compile error, as the parser was written using GCC as a back-end in such a way that ; are placed everywhere where there are no { } or ( ), automatically… So we face a whole new situation where “embedded” programming style conventions are actually changing the way that compilers for new languages are being designed… Is this good?

I don’t know, only time will tell it, but this is something everyone should think about very carefully… :smiley:

Bruno

2 Likes

Thanks for your answer. My experience is, that more experienced coders format their code properly. I wrote thousands and thousands lines of code (most of them with Eclipse formatter turned on), but all my codes written in C++ I wrote in notepad and I’m formatting it somehow (I do not declare it as perfect formatting, especially for debug statement that I like to have on one line for easier commenting). But for example omit indentation of inner block is something almost unimaginable for me.

"Parenthesis in C++ are mandatory (…) "

Parenthesis != Curly braces :wink:

In Go they are also mandatory for multiple commands, just like in C/C++…

I was referring to the parenthesis ( ) enclosing the for loop itself… :slight_smile:

Heh, I saw you deleted your comment, so I deleted mine, sorry. Btw: nice way how to send message to the user (if you have enabled e-mail notifications…)

Ups, sorry about that :slight_smile:

I don’t have e-mail notifications on… Instead, I have this as my main page :smiley: