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

thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Code indentation... outdated?

Post by thinsoldier »

allspiritseve wrote:

Code: Select all

so
    indenting
        the
            hell
                out
                    of
                    your
                code
            is
        a
    python
thing?

Code: Select all

so
indenting
  the
    hell
      out
        of
          your
        code
      is
    a
  python
thing?
I use actual TABS for indentation and set my text editors to display tabs as only 2 or 3 spaces.
5 or 8 space tabs is complete overkill and makes some things impossible to read.
2 is good.
Warning: I have no idea what I'm talking about.
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 »

thinsoldier wrote:I use actual TABS for indentation and set my text editors to display tabs as only 2 or 3 spaces.
5 or 8 space tabs is complete overkill and makes some things impossible to read.
2 is good.
No matter the number of spaces you use, I still find a semi-circle margin like that very hard to read. It also still means if I edit something 8 tabs in, I have to hit the tab button 8 times before I can type.

Also-- your editor may only use 2 or 3 spaces, but what about when somebody uses view source? Or when you're away from your computer and need to edit a file, and the only thing available is cpanel's crappy editor?
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:(1) No matter the number of spaces you use, I still find a semi-circle margin like that very hard to read. (2)It also still means if I edit something 8 tabs in, I have to hit the tab button 8 times before I can type.

(3) Also-- your editor may only use 2 or 3 spaces, but what about when somebody uses view source? (4) Or when you're away from your computer and need to edit a file, and the only thing available is cpanel's crappy editor?
(1) Every indented code block is called a statement - it has its own meaning. So one can understand what the "outter" statement does without even looking at the inner statement. In fact, many editors have even "statement collapse" feature ... guess why ;)

(2) Editors (I don't mean notepad!) will grant you a "free" tab indentation without pressing the tab key more than once.

(3) That's why I vote for using Tabs instead of spaces.

(4) And what if you have only 6x3 chars screen, or a WAP interface to cpanel, or Notepad only availabale, but Unix style new lines, or ...
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:(4) And what if you have only 6x3 chars screen, or a WAP interface to cpanel, or Notepad only availabale, but Unix style new lines, or ...
The above use-cases I listed are ones I've often come upon... can't say the same for yours. Nice try.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Code indentation... outdated?

Post by josh »

allspiritseve wrote:No matter the number of spaces you use, I still find a semi-circle margin like that very hard to read. It also still means if I edit something 8 tabs in, I have to hit the tab button 8 times before I can type.
Not if you use an IDE made in the last 10 years! :D
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:Not if you use an IDE made in the last 10 years! :D
I'm using eclipse... obviously in some cases it auto-indents, but in others I still have to hit tab quite a number of times.
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:(4) And what if you have only 6x3 chars screen, or a WAP interface to cpanel, or Notepad only availabale, but Unix style new lines, or ...
The above use-cases I listed are ones I've often come upon... can't say the same for yours. Nice try.
Believe or not I haven't seen the famous cpanel interface. But I have edited Unix format files with Notepad.
Anyway, I've just wanted to say that your 4th argument is not specific to indentation, but to editing in general ;)
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:
jshpro2 wrote:Not if you use an IDE made in the last 10 years! :D
I'm using eclipse... obviously in some cases it auto-indents, but in others I still have to hit tab quite a number of times.
I can hardly imagine how this could happen, but it would be because of the same reason if you say that editing with vi (or vim) is a nasty job to do.
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:Believe or not I haven't seen the famous cpanel interface. But I have edited Unix format files with Notepad.
Anyway, I've just wanted to say that your 4th argument is not specific to indentation, but to editing in general ;)
Well, sure, but my main point is that excessive code intentation isn't very readable, and I appreciate readable code whether its in a full-blown IDE, notepad, vim, cpanel's editor, or firefox's view source.

Along those same lines, I think you mentioned earlier that each statement has meaning, encapsulating the code it contains. If that's the case, some of that code might be better off inside another method. I think many times there isn't really a meaning though, and every single nested item is indented a tab. That seems ridiculous and excessive to me, and I'd rather see a block of text that had a functional meaning all on the same level of indentation. There are better ways to organize code semantically than tabs, I guess.
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 »

Willing to give us an example of your semantical code organization?
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:Willing to give us an example of your semantical code organization?
I don't have a specific example for you, but in general extracting methods on the back end, and grouping in blocks for the front end (header, nav, main content, footer, etc.) are common practices for me.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Code indentation... outdated?

Post by Christopher »

I think I agree with allspiritseve when it comes to HTML and other Domain Specific Languages. I think is what the OP was referring to. I can think of several area, tables for example, where full intention may be considered less readable.

However, I think indenting of program code is not really an open question anymore. There is very little variation since K&R days. It has become part of the orginazation of code expected by all programmers.
(#10850)
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 consider tables are an exception to the rule, just like ternary operators. In fact if you have multiple lines of code in a <td> I go ahead and give the start and end td tags their own lines and indent the code block inside
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 »

As I already mentioned, I think, there are no more deeply nested HTML tables nowadays - CSS and HTML have made a big progress since the days everything had to be put in tables in order to get a page layout. Even with pure XML, which I believe has the deepest hierarchy, every one uses well formatted tab indentation.
So... indentation is a must :)

PS: I would only agree with what jshpro mentioned - ternary operators (but not the nested ones!) are exceptions. But in fact, they return a value, so ... they are not really statements...
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 »

Well, it's a bit off topic but it's close, so I'll ask it:
Why, even professional ones, IT guys use one letter table names aliases in their SQL queries. And it's worse - the more complicated a SQL query is, the more people tend to use aliases. It's like using a one-letter-named variables in programming language, but it seems that while nearly nobody do this in his source code, a lot will do it in SQL...
I really can't understand it :(
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply