Page 2 of 5

Re: Code indentation... outdated?

Posted: Tue Nov 18, 2008 8:50 am
by allspiritseve
VladSun wrote:Indentation is a must! :)
Ok, but how much indentation?

Re: Code indentation... outdated?

Posted: Tue Nov 18, 2008 8:57 am
by VladSun
allspiritseve wrote:
VladSun wrote:Indentation is a must! :)
Ok, but how much indentation?
As much as it is required.

Usually, if there is too much of it (which means - too much of nested statements!) then it's a wrong software design (e.g. a function/method extraction missed). But it has nothing to do with the indentation itself as a coding style.

Re: Code indentation... outdated?

Posted: Tue Nov 18, 2008 8:58 am
by papa
VladSun wrote:
papa wrote:I also think it's vital to intend code etc, but I thought we were talking about over-intending?
Define "over-intending" ;)

Indentation is a must! :)

Another thing that bothers me, is that I rarely see well formated (with indentation) SQL codes. Even by pro-programmers
Well, I don't know if I can give a good example but if we take an html table for example. You create table1 and want to add a table within that. I would not make a new row and indent from bla. I would make a new row and start from left again, probably commenting the table though.

Code: Select all

 
<table>
 <tr>
  <td>bla></td>
 </tr>
</table>
 

Code: Select all

 
<table>
 <tr>
  <td>
 
<!-- table2 -->
<table>
 <tr>
  <td>bla2</td>
 </tr>
</table>
<!-- //table2 -->
 
</td>
 </tr>
</table>
 
Maybe that is something all programmers already do but if you look around on different sites you see nested tables etc indenting, indenting, indenting...

Looking what I'm working in now, I'd say it's a common webpublishing-tool issue. But maybe people program that way I don't know.

Do I make sense?

edit:
It's called indent damn it!

Re: Code indentation... outdated?

Posted: Tue Nov 18, 2008 9:06 am
by VladSun
papa wrote:
VladSun wrote:
papa wrote:I also think it's vital to intend code etc, but I thought we were talking about over-intending?
Define "over-intending" ;)

Indentation is a must! :)

Another thing that bothers me, is that I rarely see well formated (with indentation) SQL codes. Even by pro-programmers
Well, I don't know if I can give a good example but if we take an html table for example.
A HTML table should be used only (I know it will be a flame ;) ) to represent a tabular data, so in order to have so much of indentation, the data you are trying to present should be with very deep hierarchy. Now give me an example of such data ;)

Re: Code indentation... outdated?

Posted: Tue Nov 18, 2008 9:24 am
by allspiritseve
VladSun wrote:But it has nothing to do with the identation itself as a coding style.
Why not? Correct me if I'm wrong, the indentation is supposed to increase readability... And yet usability studies recommend straight left margins of text (think form labels) in order to be read clearly.

I'd rather group my code in semantic blocks, using indentation sparingly for sections that really deserve being set off from the rest.

Re: Code indentation... outdated?

Posted: Tue Nov 18, 2008 9:26 am
by VladSun
oops

#! TO BE REMOVED ;)

Re: Code indentation... outdated?

Posted: Tue Nov 18, 2008 9:34 am
by VladSun
allspiritseve wrote:
VladSun wrote:But it has nothing to do with the identation itself as a coding style.
Why not? Correct me if I'm wrong, the indentation is supposed to increase readability
I mean: with or without it, if you have 1000+ (e.g.) nested statements it will be awful... Agree? ;)

Re: Code indentation... outdated?

Posted: Tue Nov 18, 2008 9:37 am
by allspiritseve
VladSun wrote:
allspiritseve wrote:
VladSun wrote:But it has nothing to do with the identation itself as a coding style.
Why not? Correct me if I'm wrong, the indentation is supposed to increase readability
I mean: with or without it, if you have 1000+ (e.g.) nested statements it will be awful... Agree? ;)
Agree. :D

Re: Code indentation... outdated?

Posted: Tue Nov 18, 2008 4:00 pm
by alex.barylski
Usually, if there is too much of it (which means - too much of nested statements!) then it's a wrong software design (e.g. a function/method extraction missed). But it has nothing to do with the indentation itself as a coding style.
Precisely.

Re: Code indentation... outdated?

Posted: Thu Nov 20, 2008 8:42 am
by josh
I even indent my SQL, like this:

Code: Select all

 
<?php
$q = sprintf(
    "
    SELECT
        `fields`
    FROM
        `a`
    LEFT JOIN
        `b`
    ON
        `a`.`1` = `b`.`1`
    AND
    (
        1 = 1
    or
        2 = %d
    )
    ",
    (int)$val
);
 

Re: Code indentation... outdated?

Posted: Thu Nov 20, 2008 8:47 am
by papa
VladSun wrote: A HTML table should be used only (I know it will be a flame ;) ) to represent a tabular data, so in order to have so much of indentation, the data you are trying to present should be with very deep hierarchy. Now give me an example of such data ;)
Don't know where I was going. Shouldn't drink at work. :)

Re: Code indentation... outdated?

Posted: Thu Nov 20, 2008 8:51 am
by allspiritseve
jshpro2 wrote:I even indent my SQL, like this:
I probably wouldn't go quite that far, I'd probably keep each clause on a line, instead of two lines, but: notice how there is a clear line running down all the clauses? That's very readable, and at a scan you can find the appropriate clause. The problem I see is when it's a big half-circle, readability is sacrificed.

Re: Code indentation... outdated?

Posted: Thu Nov 20, 2008 8:53 am
by xpgeek
jshpro2 wrote:I even indent my SQL, like this:

Code: Select all

 
<?php
$q = sprintf(
    "
    SELECT
        `fields`
    FROM
        `a`
    LEFT JOIN
        `b`
    ON
        `a`.`1` = `b`.`1`
    AND
    (
        1 = 1
    or
        2 = %d
    )
    ",
    (int)$val
);
 
I am doing it too, but not in so details. You are greate fun of indentation! :bow:

Re: Code indentation... outdated?

Posted: Thu Nov 20, 2008 9:22 am
by VladSun
My way:
[sql]SELECT
   web_language_context.name AS context_name,
    web_language_item.name AS item_name,
    web_translation.text AS item_text
FROM
    web_language_item
INNER JOIN
    web_language_context ON
        web_language_item.fk_context_id = web_language_context.id
INNER JOIN
    web_language ON web_language.id = ?
LEFT JOIN
    web_translation ON
        web_translation.fk_language_id = ?
        AND
        web_translation.fk_item_id = web_language_item.id
WHERE
    web_language_item > 0
    OR
    web_language_item = -1
GROUP BY
    web_language.id, web_language.name
ORDER BY
    web_language.name DESC, web_language.id
LIMIT
    0, 10[/sql]

Re: Code indentation... outdated?

Posted: Thu Nov 20, 2008 10:31 am
by xpgeek
VladSun wrote:My way: <skipped>
Looks good!

And it's my way:
[sql] SELECT              web_language_context.name AS context_name,             web_language_item.name AS item_name,              web_translation.text AS item_textFROM  web_language_itemINNER JOIN web_language_context ON              web_language_item.fk_context_id = web_language_context.idINNER JOIN web_language ON              web_language.id = ?LEFT JOIN web_translation ON              web_translation.fk_language_id = ?  AND             web_translation.fk_item_id = web_language_item.id WHERE             web_language_item > 0 OR             web_language_item = -1GROUP BY             web_language.id,              web_language.nameORDER BY             web_language.name DESC,              web_language.idLIMIT             0, 10[/sql]
This way JOIN'S is more structured imho.
In WHERE i put AND, OR after expression, so it's very compact.
And looks nice.