Click here to Skip to main content
15,887,746 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Andrew Rissing19-Oct-10 7:54
Andrew Rissing19-Oct-10 7:54 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Richard A. Dalton19-Oct-10 8:24
Richard A. Dalton19-Oct-10 8:24 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Ray Cassick19-Oct-10 12:32
Ray Cassick19-Oct-10 12:32 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Richard A. Dalton20-Oct-10 1:32
Richard A. Dalton20-Oct-10 1:32 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
YSLGuru20-Oct-10 10:18
YSLGuru20-Oct-10 10:18 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Ian Shlasko20-Oct-10 10:58
Ian Shlasko20-Oct-10 10:58 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Chris Quinn20-Oct-10 2:22
Chris Quinn20-Oct-10 2:22 
GeneralRe: Stored Procs, Packages, Views...Pah! PinPopular
Trajan McGill20-Oct-10 11:59
Trajan McGill20-Oct-10 11:59 
No, there's more to it than "poorly written anything is junk". The problem is structural in nature, not just a matter of bad coders.

The problem introduced here is actually very similar to "DLL hell". An application's domain of code-based dependencies and interactions is well mapped and tracked by modern programming tools. Typically you define a "solution" or something along those lines, with all your code together tracked in source control, and have a build process that carefully lays out what is dependent on what.

Stored procedures are an aberration from this entire model. They're dependencies that you don't build in, they sit out there somewhere outside the same domain of control and tracking, and they change. Or they don't, and they become one-use things that build up in a giant, unstructured list, violating both notions of code organization and code re-use. They're like web services, in that they are exposed for use, and then you never really know who or what is using them, and so they are really hard, compared to regular code, to be confident in changing or eliminating. All kinds of applications, services, and other database objects could have come to rely on an SP over time, but the dependencies are invisible, and it takes a lot of work to track them down, and sometimes isn't even possible.

Don't get me wrong, SP's are actually important, for performance reasons, for code safety reasons, and so on. But they definitely break the model of good code practices that we all want to follow, not breaking it just because of coders being good or bad, but because these things exist in a different arena outside the bounds of the models and practices that good programmers put in place to manage their projects.

They also make themselves frustrating in two other ways.

#1, They lend themselves to obfuscated code in a way that regular programming languages do not. SQL is designed as a query language, not a procedural, object-oriented, or business object modeling language. Things that are perfectly clear and can be documented and written very clearly in, say, C#, are very messy in SQL. This would be okay-- use each language for its own best purpose-- except...

#2, They nearly always wind up including more than just data queries. Admittedly this is partly-- but not completely-- an architectural or coder-dependent decision, but SP's tend to absorb business logic and destroy the multi-tiered design principles that you worked so hard to keep to. You wind up with a design that looks like this:

[Presentation] <--> [Business logic] <--> [Data Access] <--> [Hybrid Raw Data/Data Access/Business Logic Layer].

That's even worse than the normal, unavoidable slight overlap or crossover between adjoining layers; the business logic appears in two places that aren't even next to each other. Furthermore, now your business logic belongs half to the programmer and half to the DBA, and you lose one of the major purposes of having tiered architecture in the first place, which is the ability to, as much as possible, contain all related concerns in one tier that can be modified (largely) independently of the others.

And you also have now just removed your business logic-- arguably the most important part of the application to control carefully and track changes for-- from the application's main area of source control and issue tracking, unless you are strictly considering every stored procedure to belong to exactly one and only one application, somehow enforcing this, and doing extra work to try to keep good track of them in the same source control system (made easier with some systems than others). (And even then, you're stuck dumping the stored procedures into a giant bin, where you're likely to have naming collisions and end up with a gigantic list than nobody wants to venture into to find something.)

You're also removing from the developers the part that most logically should belong to them. They are developing applications, which means they are designing how things work, which means they are primarily existing in the business/application logic realm. For some types of applications, there are designers who do a lot of the presentation layer, and DBA's should be heavily involved in the DAL, but developers are there to make the application work right. If you put lots of business logic in the database, then you're handing off stuff that developer/analysts should be good at, to the database admin/analysts, who are supposed to be good at other things-- namely, storing and extracting data.

Of course, you can't be perfectly pure about anything in real life. But as a general rule, stored procedures should be there to return data, and they should be designed to do that well and quickly and efficiently. And yes, arguably they do need to understand business objects to the extent of being able to maintain the integrity of a data store. But if they get too mixed up in the jobs of trying to be the application, things get way too messy.
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Richard A. Dalton20-Oct-10 23:21
Richard A. Dalton20-Oct-10 23:21 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Dr.Walt Fair, PE19-Oct-10 13:01
professionalDr.Walt Fair, PE19-Oct-10 13:01 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Rob Grainger19-Oct-10 23:10
Rob Grainger19-Oct-10 23:10 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Electron Shepherd19-Oct-10 23:36
Electron Shepherd19-Oct-10 23:36 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
OriginalGriff20-Oct-10 0:10
mveOriginalGriff20-Oct-10 0:10 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Electron Shepherd20-Oct-10 0:20
Electron Shepherd20-Oct-10 0:20 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
OriginalGriff20-Oct-10 0:25
mveOriginalGriff20-Oct-10 0:25 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Electron Shepherd20-Oct-10 1:58
Electron Shepherd20-Oct-10 1:58 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
ScottM120-Oct-10 2:41
ScottM120-Oct-10 2:41 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Electron Shepherd20-Oct-10 2:42
Electron Shepherd20-Oct-10 2:42 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
ScottM120-Oct-10 3:03
ScottM120-Oct-10 3:03 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Andrew Rissing20-Oct-10 2:54
Andrew Rissing20-Oct-10 2:54 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Electron Shepherd20-Oct-10 2:55
Electron Shepherd20-Oct-10 2:55 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
ScottM120-Oct-10 3:09
ScottM120-Oct-10 3:09 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Electron Shepherd20-Oct-10 3:10
Electron Shepherd20-Oct-10 3:10 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
ScottM120-Oct-10 3:20
ScottM120-Oct-10 3:20 
GeneralRe: Stored Procs, Packages, Views...Pah! Pin
Electron Shepherd20-Oct-10 3:22
Electron Shepherd20-Oct-10 3:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.