Question:
Why do programmers make their code so compressed and hard to read?
?
2016-01-27 13:14:33 UTC
Before I started Honors Programming, I had looked up how to code in Java because I was really interested. So far, the only thing I have learned is something actually really important: formatting. My teacher really likes us using readable code. It is easier for him and for us to fix problems. Before the class started, my code looked like everyone else's on forums:

if(x<5){
for(int i=0;i<10;i++){
x+=i;
System.out.println(x);
}
}else{
moreStuff;
}

Why?? What is the point? If you are wondering, this is how we were taught:

if(x < 5)
{
for(int i = 0; i < 10; i++)
{
x += i;
System.out.println(x);
}
}
else
{
moreStuff;
}

Now that I look at both of them, I see how much more compressed it is, but still, that shouldn't be the only reason. It makes it harder to read and especially when you are answering someone's question on a forum post, why not make it more readable? It is just easier on the eyes.
Nine answers:
?
2016-01-27 13:21:42 UTC
Many languages don't enforce any kind of style on the programmer. The key is to decide on a style and then use it consistently.



I like the first one with a few alterations.



if (x < 5) {

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

        x += i;

       System.out.println(x);

    }

} else {

    moreStuff;

}



Spaces after keywords, before and after `else` and with operators. Naturally no one can agree on what "readable" means so a team might adopt a style guide so that all the code reads consistently.



Response 00: I don't have a problem seeing where an if statement starts and ends because that's how I'm used to looking at it. That and if my statements have more than a few lines then I'm probably trying to do too much and need to simplify.



I suppose it also helps when the editor adds dotted lines in the column connecting the opening of a statement and the closing curly brace.
?
2016-01-27 14:54:38 UTC
some already answered the question why people make their code so compressed and hard to read. As they like the style their code, however sometime it is not presentable to human. The code is for them not others.



Some people even believe less whitespaces and lines would make the code running faster. They forgot the basic concept the code for human then computer.



There is no right or wrong, JAVA normal use the first case but C#/C++ use the second. I use both depending on which language i am coding. I follow the default style by IDE editor.
Hello-thar
2016-01-27 14:55:10 UTC
It's just an opinion of what you prefer (or what the entire team agrees on).



I code using many forms, but it largely depends on the language I use.



For Java, I actually code by the top version, or the compressed version. The reason being is just my own opinion. I dislike having to scroll back and forth, up and down just to look at some code that's relevant, in Java. This is especially true if the code is kind of complex and has to be somewhat long for whatever reason. Java is pretty verbose and having everything on the same screen is just easier for me for some reason.



For C, I try to stick to K&R style, which is a mix between your two forms you presented. I have no idea why, but it's comfortable for me.



So on and so forth. Honestly, use what you like best and can agree on and just be consistent. I'm sure that's something everyone can agree on, at least. Consistency, that is.
?
2016-01-27 13:31:18 UTC
It is a matter of coding style preference. As you progress in programming you'll find that very few programmers have the exact same style. Personally I prefer the first style (with more whitespace and indents) because it's easier to tell where a given block begins and ends. If I were writing it that block of code would look more like this:





if( x<5 ){

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

x+=i;

System.out.println(x);

}

}else{

moreStuff;

}
Quentin
2016-01-27 15:17:12 UTC
The second version is harder to read because it is so spread out. You have to look a lot further. Then you spend lots of time scrolling up and down the page to see the other functions.



If you are using a decent IDE then it is not hard to find the matching bracket. Put the cursor on one bracket and the other one is highlighted. Press a hotkey and you can jump back and forth between two matching brackets. So it doesn't make any difference to finding the other bracket they are both just as easy.



Both your versions have superfluous brackets around a single line. This is poor style as it makes it more complicated than needs be. If the language designers wanted that they would have put in as a requirement of the language. Python requires good indentation by the compiler and there is no reason you can't set up your editor to require it in C or C++ as well, for example when you press "pretty print".



I notice that all

the

   answerers put their answers together

and

   don't have to spread

   the text out.



So,

   why do they think

            that it is easier to read?



They don't need

   to spread the text out

   when they write in English language!



// Brackets align with text. No superfluous brackets. This is a lot clearer than having the brackets further to the left than the contents of block.



if(x < 5)

   for(int i = 0; i < 10; i++)

      {

      x += i;

      System.out.println(x);

      }

else

   moreStuff;
?
2016-01-28 18:03:09 UTC
I prefer:



(x<5) ? for(int i=0; i < 10; x+=i++, System.out.println(x));: {

// more stuff

};



Some people pride themselves on consolidation. It's an artform. But, really, readability is very important. It's just not always the most important thing. Sometimes, conciseness is a code you have to crack in order to take another step towards zen.
Chris
2016-01-27 15:45:33 UTC
I do use spacing around operators and the like, but I don't put my opening brackets on a separate line.



if (x == 5) {

 DoStuff();

}



The indentation clearly shows where a block starts, but I know lots of people like to have their brackets in the same column.
?
2016-01-28 02:40:32 UTC
I am from the old school, programming within teams, and forced to respect strict rules of consistency.

I still use them.

The major "rule" is to ALWAYS line up VERTICALLY the pairs of parenthesis:

{

}

I hate the common form:

function foo () {

...

}

This has caused numerous "programmers" to get confused and miss some, then wondering why the compiler returns "Unexpeted End of file encountered". (the case here, hundreds of times.)
VP
2016-01-27 13:44:08 UTC
Ha ha, I like the 2nd version. Things line up better and I can see the braces easier.

To each their own, eh?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...