d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Me Code Better
1234Next
Add Reply New Topic New Poll
Member
Posts: 3,580
Joined: Aug 17 2013
Gold: 275.01
Feb 20 2014 04:51pm
Since people in my class don't seem to like having their code read, or for that matter, reviewing other people's code, (with the exception of my teacher who's already busy enough as it is), I've decided I'm going to ask the small, but more engaged, population of jsp's Programmer section for some feedback.
I'd hope for this to turn into a little something like Eep's : Make Me a Better Programmer, and as such will try to keep myself motivated to keep updating this with as much relevant code and questions as I can. Why jsp? Well, why not? You guys aren't dumb.

Feedback without foundation (saying something without justification) will be ignored, unless I'm feeling like arguing that specific day. It's something I'm trying to leave in the past to be a better person overall (cynicism and overconfidence are aweful things...)
My goals for this, which are almost directly correlated with the reason I even visit this section aside from being interested, are the following:

- Improve my code reading skills
- Improve my code producing skills
- Broaden my knowledge past what is taught to us in class

Most of the code that will be displayed is directly related to school work, and by no means do I want solutions! I want feedback and criticism on my code so I can get better, because according to my teachers, "We are the best." Yeah, right. -.-


~ tl;dr ~

My first project will be a "Simon Says" android app, and as soon as I have some logic to share, I'll hurry back here and post it up for critics ^^
I'll post up a PHP class I made this week, as filler content, until I have some above-mentionned code ready for reviewing!

(Expect to see a wide range of languages in this thread, like C++, C# and Java, but certainly not limited to these)

Keep the debates clean and let's all try to gain a bit from this!
Member
Posts: 3,580
Joined: Aug 17 2013
Gold: 275.01
Feb 20 2014 04:55pm
PHP "Form" Validator

I'm only now learning PHP so there's surely something wrong with this. Critique away!
The idea is to give it data from a form, clean it up, and then give it back to the page in charge of then adding new users and sending a confirmation e-mail (still in the works)

Code
<?php

class YardFormValidator
{
private $data = array();
private $processedData = array();

function formValidation()
{
foreach($this->data as $checkData)
{
if(empty($checkData))
{
echo "Error for " . $checkData . " : Data Empty! <br>";
return false;
}
else
{
array_push($this->processedData, $this->test_input($checkData));
echo $checkData . " : Data Passed! <br>";
}
}
return true;
}

function setData(&$newData)
{
$this->data = $newData;
}

function getProcessedData()
{
return $this->processedData;
}

function test_input(&$data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}

?>
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 20 2014 11:28pm
Seems pretty straight forward. Looks like you just took the example here http://www.w3schools.com/Php/php_form_validation.asp and modified it to work on any operation with your formValidation function.

I am curious why you are keeping everything in the processedData array when you are passing by reference to the test_input function. That is just going to modify everything on the form anyways. What is the goal of the processedData array? Is it just so you can say "look at all the data I processed" for some reason later on?

This post was edited by Minkomonster on Feb 20 2014 11:28pm
Member
Posts: 3,580
Joined: Aug 17 2013
Gold: 275.01
Feb 21 2014 06:43am
Quote (Minkomonster @ Feb 21 2014 02:28am)
Seems pretty straight forward. Looks like you just took the example here http://www.w3schools.com/Php/php%5Fform%5Fvalidation.asp and modified it to work on any operation with your formValidation function.

I am curious why you are keeping everything in the processedData array when you are passing by reference to the test_input function. That is just going to modify everything on the form anyways. What is the goal of the processedData array? Is it just so you can say "look at all the data I processed" for some reason later on?


You'd be right on the modified W3School code ^^ I'm not sure where, but I read that you need to pass the data to a function with & if you wanted to modify its contents. This seemed a bit odd but I followed the advice. Usually, in C++ for example, passing something by reference avoids making copies everytime, I figured it must've been different with PHP.

As for processedData, for me it "visually" made sense, there's one array to receive the data, which you cannot "get" back, and the processedData array that you can only "get" and not "set" to modify any data once it's gone through processing. I can imagine there are better ways going about this and am open to suggestions for sure.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 21 2014 09:22am
Pass by reference passes in the address where that variable is stored. It means any modifocations done to it will be directly done to the value stored at the address. Pass by value passes a value which is then stored in local scope. So I just don't understand why you pass the data array in as reference, one because arrays are always pass by reference anyways, and two because you end up returning the array anyways. One of these operations seems unnecessary. And I am still not following what an array of processed data is used for
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 21 2014 10:55am
You came to the right place. These guys and gals (whichever you may be) have been very helpful since I started school a few years ago. There used to be a few bad apples who would run people out of the forum but it's been cleaned up and the help is solid, top notch advice from what I've read and seen from them. (Take that opinion with a heavy grain of salt as I myself am still learning.)

Edit: damn phone did it again.

This post was edited by NinjaSushi2 on Feb 21 2014 10:57am
Member
Posts: 3,580
Joined: Aug 17 2013
Gold: 275.01
Feb 21 2014 12:55pm
Quote (Minkomonster @ Feb 21 2014 12:22pm)
Pass by reference passes in the address where that variable is stored. It means any modifocations done to it will be directly done to the value stored at the address. Pass by value passes a value which is then stored in local scope. So I just don't understand why you pass the data array in as reference, one because arrays are always pass by reference anyways, and two because you end up returning the array anyways. One of these operations seems unnecessary. And I am still not following what an array of processed data is used for


I didn't know arrays were defaultly passed by reference. In reality I could just use the one array, with a getter and setter, and do what I'm doing without any problem. I guess you can look at the $data array as an entry point for the data and the $processedData array as an exit point for said data; though it's seemingly unnecessary now that I think of it -.-

Also I noticed this completely breaks if any of the data is empty and provides no way of knowing which one it was... I guess I'm already making sure, on the client side of things, that all fields are required, so in this case I'm doing one check too many I guess.

Quote (NinjaSushi2 @ Feb 21 2014 01:55pm)
You came to the right place. These guys and gals (whichever you may be) have been very helpful since I started school a few years ago. There used to be a few bad apples who would run people out of the forum but it's been cleaned up and the help is solid, top notch advice from what I've read and seen from them. (Take that opinion with a heavy grain of salt as I myself am still learning.)

Edit: damn phone did it again.


Yeah I got some great advice on an earlier thread of mine and I figure this is way that not only I, but others can learn from as well ^^


I should get started on that android game too -.-
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 21 2014 01:04pm
All an array contains is a pointer to a location in memory, eg an address. You then access the values stored at this contiguous block of allocated memory by offsetting the address by indexing it.

If you create an array of size 5, the system searches for s block of memory 5*sizeofdatatype and reserves this space. It then stores the address of the first cell of this memory in the array. When you do array[3] you are saying give me the value stored at address+3 cell.

For this reason, if you pass an array as a parameter you are always passing an address as it's value and therefore passing by reference.

This post was edited by Minkomonster on Feb 21 2014 01:07pm
Member
Posts: 3,580
Joined: Aug 17 2013
Gold: 275.01
Feb 21 2014 01:07pm
Quote (Minkomonster @ Feb 21 2014 04:04pm)
All an array contains is a pointer to a location in memory, eg an address. You then access the values stored at this contiguous block of allocated memory by offsetting the address by indexing it.

If you create an array of size 5, the system searches for s block of memory 5*sizeofdatatype and reserves this space. It then stores the address of the first cell of this memory in the array. When you do array[3] you are saying give me the value stored at address+3 cell.

For this reason, if you pass an array as a parameter you are always passing an address and therefore passing by reference.


Ok yeah this makes sense, isn't there something particular about the way it increments each address as well? (As in not in steps of 1? This is a really out of context question I know)
Thanks for the clarifications!
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 21 2014 01:12pm
Yes

Int array[5] allocates memory in a contiguous block for 5 ints. Ints are 4 bytes a piece so this array contains an address to the first cell of a 20 byte block. When you index it array[3] you are offsetting the initial index by the seize of 3 Ints or 12 bytes. Thus you are now points to the 12th byte of this block.
Go Back To Programming & Development Topic List
1234Next
Add Reply New Topic New Poll