Forum Topic: php: sloppy coding...?

(286 views • 16 replies)

This topic is 1 page long.

<< < > >>
None

kernalphage

Reply To Post Reply & Quote

Posted at: 6/24/08 07:35 PM

kernalphage NEUTRAL LEVEL 10

Sign-Up: 01/02/07

Posts: 2,332

okay, so... from a couple books, a couple tutorials, i threw together a rudimentary "picture viewer" system.
its... Meh, to say the least... but a big achievement over a couple days ago when i didn't realize that wamp sets the passwords to '' ie: nothing.

but the problem is not the server side, nor the shine on the outside, but during editing.
for some reason, they said to stop the PHP code for a minute to run full HTML, instead of echoing it all out line-by-line.
so... by the end, i end up with something like:

   <?php
    	}
    	else
        {
    ?>
       	<h1>Error.</h1> 
        <p>[*BZZT*]The comic you requested does NOT exist. try again later.[*cliCKT*]</p>
    <?php
    }
    ?>

running all throughout the page.

it looks all fine and dandy (with or without a ?comic=, which was the general idea)... but when i'm in "design" mode, it shows EVERYTHING. like the picture below.

what do you think would be a good way to clean up the code? reference a template .html or .txt file? keep adding to one variable and echo (print?) it out? I'm still dangerously new to this stuff, so no 'beta testing' yet.

ooh, 'nother biggie... php is server-side, correct? so... i don't have to worry about putting a password in the mySQL_connect()?

php: sloppy coding...?

my art.,any requests?Art tech support!
pm me if i should start a webcomic. 10+ people think so!

BBS Signature

None

akasquid

Reply To Post Reply & Quote

Posted at: 6/24/08 07:37 PM

akasquid NEUTRAL LEVEL 02

Sign-Up: 05/07/08

Posts: 260

Don't bother with design view? It's a heap of shit and renders stuff totally different to how a browser will. Publish and test, so much easier.


None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 6/24/08 08:26 PM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,008

Yeah, you're totally safe with putting your password in the mySQL_connect function. Noone will see it unless they get access to your FTP.

BBS Signature

None

plasmaz

Reply To Post Reply & Quote

Posted at: 6/24/08 08:28 PM

plasmaz LIGHT LEVEL 09

Sign-Up: 02/13/07

Posts: 37

Try to use a templating system (such as smart). When coding systems that way it can become very difficult to maintain and read if the system become very large.

This way you can separate your code, from your html.

- Michael McMullen
Wii Messages: http://www.wii-messages.com

BBS Signature

None

BoneIdol

Reply To Post Reply & Quote

Posted at: 6/25/08 02:16 AM

BoneIdol NEUTRAL LEVEL 05

Sign-Up: 08/14/06

Posts: 819

Seriously, use a framework that supports MVC. MVC is pretty much all about keeping code away from presentation.

Some good ones are CakePHP and Code Igniter. Code Igniter is the simpler of the two but CakePHP, if learned properly, will speed up your development a lot more. I would suggest buying a book on CakePHP, if you go for it; the freely available documentation is horrible.

Sufficiently advanced incompetence is indistinguishable from malice.


None

henke37

Reply To Post Reply & Quote

Posted at: 6/25/08 06:28 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,674

Actually, no MVC, is not about that at all. It is about 3 objects, the view, the model and the controller. Go read "head first designpatterns", they explain it much better than I can.

And I honestly believe that template systems and frameworks is unneeded cruff.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

BoneIdol

Reply To Post Reply & Quote

Posted at: 6/25/08 06:49 AM

BoneIdol NEUTRAL LEVEL 05

Sign-Up: 08/14/06

Posts: 819

At 6/25/08 06:28 AM, henke37 wrote: Actually, no MVC, is not about that at all. It is about 3 objects, the view, the model and the controller. Go read "head first designpatterns", they explain it much better than I can.

Which, traditionally, are used to split up the user interface, business logic (and/or actual data depending on application) and controlling code, am I right? And could, be applied to the web by splitting up the html (user interface), the database (although this isn't as necessary) and the actual logic in the page?

While your right in that it isn't necessarily about seperating presentation from content (html and css do that) it certainly helps in seperating php from html. My choice of words was based on a common mantra among people who think that mixing php and html is the greatest sacrilidge one can commit.

In all honesty though, I'm getting increasingly cynical about the concept of clean PHP code; it's so much hard work. With other languages clean code seems to come naturally after using them for a while, whereas with PHP it's such an uphill bloody struggle. Frameworks do help a LOT though.

Sufficiently advanced incompetence is indistinguishable from malice.


None

different

Reply To Post Reply & Quote

Posted at: 6/25/08 04:39 PM

different DARK LEVEL 35

Sign-Up: 07/08/04

Posts: 3,763

PHP was never designed to be a clean framework. It's only in it's more recent incarnations that it has gained object orientated programming, which is optional. It's designed for building web applications from the ground up very quickly - which is why it can be placed in and around HTML. It's a powerful language - but clean? Clean is overrated. :-)

we play iPhone, daily game reviews, twitter.


None

elbekko

Reply To Post Reply & Quote

Posted at: 6/25/08 04:42 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,587

If you keep your code properly structured, it's no problem at all.

And I absolutely detest templating systems. They're the bad, bad, evil, devil and should die.

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

None

BoneIdol

Reply To Post Reply & Quote

Posted at: 6/26/08 05:02 AM

BoneIdol NEUTRAL LEVEL 05

Sign-Up: 08/14/06

Posts: 819

At 6/25/08 04:39 PM, different wrote: PHP was never designed to be a clean framework.

I often wonder if PHP was designed at all. I'm not often so confrontational but pretty much all you said is bollocks. Clean code = more maintainable code. Who the fuck wants to rewrite a whole site whenever a simple change is required?

I am sick and tired of fixing other peoples' shitty, horribly code. Most of the people who write it will, when pressed about it, chuckle and go "oh that's PHP for you; it's not designed to be clean". Yeah? Well why the fuck not? Perhaps you just can't be fucked to learn how to write clean code? Which brings me onto elbekko's post.

The problem (most of the time) isn't that people can't be fucked to write clean, structured code, the problem is that everyone has different ideas of what constitutes it. I know people who will make websites that include 15 files just to make a simple bloody homepage, who insist that is "properly structured". While breaking your code into different modules is nice, there is doing it properly and there is taking the piss.

I know people who will keep all their POST handling in one file (with one huge switch statement, often with really stupid fall throughs), even when it's 500 lines long. And you know what? They insist that's "properly structured". too.

I could give you dozens of examples of such cock ups in "design", (I quote this because very little actual programming design goes into PHP projects, as they're usually commisioned to the cheapest company and the client wants it yesterday) but I'm afraid you'll drift into a coma.

The thing is I'm no exception to this either; I've written some god-awful code in my time. Often in an effort to try and achieve cleaner code, ironically. Any programmer who says that: a) they've never written shit code and b) they're current coding style is perfect is talking out of their arse.

Which is why I think this "properly structured" thing is utter nonsense. Everyone has their own way of getting things done. Which is why I advocate more people learn to use frameworks. With a framework there's a certain way to do things. It makes it so much easier to debug other peoples' code and have more than one person working on a project.

So to summarise:

Dear world;

Stop writiing such shit PHP code. Someone has to look after it when you're done with it, and it's often me. You may wish to take up a framework should you have problems with this simple request.

Yours faithfully:

BoneIdol

I feel oddly better for that. Sorry about the rant everyone, touchy subject for me.

Sufficiently advanced incompetence is indistinguishable from malice.


None

elbekko

Reply To Post Reply & Quote

Posted at: 6/26/08 06:55 AM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,587

Frameworks *always* add extra bloat. Bloat is to be avoided.

By the way, BoneIdol, I feel the same way. But there are a few ways code can be structured very cleanly. If you want a good example, look at FluxBB's code.
I've written some damn ugly code too in my time, but I've learned to do so much better now. And I won't say it's perfect, but I will say it's pretty damn close - since I haven't found a single person that doesn't understand the flow of code I write (unless they can't program or think logically ofcourse).
The main point of it all if that indentation is important, and so is block notation.

I could go on and on about readable code, but it'll stay a personal thing. Ofcourse, you should try to make source code as readable as possible, especially if you know you won't be the one maintaining it.

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

None

different

Reply To Post Reply & Quote

Posted at: 6/26/08 02:21 PM

different DARK LEVEL 35

Sign-Up: 07/08/04

Posts: 3,763

At 6/26/08 05:02 AM, BoneIdol wrote: Most of the people who write it will, when pressed about it, chuckle and go "oh that's PHP for you; it's not designed to be clean". Yeah? Well why the fuck not? Perhaps you just can't be fucked to learn how to write clean code? Which brings me onto elbekko's post.

You're misinterpretating my point; which is that PHP, can encourage sloppy code because it is not an objective orientated language, and can be weaved in and around HTML. Of course, the OO features introduced in PHP4 and 5 show that PHP can actually be a very clean language.

The problem (most of the time) isn't that people can't be fucked to write clean, structured code, the problem is that everyone has different ideas of what constitutes it. I know people who will make websites that include 15 files just to make a simple bloody homepage, who insist that is "properly structured". While breaking your code into different modules is nice, there is doing it properly and there is taking the piss.

And er.. this is not PHP's fault. If it makes sense to me to have 15 files for my homepage, that's what I'll do. Which would you prefer - something that worked (did the job) and took a couple of hours to build, or something that took six months? It's like people who seperate CSS into say 5 files - why? Because one huge CSS file can be intimidating to other people who need to change something and haven't gotten any prior experience fixing the code.

I know people who will keep all their POST handling in one file (with one huge switch statement, often with really stupid fall throughs), even when it's 500 lines long. And you know what? They insist that's "properly structured". too.

Again, if it works, then what's the problem? If it's properly structured for them, then it's fine. It doesn't matter whether it's written in 2 lines or 200. If it works, it works.

The thing is I'm no exception to this either; I've written some god-awful code in my time. Often in an effort to try and achieve cleaner code, ironically. Any programmer who says that: a) they've never written shit code and b) they're current coding style is perfect is talking out of their arse.

Who cares? We're all human and we're all imperfect. Godawful* code is GOOD if it WORKS. Even if you think it's bad, sales/marketing may well love it, it may be another Facebook and everyone will love it, regardless of all that bloat.

* in the sense of it being bloated/slow/convoluted, not in the sense of being insecure, buggy etc.

we play iPhone, daily game reviews, twitter.


None

elbekko

Reply To Post Reply & Quote

Posted at: 6/26/08 07:59 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,587

I just threw up a little.

Seriously, if that's how you think, stop programming NOW. "If it works, it's good" is far from a good point. Very far.

And OO has nothing to do with clean code. As a matter of fact, especially in the PHP implementation, it can load to sloppy code more often than not.

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

None

BoneIdol

Reply To Post Reply & Quote

Posted at: 6/27/08 05:27 AM

BoneIdol NEUTRAL LEVEL 05

Sign-Up: 08/14/06

Posts: 819

At 6/26/08 07:59 PM, elbekko wrote: I just threw up a little.

Seriously, if that's how you think, stop programming NOW. "If it works, it's good" is far from a good point. Very far.

I agree completely with this point. We're software engineers, when was the last time you ever heard an engineer go "meh, it'll do"? Shitty software can be just as dangerous as a shitty building design (particularly to peoples' bank accounts).

Such approaches work for someone's little homepage or something, but when you do things like software to manage a franchise, for example, this approach leads to hours and hours of grinding misery.

No code should be written as if it's going to be thrown away later. Weaving PHP in big blocks of html is a good way to make it very hard to re-use later. I do not want to spend the rest of my life writing out PHP code to add and edit news posts and I'm sure you don't either.

To be honest though, I've completely lost the necessary bile to play the devil's advocate on this topic. I think my frustration was aimed more at the kind of organisations that tend to use PHP more than the language itself.

On the whole, I'd say PHP isn't necessarily a bad language, it just seems to attract so many people who couldn't give a toss about their job properly. I'm reliably informed that Java has this problem as well. This is probably why all the current fad languages like ruby are paying so much more than PHP; because they attract all the real programmer/hacker types (ooh hey look! New toy!).

However, I feel I should point out a few technical issues with PHP that do get on my tits:
- May as well get this out of the way first; function naming and arguement order are about as consistent as your average politician. Everyone whinges about this though.
- Every single function is in the global namespace, which gives you wonderful opportunities to shoot yourself in the foot by using a function name already in use.
- Exceptions in PHP are a joke.
- The bastardardised version of OOP that passes for classes in PHP. To be fair, they are getting better.
- The environment contains abominations like safe_mode.
- The environment has literrally no consistency. I regularly have to fix code whenever a server is updated because it depends on some stupid little clause in php.ini.
- Writing extensions is such a god damn pain and there is sweet FA documentation about how to do it.
- References seldom work exactly as you'd want them to.
- There's seldom incentive to make webhosts upgrade their versions of PHP. In fact (as mentioned above) this is usually a detrimental activity for them, as everyones' websites break.
- Why the hell does the maximum size of a file upload need to be set in 4 seperate places, anyway?

I could go on, but those are just off the top of my head from working 9-5 with PHP for the past 2 years. There is one thing I absolutely cannot fault PHP on though, and that is the documentation (with exception of extensions, as mentioned above). php.net is absolutely top notch, easy to navigate, concise and filled with useful comments from other developers. Compared to the bloated behemoth that is msdn, it is an absolute godsend.

Sufficiently advanced incompetence is indistinguishable from malice.


None

henke37

Reply To Post Reply & Quote

Posted at: 6/27/08 04:09 PM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,674

What is it about the exceptions in php that you don't like? The fact that they are not checked? Php isn't alone on that.
The fact that it lacks a pointless finally clause? my comment is: Just fricking put the code after the exception handling.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

different

Reply To Post Reply & Quote

Posted at: 6/27/08 05:09 PM

different DARK LEVEL 35

Sign-Up: 07/08/04

Posts: 3,763

At 6/26/08 07:59 PM, elbekko wrote: Seriously, if that's how you think, stop programming NOW. "If it works, it's good" is far from a good point.

Of course, code is a process of continual refinement, and well crafted code is easier to maintain. It also depends on the project. But why is good enough not good enough?

I don't need a perfectly written piece of coding for a website that will only be updated twice a year. Right?

we play iPhone, daily game reviews, twitter.


None

BoneIdol

Reply To Post Reply & Quote

Posted at: 6/28/08 06:30 AM

BoneIdol NEUTRAL LEVEL 05

Sign-Up: 08/14/06

Posts: 819

At 6/27/08 04:09 PM, henke37 wrote: What is it about the exceptions in php that you don't like? The fact that they are not checked? Php isn't alone on that.
The fact that it lacks a pointless finally clause? my comment is: Just fricking put the code after the exception handling.

To tell the truth, I stumbled onto this little construct not long after writing that that has restored my faith in php exceptions:

<?php
set_error_handler(create_function('$x, $y', 'throw new Exception($y, $x);'), E_ALL & ~E_NOTICE);
?>

How cool is that? Now every PHP error now throws a propper, catchable exception. Now you don't have a try block filled with a shitload of ifs (which was what I was annoyed about). Only downside to catching EVERY PHP error though is that you can catch ones you don't want to.

Also, I'm another one that never understood the point of finally constructs; I first discovered the odd little beasts in Delphi. The description of them was something like "code that HAS to be run should go in a finally construct". Do forgive me if I'm being pedantic here, but I thought ALL my code has to run unless I tell it not to.

And different, while your right there to some degree, your example is wrong (sorry you are getting so much crap here). A project that gets updated once or twice a year really needs good code because within 6 or so months you'll have forgotten everything.

Stuff that gets updated very regularly can afford sloppy coding (even if it's an utter nightmare for people like me) because with such regular work being carried out the poor sod maintaining it will get used to all the odd quirks and poor code structure.

Sufficiently advanced incompetence is indistinguishable from malice.


All times are Eastern Standard Time (GMT -5) | Current Time: 04:16 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!