473,569 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best Coding Practice

First, let me say that this question is a rather general programming
question, but the context is PHP, so I figured this group would have
the most relevant insight.

Anyways, this is also more of an opinion based question than one
seeking a definite answer. Recently, while maintaining a rather large
system. I needed to add an array class member to an object. It was
exactly the same as another class member, except that one array stored
regular products, and the other stored free promotional products. As
such, I needed a way to add products to the new, free array. Since all
the logic was the same between the two arrays aside from the price, I
had a few different options to do this. I'm interested in polling
which way some of the group members feel would have been the best.
Naturally these are abbreviated versions of what I envisioned as
possible solutions.
#1.
public function addArrayA($obje ct){
//logic
$a[] = $object;
}

public function addArrayB($obje ct){
//same logic
$b[] = $object;
}
#2. (These next two are arranged as such, because the class using
these functions is included in many script files,
all of which I may not be aware of, so there would have to be some
default value for the array that was always used
before this new array was needed)
public function addArray($objec t, $free = NULL){
//logic
if(!$free){
$a[] = $object;
}else{
$b[] = $object;
}
}

or

#3
public function addArray($objec t, $arr = "a"){
//logic
$$arr[] = $object;
}

I ended up going with option number 1, because I felt that despite the
inefficient, redundant code it would later be more readable to other
programmers that might work on the project. Additionally, I didn't
feel wholly comfortable with default variables being the only
difference between a full price product and a free product. Thoughts?

Aug 24 '07
52 3340
bu************* @gmail.com wrote:
I needed to add an array class member to an object. It was exactly the
same as another class member, except that one array stored regular
products, and the other stored free promotional products. As such, I
needed a way to add products to the new, free array. Since all the logic
was the same between the two arrays aside from the price, I had a few
different options to do this.
Frankly I think all the options you outlined expose too much of the inner
workings of your class. What happens when you add a third category of
products (products that cost money, free products & products we have to
pay you to take away!)

A better solution would be something like this:

public function add_product (Product $p)
{
if ($p->price==0)
$this->free_product s[] = $p;
else
$this->products[] = $p;
}

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 64 days, 11:44.]

TrivialEncoder/0.2
http://tobyinkster.co.uk/blog/2007/0...ivial-encoder/
Aug 24 '07 #11
On Aug 24, 12:59 am, Sanders Kaufman <bu...@kaufman. netwrote:
rf wrote:
"Sanders Kaufman" <bu...@kaufman. netwrote in message
Oh, I get it. It's a politically-correct, bidness-safe way of saying you
did something fundamentally wrong and have to re-design the whole damned
thing.
No it is most definately not. If you had read and understood the above
article you would know that.
While you are looking again at the article you may wish to follow the link
to Extreme Programming, of which refactoring is an integral component.
This is not, of course, programming 101. It's many levels above that.

Yeah - so advanced that it leaves the programming world entirely, and
enters that of PC bidness-speak. Bleccchh.

I'm a code-monkey, and I like it.
Sanders, I respect where you are coming from as wanting to be a "code-
monkey" and remove yourself from the "PC bidness-speak", as I've been
that way (and continue to be that way to some extent). There is a big
difference, however, in adopting sound best practices in software
engineering and "running with the crowd", as it were, using terms we
all love to hate like Web 2.0 and the like.

Refactoring your code doesn't always mean you screwed up in the design
or within the actual logic of the code. Most of the time, it has to
deal with improving your code to meet new business requirements. A
good (and dramatic) example is if you started with a very small
project that used OO, but not an MVC or similar framework. Later,
your customer tells you that their business has expanded and their
presence on the Internet must grow accordingly and now the customer is
needing something that is well beyond the original specifications of
the web application you created. Then starts the refactoring
process. Refactoring could also deal not just with your business
logic, but also database schema and aspects therein.

I recommend to all that hasn't already read them, to check out books
by Martin Fowler as a starter into this subject.

The term "refactorin g" can be considered a label, really, similar to
giving a design pattern a name. It gives technical people a common
language so they can work together. Just because your PHB may pickup
on the term, and use it (probably incorrectly), doesn't mean it's
garbage and does not relate to a software engineer.

Extreme programming uses the term refactoring, but even though it may
be in the index of the book, doesn't mean they are strictly related.
Aug 24 '07 #12

| Agreed, please see my post above. I'll be sure to focus on the
| problem rather than the approach of the person in the future. Thanks
| for pointing this out, Jerry.

you always have to consider BOTH. that 'person in the future' is probably
going to be you. either way, it is the structure and pattern of what you
code that makes a project manageable and expandable. just looking at the
current problem is amatuer practice at best. that's called putting out fires
and is too short-sighted to do anyone much good.
Aug 24 '07 #13
ELINTPimp wrote:
>
Refactoring your code doesn't always mean you screwed up in the design
or within the actual logic of the code. Most of the time, it has to
deal with improving your code to meet new business requirements.
I understand all that... and how "refactor" doesn't necessarily mean
that everything's a mess.

In fact, I just "refactored " some code in which I was passing CSV
strings, but needed to change that to an array.

But I just told my payer that I did something fundamentally wrong, and
had to fix it before I went on. Had I said I need to "refactor" my code
, it wouldn't have been clear to her what happened. Worse, I think it
would have scared her into thinking that I was another one of many
coders she's hired before who bend over backwards to avoid admitting
that they made a mistake... while charging her for it.

I realize that in some circles, that kind of biz-speak is acceptable -
but those aren't my circles.

Aug 24 '07 #14
Sanders Kaufman wrote:
ELINTPimp wrote:
>>
Refactoring your code doesn't always mean you screwed up in the design
or within the actual logic of the code. Most of the time, it has to
deal with improving your code to meet new business requirements.

I understand all that... and how "refactor" doesn't necessarily mean
that everything's a mess.

In fact, I just "refactored " some code in which I was passing CSV
strings, but needed to change that to an array.

But I just told my payer that I did something fundamentally wrong, and
had to fix it before I went on. Had I said I need to "refactor" my code
, it wouldn't have been clear to her what happened. Worse, I think it
would have scared her into thinking that I was another one of many
coders she's hired before who bend over backwards to avoid admitting
that they made a mistake... while charging her for it.

I realize that in some circles, that kind of biz-speak is acceptable -
but those aren't my circles.
But that's not refactoring. You changed the interface. Refactoring
does not change the interface - just the internal workings.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Aug 25 '07 #15
Jerry Stuckle wrote:
But that's not refactoring. You changed the interface. Refactoring
does not change the interface - just the internal workings.

What happened was that I had that Database->Baseclass->Implementati on
structure going on, but I oopsed and put some features in the baseclass
that belonged in the database class.

Since the whole project relied on that wrong way of doing things, I had
to go to every page that extended the baseclass and modify it to reflect
the array way instead of the csv way.

That's refactoring, right?

btw - I just finished fixing up a working implementation and it's at
"http://www.kaufman.net/bvckvs/bvckvs_publicat ion.php".

Except for a lot of cosmetology to do, I think it's what I wanted.
Aug 25 '07 #16
Sanders Kaufman wrote:
Jerry Stuckle wrote:
>But that's not refactoring. You changed the interface. Refactoring
does not change the interface - just the internal workings.


What happened was that I had that Database->Baseclass->Implementati on
structure going on, but I oopsed and put some features in the baseclass
that belonged in the database class.

Since the whole project relied on that wrong way of doing things, I had
to go to every page that extended the baseclass and modify it to reflect
the array way instead of the csv way.

That's refactoring, right?

btw - I just finished fixing up a working implementation and it's at
"http://www.kaufman.net/bvckvs/bvckvs_publicat ion.php".

Except for a lot of cosmetology to do, I think it's what I wanted.
No, refactoring is changing the implementation without changing the
interface.

IOW, you change HOW you do things, but not WHAT you do. You changed the
interface.

An example of refactoring would be to change a class so that it gets its
data from a relational database instead of a flat file. The function
calls (interface) remain the same, but the code in the functions
(implementation ) changes.

Refactoring in OO would mean you would not have to change anything
outside of the class itself.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Aug 25 '07 #17
Jerry Stuckle wrote:
No, refactoring is changing the implementation without changing the
interface.

IOW, you change HOW you do things, but not WHAT you do. You changed the
interface.

An example of refactoring would be to change a class so that it gets its
data from a relational database instead of a flat file. The function
calls (interface) remain the same, but the code in the functions
(implementation ) changes.

Refactoring in OO would mean you would not have to change anything
outside of the class itself.
Ahh - so when I had to use new kinds of parameters - I was rewriting.
But if I'd made it all zero-impact on how it's used, it would have been
refactoring.

I don't get it. I can repeat it and rephrase it. But I don't get it.

Aug 25 '07 #18
No, refactoring is changing the implementation without changing the
interface.
Please read the book before you utter nonsense.

Refactoring is changing the _structure_ of the code without changing the
_behaviour_ of that code. So interface changes can be refactorings.

<snipped more incorrect stuff>
Refactoring in OO would mean you would not have to change anything
outside of the class itself.
I think you confuse with the Open Closed Principle. The Open Closed
Principle and refactoring are perpendicular ways to adapt code in a
_controlled_ fashion: The Open Closed Principle leaves the structure
intact, while refactoring makes sure the code behaviour remains the same
while you are restructuring it.
Aug 25 '07 #19
Dikkie Dik wrote:
>No, refactoring is changing the implementation without changing the
interface.

Please read the book before you utter nonsense.

Refactoring is changing the _structure_ of the code without changing the
_behaviour_ of that code. So interface changes can be refactorings.
I have read the book - many times. And I've been involved in
"refactorin g" since long before the term ever came up. The interface IS
the behavior - and changing the interface changes the behavior.

Refactoring means you don't have to chance code outside that which you
are changing. For instance, you can change the body of the function
without changing the function name, parameter list and return value.
This requires no change outside of the function, and is refactoring.

But if you change the function name, parameter list and/or return value,
you have to change all of the code calling it. This is NOT refactoring.

Pull your head out of your ass and learn what you're talking about
before showing what an idiot you are.
<snipped more incorrect stuff>
>Refactoring in OO would mean you would not have to change anything
outside of the class itself.

I think you confuse with the Open Closed Principle. The Open Closed
Principle and refactoring are perpendicular ways to adapt code in a
_controlled_ fashion: The Open Closed Principle leaves the structure
intact, while refactoring makes sure the code behaviour remains the same
while you are restructuring it.
Not at all. But you have no idea what you're talking about.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Aug 25 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
9223
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
2
3925
by: Dave | last post by:
Does anyone know much about this tool? Also, if anyone can point me to a TSQL coding standard, please let me know. -- Dave
136
9251
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to...
5
3992
by: Ant | last post by:
hi, I'm now using C#. Seeing as though you can declare & initialize or pass a value to a variable on the same line as the declaration, is it still best practice to group all the variables together at the top of the method, or is it now acceptable for them to be declared at the point where they are initially needed, seeing as though you can...
10
2972
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? public class MyClass private _variableName as integer public property VariableName as integer
13
3094
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those sorts of clients. Mine are all small businesses whose sites will never reach those sorts of scales. I deal with businesses whose sites get maybe a...
4
8355
by: Number 11950 - GPEMC! Replace number with 11950 | last post by:
Just looking for the best way to set an image to fill a <divcontainer as a background without upsetting HTML/CSS validation... Thanks in Advance... -- Timothy Casey GPEMC! >11950 is the number@fieldcraft.biz 2email Terms & conditions apply. See www.fieldcraft.biz/GPEMC Discover valid interoperable web menus, IE security, TSR Control, &...
16
2784
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. ...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.