Sql Formatting standards

I'm looking for some general guidance on Sql formatting, in PL-SQL, views, etc.

Here in our Nazi-driven shop, the standards are, shall we say, "exacting". I'd like something in between - standardized, readable, but not to the point of straightjackets.

One of my primary arguments for compact code is that IT SAVES TIME from scrolling up and down ALL DAY LONG. PLUS - it increases comprehension, productivity, etc. and MOST OF TIME TIME you're not really focusing on the columns, you're focusing on the logic (WHERE ...)

So I'm looking for standards but also the *reason* for the standards.

In our shop it's one line for SELECT, FROM, WHERE, ORDER BY, etc. That drives me nuts, because it's unnecessary, in my (not so) humble opinion. I can't see any good reason for it. When you're looking at code ALL DAY LONG, it doesn't help to put the keywords ON THEIR OWN LINE. The eye/brain is quite capable of processing these words which are ALWAYS ON THE LEFT and then if your other code is reasonably formatted, you save time and aggravation.

Also, I find it unnecessary to put EVERY COLUMN in the select on its own line, again because it takes up space and causes unnecessary scrolling up and down, and loss of "context" of the code.

Also, on simple case statements, it seems reasonable to put on one line, e.g.,
case when ac.Embark_Dt is not null then ac.Embark_Dt else ay.start_date end as Embark_Dt,

Any developer beyond the newbie stage can read this without much difficulty at all.

Also, thoughts on UPPERCASE vs lower, mixed case. Again, I fail to see why keywords in UPPERCASE help all that much. With tools like PL-SQL Developer, they're highlighted anyway, plus anyone past the newbie stage KNOWS WHATS GOING ON ANYWAY.

I think Right-Align right-align is goofy too, although I do like alignment. for example,
SELECT last_name, first_name
FROM employee
WHERE department_id = 15
AND hire_date < SYSDATE;

I think is better as left-aligned
SELECT last_name, first_name
FROM employee
WHERE department_id = 15
AND hire_date < SYSDATE;

minor difference but to me your brain skips by those words anyway, they don't need to be thrown in your face as you're reading the logic.

Here's some compact code which to me is better, because MOST OF THE TIME you're really not scanning the columns, you're looking at the WHERE logic. So, to me, it's a WASTE OF TIME scrolling up and down all day long, plus you miss "seeing the picture" because of the incessant scrolling.

COMPACT CODE
select owner c1, object_type c3, object_name c2
from dba_objects
where status != 'VALID'
order by owner, object_type;

much better than the Ask Tom long, drawn out code
select
owner c1,
object_type c3,
object_name c2
from
dba_objects
where
status != 'VALID'
order by
owner,
object_type;

if you have 20+ columns, you're wasting a lot of space.

(rant over)
 
Last edited:
It sounds like, regardless of whether your company will agree with some of your opinions, you might be someone who could benefit greatly from a larger monitor and/or a different font.

My main monitor is 1600x1200 and I hate having to work remotely on a smaller monitor. I specifically kept a non-widescreen monitor because of the reduced scrolling afforded by the extra height.

I have tried a large number of editor fonts and they vary in the amount of vertical whitespace they introduce. I settled on DejaVu Sans Mono, but I used Lucida Console for a long time which is tighter vertically. Google "programming fonts" and see if you can find something that makes you happier.
 
Worker, ok that's a good point, but I'm working on two decent size monitors at 1920x1080 and then 1680x1050.

>>I hate having to work remotely on a smaller monitor.
- I agree, when I remote in from home, my monitors are even bigger, Windows 10 does a great job of resolving sizes and such.

I don't have an issue with programming fonts.

thanks for the reply, except you didn't address my question. My monitors and font are fine, but if you have any comments on my question, that would be great.
 
I agree with Worker's point that having a tall monitor (portrait mode) at high resolution helps reduce the need to scroll. I'm lucky enough to have 2 monitors set at 1200 x 1920, which I love for viewing code.

To the OP's point, I'll respectfully disagree that scrolling is the primary consideration for the speed at which you can evaluate and edit code. Instead, I'd say everyone's got their own preferences. I prefer stacking once the statement reaches a certain size where my eyes can't parse them in one gulp.

I've got 30+ years as a professional code monkey so I think I've passed the newbie stage. :-)

In a SELECT, I'll put multiple fields on the same line if there's fewer than, say 4 fields. More than that and I'll stack them so my eyes don't have to scan back and forth so much.

In a CASE statement, I don't like multiple THEN clauses on the same line. Personally on a CASE statement, I'd like to see THEN always appear on the next line after the WHEN.

But you're basically preaching a religion, where everyone's got their specific needs and not everyone will agree. When I have to evaluate a co-worker's procedure, I'll frequently run the PL/SQL Developer Beautifier to format for my needs, at least until I understand the flow of the code. When I need to make changes, I'll revert to the original file and make my changes. BUT if I'm going to become the new "owner" (because the previous developer left the company), I'll reformat, make my changes and save to the code repository with my preferred format.

I hope this helps.

Stew
 
Stew,

I tried the portrait monitor and just found it a bit cumbersome (and not ergonomically good for my neck . . . ). But I do think that suggestions validates my point somewhat about seeing code in context.

>>I'll respectfully disagree that scrolling is the primary consideration for the speed at which you can evaluate and edit code.
- I agree with a portrait style monitor. Plus I just don't see why stacking (especially of columns) is so hard to read. That's how books are made !

>>In a CASE statement, I don't like multiple THEN clauses on the same line.
- totally agree on that! But with THEN I don't see an issue.

Thanks !

 
Back
Top