Code indentation... outdated?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Code indentation... outdated?

Post by allspiritseve »

VladSun wrote:Indentation is a must! :)
Ok, but how much indentation?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Code indentation... outdated?

Post 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.
Last edited by VladSun on Tue Nov 18, 2008 9:36 am, edited 2 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Code indentation... outdated?

Post 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!
Last edited by papa on Tue Nov 18, 2008 9:17 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Code indentation... outdated?

Post 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 ;)
Last edited by VladSun on Tue Nov 18, 2008 9:36 am, edited 4 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Code indentation... outdated?

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Code indentation... outdated?

Post by VladSun »

oops

#! TO BE REMOVED ;)
Last edited by VladSun on Tue Nov 18, 2008 9:37 am, edited 3 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Code indentation... outdated?

Post 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? ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Code indentation... outdated?

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Code indentation... outdated?

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Code indentation... outdated?

Post 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
);
 
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Code indentation... outdated?

Post 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. :)
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Code indentation... outdated?

Post 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.
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Re: Code indentation... outdated?

Post 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:
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Code indentation... outdated?

Post 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]
Last edited by VladSun on Thu Sep 09, 2010 4:36 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Re: Code indentation... outdated?

Post 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.
Post Reply