Code indentation... outdated?
Moderator: General Moderators
Re: Code indentation... outdated?
Well I'm not referring to nested tables necessarily. It could be a few links with extremely long URLs, images with long pathnames, etc.. Sometimes it preferable to break it out to avoid wordwrap, and if so you might as well indent to make it explicit
-
thinsoldier
- Forum Contributor
- Posts: 367
- Joined: Fri Jul 20, 2007 11:29 am
- Contact:
Re: Code indentation... outdated?
I've worked with people who write absolutely no object oriented code, so asking them to break things up into sensible methods is out of the question.
Some time ago I think I heard one say they decided to never ever indent, ever.
And it wouldn't surprise me if someone took a file I wrote and grep/replaced every ^tab and ^space.
Indentation has it's very valid purposes. And it should be rare for anything to be indented beyond 4 or 5 tabs unless it was really necessary and I've looked through about 30 files in the last couple hours and the only time I've seen anything even remotely close to a semi-circular layout it was HTML (and most of the time it was done for a good reason.)
I'd say 85% of my code looks like this
Yes I put every {/} on a separate line. I can't find anything or think straight any other way.
Some time ago I think I heard one say they decided to never ever indent, ever.
And it wouldn't surprise me if someone took a file I wrote and grep/replaced every ^tab and ^space.
Indentation has it's very valid purposes. And it should be rare for anything to be indented beyond 4 or 5 tabs unless it was really necessary and I've looked through about 30 files in the last couple hours and the only time I've seen anything even remotely close to a semi-circular layout it was HTML (and most of the time it was done for a good reason.)
I'd say 85% of my code looks like this
Code: Select all
foreach($listarray as $addy)
{
$mail->AddAddress($addy);
if(!$mail->Send())
{
echo "Message was not sent";
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent to ";
}
$mail->ClearAddresses();
}
Warning: I have no idea what I'm talking about.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Code indentation... outdated?
I think it is because the table.column simply makes the statements really long and less clear. When you go to one letter you are almost abstracting the query to its essence and saying "here's what I am doing to tables A, B and C". The thing about SQL is that it does not really matter whether you are joining Companies and People or Cars and Parts. The content of the tables and columns is irrelevant in SQL. So think in A, B and C removes that noise.VladSun wrote: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
(#10850)
Re: Code indentation... outdated?
It's also better for abstraction. If one object is augmenting a query another object is generating, all it has to know is a, b, c.. even if the table names change in the first object, the SQL server maps the aliases at runtime for you, so all the augmenting object has to know is a is the main table in the query, b is the 2nd joined table, c is the 3rd joined table, etc... The force at play here is static coupling, changed to the more desirable form of coupling, dynamic coupling. Also like Christopher said its less typing
Re: Code indentation... outdated?
So, indeed, it doesn't matter whether it's a TABLE or a DIVjshpro2 wrote:Well I'm not referring to nested tables necessarily. It could be a few links with extremely long URLs, images with long pathnames, etc.. Sometimes it preferable to break it out to avoid wordwrap, and if so you might as well indent to make it explicit
There are 10 types of people in this world, those who understand binary and those who don't
Re: Code indentation... outdated?
Let me make some analogy with PHP:arborint wrote:I think it is because the table.column simply makes the statements really long and less clear.
table.column => $object->property
and
ac => $ac(WTF?!?
Statements become long when everything is put in a single line ... I've shown in my previous post here what formatting I use - I have no such reading issues.
Also, I don't like prefixing fields with the table name, although foreign keys should be prefixed. I think the "USING" keyword involves bad practices
I can't agree - the content of the tables and joining it must make sense in a SQL query, so it's not irrelevant. It will be the same if you create a SQL View and make its name one letter...arborint wrote:When you go to one letter you are almost abstracting the query to its essence and saying "here's what I am doing to tables A, B and C". The thing about SQL is that it does not really matter whether you are joining Companies and People or Cars and Parts. The content of the tables and columns is irrelevant in SQL. So think in A, B and C removes that noise.
C'mon manjshpro2 wrote:Also like Christopher said its less typing
There are 10 types of people in this world, those who understand binary and those who don't
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Code indentation... outdated?
I don't think it is a matter of agreeing. You asked why many people use table aliases. I find it is a convention that makes the SQL more readable, so do most other DBAs as you noted. I think it is similar to thinsoldier's need to have braces on their own line or he can't "can't find anything or think straight". Some people are sensitive to changes in formatting and naming. Certainly your SQL or thinsoldiers's PHP are perfectly readable by most people (except those few who have the opposite sensitivityVladSun wrote:I can't agree - the content of the tables and joining it must make sense in a SQL query, so it's not irrelevant. It will be the same if you create a SQL View and make its name one letter...
(#10850)
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Code indentation... outdated?
I'm with you on this one...but it depends a lot on personal taste I think...C'mon man Less typing doesn't worth the following much more reading and the headake.
Personally I use a phrasebook to load all SQL statements as prepared statements so verbose table names and fields are the common for me but the SQL is stored in an external INI file so the SQL statements can really be anything you can imagine and formatted nicely. When I wrote SQL statements on a single line directly passed to mysql_query or whatever...I had a tendancy to alias everything.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Code indentation... outdated?
You usually say this, but by Vlad's own admission -- most DBA's do it a way he does not understand. I think "follow your personal taste" is almost always bad advice in software development.PCSpectra wrote:...but it depends a lot on personal taste I think...
(#10850)
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Code indentation... outdated?
The point was that it is simply a personal choice...I highly dought VladSun doesn't *understand* the SQL statements...he's probably just frustrated by the brevity of single character aliases, much like I am with poorly commented code, overly complex functions, etc...arborint wrote:You usually say this, but by Vlad's own admission -- most DBA's do it a way he does not understand. I think "follow your personal taste" is almost always bad advice in software development.PCSpectra wrote:...but it depends a lot on personal taste I think...
My stance has always been DRY KISS...were not programming on machines with 5 bytes of memory so why people insist on abbreviating everything still makes no sense, except to those who write it originally...
Complete, sensible variable names will always make more sense than quick short cuts...but getting people to agree on what is a "shortcut" or logical convention is never going to happen as it's a matter of personal choice.
Cheers,
Alex
Re: Code indentation... outdated?
I didn't agree with the "added abstraction" you said about.arborint wrote:I don't think it is a matter of agreeing.
There are 10 types of people in this world, those who understand binary and those who don't
Re: Code indentation... outdated?
Real world example:
My collegue's query:
[sql]SELECTc.id,c.call_id,c.caller,sp_report_call_contact_get(c.id) AS caller_contact,c.called,(SELECT q.name FROM queue q WHERE id = (SELECT qcs.fk_queue_id FROM call_segment cs INNER JOIN queue_call_segment qcs ON qcs.fk_call_segment_id = cs.id WHERE cs.fk_call_id = c.id ORDER BY cs.start_time ASC LIMIT 1) ) AS first_queue_name,cv.start_time,cv.total_duration,cv.times_directed_to_queue,cv.times_waited_on_queue,cv.times_offered_to_agent,cv.times_connected_to_agent,cv.total_queue_wait_duration,cv.total_agent_offer_duration,date_trunc('second', cv.avg_agent_offer_duration),cv.max_agent_offer_duration,cv.total_agent_talk_duration,cv.total_hold_durationFROMcall c INNER JOIN inbound_call_view cv ON cv.fk_call_id = c.idWHEREc.id IN (SELECT DISTINCT csii.fk_call_id FROM call_segment csii WHERE csii.start_time BETWEEN _from_timestamp AND _to_timestamp);[/sql]
My query:
[sql]SELECT agent.id::bigint, web_user.fullname, aux_reason.code::integer, aux_reason.id::bigint, avg(agent_aux_reason_per_day_view.aux_count)::integer, date_trunc('second', avg(agent_aux_reason_per_day_view.aux_time))::interval FROM agent_aux_reason_per_day_view INNER JOIN agent ON agent.id = agent_aux_reason_per_day_view.agent_id INNER JOIN web_user_agent ON web_user_agent.fk_agent_id = agent.id INNER JOIN web_user ON web_user.id = web_user_agent.fk_user_id INNER JOIN aux_reason ON aux_reason.id = agent_aux_reason_per_day_view.fk_reason_id WHERE agent_aux_reason_per_day_view.day BETWEEN _start_time AND _end_time GROUP BY agent.id, web_user.fullname, aux_reason.code, aux_reason.id;[/sql]
Which one is clearer?
My collegue's query:
[sql]SELECTc.id,c.call_id,c.caller,sp_report_call_contact_get(c.id) AS caller_contact,c.called,(SELECT q.name FROM queue q WHERE id = (SELECT qcs.fk_queue_id FROM call_segment cs INNER JOIN queue_call_segment qcs ON qcs.fk_call_segment_id = cs.id WHERE cs.fk_call_id = c.id ORDER BY cs.start_time ASC LIMIT 1) ) AS first_queue_name,cv.start_time,cv.total_duration,cv.times_directed_to_queue,cv.times_waited_on_queue,cv.times_offered_to_agent,cv.times_connected_to_agent,cv.total_queue_wait_duration,cv.total_agent_offer_duration,date_trunc('second', cv.avg_agent_offer_duration),cv.max_agent_offer_duration,cv.total_agent_talk_duration,cv.total_hold_durationFROMcall c INNER JOIN inbound_call_view cv ON cv.fk_call_id = c.idWHEREc.id IN (SELECT DISTINCT csii.fk_call_id FROM call_segment csii WHERE csii.start_time BETWEEN _from_timestamp AND _to_timestamp);[/sql]
My query:
[sql]SELECT agent.id::bigint, web_user.fullname, aux_reason.code::integer, aux_reason.id::bigint, avg(agent_aux_reason_per_day_view.aux_count)::integer, date_trunc('second', avg(agent_aux_reason_per_day_view.aux_time))::interval FROM agent_aux_reason_per_day_view INNER JOIN agent ON agent.id = agent_aux_reason_per_day_view.agent_id INNER JOIN web_user_agent ON web_user_agent.fk_agent_id = agent.id INNER JOIN web_user ON web_user.id = web_user_agent.fk_user_id INNER JOIN aux_reason ON aux_reason.id = agent_aux_reason_per_day_view.fk_reason_id WHERE agent_aux_reason_per_day_view.day BETWEEN _start_time AND _end_time GROUP BY agent.id, web_user.fullname, aux_reason.code, aux_reason.id;[/sql]
Which one is clearer?
Last edited by VladSun on Thu Sep 09, 2010 4:44 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Code indentation... outdated?
I don't think there is any disputing which is clearer to the virgin developer...if I just entered the codebase I would much prefer to see the latter as it's more informative and obvious as to what is going on...the former is just bad practice...
Like using variable names like:
Cheers,
Alex
Like using variable names like:
Code: Select all
$tmp or $cntAlex
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Code indentation... outdated?
I am a firm believer that if I followed my personal choices I would be a much worse developer than I am.PCSpectra wrote:The point was that it is simply a personal choice...
And the point of being clearer to someone who knows nothing is?PCSpectra wrote:I don't think there is any disputing which is clearer to the virgin developer...
(#10850)
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Code indentation... outdated?
I firmly believe you...but if everyone thought they should follow someone else's lead...the world would never move forward it would just move in circles.I am a firm believer that if I followed my personal choices I would be a much worse developer than I am.
By virgin developer I meant someone just new to the project...not nessecarily programming in general.And the point of being clearer to someone who knows nothing is?
Obviously either example would be gobbly gook to someone who didn't understood SQL in the slightest.