Advanced coding techniques?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Advanced coding techniques?

Post by alex.barylski »

Personally I dislike them and try and avoid them....aka shortcuts....especially when their not clearly documented/commented...they drive me nutts in other's code...

I'll give an example (ignoring bad practice of using globals)

You load an array of language codes from a INI file and store them inside $GLOBALS

ini file:

Code: Select all

HomePageCaption = "This is the english caption"
HomePageHeader = "This is the homepage header"
Now in PHP you automate the initialization of the $GLOBAL using a simple for loop now you have a series of $GLOBALS, like...

Code: Select all

$GLOBALS['HomePageCaption'] = "This is the english caption";
$GLOBALS['HomePageHeader'] = "This is the english header";
...
I encountered such a situation recently and was left looking for the initlaization of the GLOBALS for a few hours...no matter how many grep's I ran I couldn't find the spot as there was no explicit initialization of the GLOBALS (they were magically done in a for loop).

This is not intended to disscuss the finer points of KISS or good design, but rather start a disscussion on advanced coding techniques which might have negative implications in the long run.

Cheers :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think this is more a PHP Theory and Design topic Hockey.

Moved.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I see you're up to your old tricks of posting in General Discussion again Hockey.

:| We are not amused.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

No tricks man....just a difference of opinion I guess...it actually started off as more a general topic but found it's way into the realm T & D :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Advanced coding techniques?

Post by Christopher »

Hockey wrote:This is not intended to disscuss the finer points of KISS or good design, but rather start a disscussion on advanced coding techniques which might have negative implications in the long run.
I am not sure that the example you gave is "advanced" -- it sounds more like a "bad coding technique" on several levels. It uses globals and does not encapsulate related data in some kind of container that clearly identifies the contents to the programmer (e.g. $config). I think that bad coding techniques can be identified by using code smells to check for them in code.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Advanced coding techniques?

Post by alex.barylski »

arborint wrote:
Hockey wrote:This is not intended to disscuss the finer points of KISS or good design, but rather start a disscussion on advanced coding techniques which might have negative implications in the long run.
I am not sure that the example you gave is "advanced" -- it sounds more like a "bad coding technique" on several levels. It uses globals and does not encapsulate related data in some kind of container that clearly identifies the contents to the programmer (e.g. $config). I think that bad coding techniques can be identified by using code smells to check for them in code.
It's an advanced technique (relative to PHP development)...I see it quite often in programmers with a few years programming experience...

I say advanced because the less experienced I have worked with generally don't think or try to automate such initializations...instead they do it manually

Code: Select all

$GLOBALS['blah'] = "blah";
$GLOBALS['blah'] = "blah";
$GLOBALS['blah'] = "blah";
$GLOBALS['blah'] = "blah";
This is much clearer (ignoring the fact globals are being used) than an automated FOR loop which does all the magic for you...easier to find using a grep...and so on ;)

I dunno about you, but as a contract developer I work on *many* existing codebases which range from great to garbage...so I have little choice but to accept good or bad programming practices...make good with what I'm given...

I see plenty of advanced coding techniques which are generally bad practice...this is often caused by skilled developers and the mindset "If it was difficult to write it should be equally difficult to understand" :P

Consider the >> operator as an example...many C developers use advanced shift techniques to execute faster multiplication/division by powers of 2...while this did yield faster code on many platforms (now compilers take care of this for you) it also obfuscated the source slightly...

Someone not familiar with shifting tricks might take longer than nessecary in locating the culprit code...looking for a * or / operator NOT a >> or <<

The whole pupose of KISS is to make things as explicit as possible - self documenting code using high level API's and so on. Easy to understand not just for the original developer but someone coming in later (like me) when s*it hits the fan :P

Don't get me wrong, I love sifting through advanced code (assembler, C/C++, etc) but when I have a job to do and time is money I want results ASAP as time is a relfection of my performance...so KISS is the impetus to in my development...

Cheers :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Advanced coding techniques?

Post by timvw »

Hockey wrote:It's an advanced technique (relative to PHP development)...I see it quite often in programmers with a few years programming experience...
I think you have an odd definition for 'advanced technique'.
Hockey wrote: I say advanced because the less experienced I have worked with generally don't think or try to automate such initializations...instead they do it manually

Code: Select all

$GLOBALS['blah'] = "blah";
$GLOBALS['blah'] = "blah";
$GLOBALS['blah'] = "blah";
$GLOBALS['blah'] = "blah";
This is much clearer (ignoring the fact globals are being used) than an automated FOR loop which does all the magic for you...easier to find using a grep...and so on ;)
So you are saying that clearer code is less advanced? Imho, both techniques are equally 'advanced'. All they do is import variables into the GLOBAL namespace..
Hockey wrote: I see plenty of advanced coding techniques which are generally bad practice...this is often caused by skilled developers and the mindset "If it was difficult to write it should be equally difficult to understand" :P
I wouldn't consider something to be 'advanced' if it's a bad practice. I wouldn't name a developer skilled if that's the mindset he has.
Hockey wrote: Consider the >> operator as an example...many C developers use advanced shift techniques to execute faster multiplication/division by powers of 2...while this did yield faster code on many platforms (now compilers take care of this for you) it also obfuscated the source slightly...
How does it obfuscate the code? It's only a matter of understanding the >> operator (And no, i don't think that there are many c (or assembly) developers that have not learned about SHL and SHR as a means of * and / operations for base 2)
Hockey wrote: Someone not familiar with shifting tricks might take longer than nessecary in locating the culprit code...looking for a * or / operator NOT a >> or <<
But should someone not familiar with 'shifting tricks' (as if the use of >> alone is a trick) actually look into such code? I would think there are people better qualified to do that.. In case there aren't such people anymore in the organisation something has gone wrong.. but i wouldn't blame the original developer...
Hockey wrote: The whole pupose of KISS is to make things as explicit as possible - self documenting code using high level API's and so on. Easy to understand not just for the original developer but someone coming in later (like me) when s*it hits the fan :P
For me, KISS is about keeping it as simple as possible. And if that requires obfuscated code in order to get that extra bit of performance out of it, than it will that obfuscated code.
Hockey wrote: Don't get me wrong, I love sifting through advanced code (assembler, C/C++, etc) but when I have a job to do and time is money I want results ASAP as time is a relfection of my performance...so KISS is the impetus to in my development...
As an enterprise you'll have to try and predict the costs versus the benefits... How much does it cost us to document every design choice made? How much would it cost for someone to figure it out in a later stage? Is the difference significant enough to make the actual cost right now?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

My response is what timvw said. ;) :)
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

You both are clearly very experienced developers...I could learn alot from you...I'd like to see some application code (not just fibonacci number sequence algorithms in 10 languages)...but complete, well designed, KISS applications to serve as an example of a solid application design and development.

I think submitting something complete would benefit the entire community??? I think we could *all* learn more about good design from real world applications as opposed to just theory and statements from Fowler, etc???

I have worked on literally 100's of projects from C++ to PHP and I have never seen a perfect project, but I think you two might actually have everything figured out. I look forward to your wisdom and learning from your experiences :)

Download links??? Sourceforge.net?

Cheers :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Hockey wrote: I think submitting something complete would benefit the entire community??? I think we could *all* learn more about good design from real world applications as opposed to just theory and statements from Fowler, etc???
Sorry, the companies i work with/for always make me sign confidentiality clauses.. So i it's more or less impossible to share real world applications... =P
Hockey wrote: I have worked on literally 100's of projects from C++ to PHP and I have never seen a perfect project
If perfect projects existed a lot of project managers would have to look for a different job. In my experience it's their task to keep a project going in the direction it was intended to go (without derailing too much)... And i don't know a project manager that can't fill his day (and sometimes weekends) just doing that....

I think that most developers believe they've written the 'perfect' application when they finish something.. And in case they don't believe that it's perfect they're atleast aware of the design problems and know on which situations they want to spend more time the next time they implement something similar...

http://www.sourceforge.net/projects/ap3 perhaps? ;) Personally found viewtopic.php?t=43777 one of the better topics on finding (and implementing) a solution... (And no, there was and still is room for improvement in the resulting code.. but at the time it felt pretty good ;))
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I have to say that recently I have been very pleased with the currently state of PHP frameworks. Specifically, any MVC framework with a Front Controller loading Action Controller classes, especially with Request and Response objects. I notice that from project to project that the code stays very similar and things seem to fall in place more naturally than in past years in PHP and other languages. I find the response, forward, redirect style of PHP MVC to be flexible in incorporating new features yet still provides a lot of reuse from existing Action classes. It is to the point where I can just implement a new CSS stylesheet, build the wraparound layout HTML, and I am ready to get to the site specific stuff for that project. I have used the Zend Framework, Cake and my AP3 code (for PHP4) all to similar effect.

If you could define (ultimately in detail) some examples of what the minimum for a "complete application" is, perhaps we could explore some designs and implementations. I certainly be interested in doing that because I always learn from such excercises.
(#10850)
Post Reply