Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was taught by an old-time programmer who called this nested code

if(condition){
  <div>
    <b>text <u>text</u></b><br/>
    <table>
      <tr>
         <td>a</td>
         <td>b</td>
      </tr>
    </table>
  </div>
} else {
  log();
}

as opposed to this code which was considered to be not nested

if(condition){<div>
<b>text
    <u>text</u></b>
    <table><tr><td>a</td><td>b</td></tr></table>
    </div>
} else {log();}

When I started reading tutorials I found early on that I had the wrong definition to nested.

Messy vs clean is not sufficient for this because messy code encompasses other problems that I am not talking about, for example confusing flow of logic, using static initializers inappropriately, poor use of variable names, using twice the code to get the job done in a more confusing manner, etc.

What is the right word for this, where the first example is ... and the second example is not ...?

share|improve this question
3  
The real question is where is your <tbody>? –  WuHoUnited Sep 29 '11 at 0:22
1  
TBH, I think the correct word is actually nested. You could say indented, but nesting is more correct, since it implies indent/outdent-ing that follows the structure of commands/elements. –  Slomojo Sep 29 '11 at 1:40
add comment

5 Answers

up vote 3 down vote accepted

Nesting is related to the enclosing of control structures one into another, and this is usually indicated through different indentation levels within the source code.

You can say the first example is Nested Source Code, and the second is not, you would be correct. If you said it was simply indented, you don't provide any additional information with regard to why it's indented.

share|improve this answer
    
I guess Properly Nested would be the word. –  George Bailey Sep 29 '11 at 14:11
add comment

"Indented / indentation" may be the word you're looking for ?

... indent style is a convention governing the indentation of blocks of code to convey the program's structure... Indent style is just one aspect of programming style.

Indentation is not a requirement of most programming languages, where it is used as secondary notation. Rather, programmers indent to better convey the structure of their programs to human readers. In particular, indentation is used to show the relationship between control flow constructs such as conditions or loops and code contained within and outside them...

share|improve this answer
2  
Might be worthwhile adding that "nesting" is used in a more logically structural sense than the cosmetic "indentation" term. –  Yam Marcovic Sep 29 '11 at 0:10
    
Agreed, nesting/nested is a better word for this than simply indenting. –  Slomojo Sep 29 '11 at 1:41
add comment

I would say well formatted html or clean code. Like most computer languages formatting is not that important for the computer. However, for mortals like me proper formatting makes it much easier to understand. It doesn't take a lot of time to write clean code, but it will save you a lot of time in the long run. If I needed to debug the second example, the first thing I would do is reformat it.

There are various tools which will reformat the text so it is much more readable. Unfortunately, the results may show how poor the original code is. Many code generators produce pretty poor HTML, which may be the cause of or result from failing to properly format the code.

share|improve this answer
add comment

The following code is nested:

if (someCondition) { /*doSomething*/ } else { while (someOtherCondition) { if (someSubCondition) { /*doSomethingElse*/ } else { /*doSomethingCompletelyDifferent*/ }}}

Nested means, that some inner statements are nested within some outer statements. In fact, in most common programming languages, virtually all code is nested, because your code is nested with in functions, which are nested within modules. And in fact almost every statement nests other statements within itself.

Now the following code is indented:

doSomething();
      doSomethingElse();
      doSomethingElseYet();
doSomethingCompletelyDifferent();

On it's own, indenting only means, that you add horizontal whitespaces (usually space or tab) after linebreaks.

Now proper indenting as a part of proper formatting is the process of giving code a visual structure, that makes it's (nested) structure apparent (which is definitely not the case for the first code example). So let's properly indent it (without wanting to restart the religious war on the different indenting styles):

if (someCondition) { 
    /*doSomething*/
} 
else { 
    while (someOtherCondition) {
        if (someSubCondition) {
            /*doSomethingElse*/
        }
        else {
            /*doSomethingCompletelyDifferent*/
        }
    }
}

Now the visual structure of the code represents it's nesting. By the number of tabs preceding a statement, you can directly see how deeply it is nested. You can easily distinguish inner from outer statements, and see where statements start and end.

So to summarize, code formatting (which includes indenting) is the technique of using (mostly) non-significant characters to give source code a visual structure. Proper code formatting means applying this technique to give the code a visual structure, that communicates it's semantic structure.

share|improve this answer
add comment

I would say "formatted" or "well-formatted". I disagree with "clean code" and "tight code" which other posters have suggested.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.