4 ms·
> As soon as I'm looking at more than one programming or > markup language in the same file, I'm looking at > spaghetti code. By this definition the follow
by pyre 15y ago
> As soon as I'm looking at more than one programming or
> markup language in the same file, I'm looking at
> spaghetti code.
By this definition the following is 'spaghetti code':
$dbh->do("delete from my_table");
Following his logic:
1. Realize there's SQL and Perl in the same line.
2. STOP EVERYTHING
3. Start attempting to fully abstract SQL into Perl
so that no one will ever need to write a line of
SQL again.
4. Die an old man, never having accomplished this task.
- notaddicted 15y agoI've actually seriously considered keeping all my SQL queries in their own files, and reading them into local variables on load. It would allow them to be tracked and edited individually... but I now use an ORM.
- pyre 15y agoThe point being that ORMs have limitations. Advocating that something is 'one-size-fits-all' is foolish (not claiming that that's what you're doing).
- einhverfr 15y agoI do keep the SQL queries in their own files. The only difference is that they get loaded into the db (as stored procs) at first and then called by name. Works well.
- draegtun 15y agoThis is called the Phrasebook pattern and was probably being pushed as best practise until ORM's took hold. * http://ootips.org/yonat/patterns/phrasebook.html http://ootips.org/yonat/patterns/phrasebook.html * http://www.perl.com/pub/2002/10/22/phrasebook.html http://www.perl.com/pub/2002/10/22/phrasebook.html * https://metacpan.org/module/Data::Phrasebook::SQL https://metacpan.org/module/Data::Phrasebook::SQL
- ExpiredLink 15y ago> .. but I now use an ORM. "ORM is an anti-pattern": http://seldo.com/weblog/2011/08/11/orm_is_an_antipattern http://seldo.com/weblog/2011/08/11/orm_is_an_antipattern
- einhverfr 15y agoAgreed ORM's are usually very sub-optimal. They make sense only because application developers aren't db people. And this is the problem. I suspect for certain parts of some apps, ORM's might work OK. But in general for applications of any complexity they will break down hard and fast, and lead to bad db design, bad performance, or both.
- MatthewPhillips 15y agoORM is just less work. If you write in OOP you are already creating objects anyways. Then writing get, save, and update procedures is just a lot of extra work.
- einhverfr 15y agoThe problem is then you build databases around your ORM but if you are doing a single app database, why use an RDBMS at all? In other words, I see an ORM as an antipattern because the subset of cases where it works really well at bridging the gap you probably don't really want an RDBMS in there anyway. And if you do make it work (encapsulating your db behind updatable views) without running into this, it's a heck of a lot more work than hand-coding SQL.....
- einhverfr 15y agoI don't consider that spaghetti code, but it does have problems. The big one is the tendency to assemble queries by string interpolation.... What we've done in LedgerSMB is quite simple. All db statements in new code (outside the mapper functions) are in stored procedures. Everything is mapped in there using a Perl API so in the new code (post-1.2) otherwise all SQL is in one set of files and all Perl is in another, with minimal overlap. This sort of thing makes for very clean development. No, spaghetti code is making me have 15 files open in VIM trying to figure out which of them has the function that is being called in it, with several candidates all bearing the same function name (oh, the horrors).
- postfuturist 15y agoIn small doses, you can get things done quickly like this. However, it builds up over time into a ball of spaghetti, so yeah, maybe after the 5th or 6th blob of inline SQL, you might think about using an abstraction while you still can. I've worked (and work) on old projects with hundreds of SQL queries spread throughout the code, it _is_ spaghetti code and there are better ways to do it.
- pyre 15y ago> I've worked (and work) on old projects with hundreds > of SQL queries spread throughout the code, So have/do I. The fact that there is SQL inlined with the code is probably the least of your worries when talking about such a large/old code base.
- postfuturist 15y agoActually, the bits of SQL everywhere is the #1 problem with the code. We spent weeks just auditing them all to remove SQL injection vulnerabilities. All the SQL are fragile bits of code, difficult to write meaningful unit tests for, since you can't separate database interaction from logic, etc.
- einhverfr 15y agoBeen there too, which is why I like stored procedures now (testability, clean code, predictability).
- einhverfr 15y agoThe issue with inline SQL queries isn't the fact that SQL is mixed in with other programming languages itself. It's the propensity to write BAD SQL that's inlined because now you can apply the full power of the programming language to generate your SQL. Stored procedures are a great way to abstract this out and force good SQL coding practices btw.
- ars 15y ago
- draegtun 15y agoInstead of points 3 & 4 you could replace it with... 3. Load SQL module from CPAN. Something like SQL::Abstract (https://metacpan.org/module/SQL::Abstract https://metacpan.org/module/SQL::Abstract) or Fey (https://metacpan.org/module/Fey https://metacpan.org/module/Fey) would slot in easy for most tasks like this: use SQL::Abstract; my $sql = SQL::Abstract->new; $dbh->do( $sql->delete("my_table") );