totally useless code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

totally useless code

Post by Nay »

hey, i was away for sometime. stuck with httpd and php 4.2 on my redhat. so i coded this. i didn't know where to put (general dicussion or php code) but it's more php codey. so what's the point, well, i don't see a code review section anywhere so i'd just put it here and ask what do you guys think about it? :lol:

Code: Select all

<?php

$names = array("numbers", "letters", "characters"); // names of the following arrays
$numbers = range(0, 9); // makes an array from 0 to 9
$letters = range(a, z); // makes an array from a to z
$characters = array("~", "@", "#", '$', "%", "^", "&", "*", "(", ")"); // array of other characters

$decs = array("strong", "em", "u"); // array of the decoration tags, bold, italic and underline

   function randCol() {
      $c_letters = range(a, f); // valid color letters (a to f)
      $c_numbers = range(0, 9); // valid color numbers (0 to 9)
      $c_all = Array_merge($c_letters, $c_numbers); // joins the two arrays
      $color = ""; // defines color to return, started empty
      while(strlen($color) < 6) { // make sure that the color code IS 6 characters
      $max = count($c_all); // gets max key of the array
      $num = rand(0, $max); // produces a random number
      $color .= $c_all[$num]; // adds the given value to "color"
      }
      return $color; // returns color
   }

   function randVal($array) {
         shuffle($array); // shuffles the given array
         $max = count($array) - 1; // gets the biggest KEY in the array
         $num = rand(0, $max); // produces a random number
         $val = $array[$num]; // defines a random value in the array to return
         return $val; // returns the value
   }

print <<< PAGE_START
<html>

<head>

   <title>Rando!</title>

</head>

<body bgcolor="000000" onLoad="setTimeout('document.location = document.location', 2000)">

   <table cellpadding="0" cellspacing="0" border="0" width="100%">
PAGE_START;

// page starting HTML outputted

   for($rows = 0; $rows < 20; $rows++) // make 20 rows
   {
      print "   \n<tr>\n"; // start TR
         for($cols = 0; $cols < 10; $cols++) // make 10 columns per row
         {
            $key = randVal($names); // gets a random array
            $key = randVal(${$key}); // gets a random value out of the random array returned
            $dec = randVal($decs); // gets a random decoration to apply
            $color = randCol(); // makes a random color
            print <<< COL
         <td>
            <span style="color:$color">
               <$dec>
                  $key
               </$dec>
            </span>
            <br />
         </td>

COL;
         }
      print "   </tr>\n\n"; // ends the row
   }

print <<< PAGE_END
   </table>
</body>

</html>
PAGE_END;
?>
-Nay

ps: this is my first indented code ever! i started using indented coding after using gEdit in redhat since u can actually customize to add spaces rather than tabs. me uses 3 spaces :P
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Looks good.

I like indenting because it makes the code more understandable when reviewing it and looking for bugs.
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

yah :wink: good coding ...
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Tabs are better for indenting in my opinion, reduces overall file size.

Not an issue for a small script, but you can tell the difference for larger scripts.

1 Tab = 1 bit
3 spaces = 3 bits

so, a 500 line script, assuming 1 tab on each line, my tabs would equal 62bytes, yours would be 182 bytes.

Mark
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Yes Yes, very technical Mark :D.

But then for some reason when I open up my code in notepad on a windows machine, it looks all screwed! O_o

-Nay
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

why anyone would want to open code in Notepad is beyond me!

Mark
User avatar
Toneboy
Forum Contributor
Posts: 102
Joined: Wed Jul 31, 2002 5:59 am
Location: Law, Scotland.
Contact:

Post by Toneboy »

Bech100 wrote:Tabs are better for indenting in my opinion, reduces overall file size.

Not an issue for a small script, but you can tell the difference for larger scripts.

1 Tab = 1 bit
3 spaces = 3 bits

so, a 500 line script, assuming 1 tab on each line, my tabs would equal 62bytes, yours would be 182 bytes.

Mark
Eh? 182 / 62 doesn't go - surely it would be 186 bytes?

I guess Maths wasn't Snoop's strong point. ;)

P.S. I can only be pedantic because I'm not as clued up on PHP as other people. :)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

hahaha - just a typo, my working out on the piece of paper here was correct.

The actual figure should be 187.5.

8 bits in a byte --- 500 bits ---- 62.5 bytes.

multiply by 3 to get Nays size.

Mark
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

hey, i'm not THAT heavy!

only 90 lbs :D............lol too light for a 14 yr old.........?

mMm..........maybe.......

-Nay
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Why do you think the tab and space each occupies one bit? It occupies one byte, actually not less then one byte because some languages require more then 256 characters.
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

Nay,
It looks good but I will make a few comments since you asked. Consistancy is key in writing cose. A college prof would tear you up on the fact that you do two things two different ways.

Example: Here you use this sstyle for your braces.

Code: Select all

function randVal($array) { 
         shuffle($array); // shuffles the given array 
         $max = count($array) - 1; // gets the biggest KEY in the array 
         $num = rand(0, $max); // produces a random number 
         $val = $array[$num]; // defines a random value in the array to return 
         return $val; // returns the value 
   }
And here you use this style:

Code: Select all

for($rows = 0; $rows < 20; $rows++) // make 20 rows 
   { 
      print "   \n<tr>\n"; // start TR 
         for($cols = 0; $cols < 10; $cols++) // make 10 columns per row 
         { 
            $key = randVal($names); // gets a random array 
            $key = randVal(${$key}); // gets a random value out of the random array returned 
            $dec = randVal($decs); // gets a random decoration to apply 
            $color = randCol(); // makes a random color 
            print <<< COL 
         <td> 
            <span style="color:$color"> 
               <$dec> 
                  $key 
               </$dec> 
            </span> 
            <br /> 
         </td> 

COL; 
         } 
      print "   </tr>\n\n"; // ends the row 
   }
Pick one and stick with it in everything that you write. It is completly a prefference thing... the

Code: Select all

if (condition)
{
    do work
}
style is a more traditional format but as I said, it is a personal choice... I use this style mainly because I think that it is easier to read.

The same goes for your comments. Most places the comments are at the end of the line but then you have one at the top of a block. It is not necessary to comment every line. Someone reading your code should know that

Code: Select all

return $color; // returns color
Returns the color (especially because you make good use of meaningful variable names). So, commenting things like that is a bit redundant. Mostly, you should comment each block of code telling what the block does, why, and mentioning any possible problems that could arise. Just pick one way (traditionaly above the code is standard) and reserve the in-line style for difficult to understand areas where you want to catch the readers eye. In essence, you have written psuedo code in the comments next to every line which takes away from the power of a comment to attract the attention of someone that is reading it. Every function deserves a comment at the top without question. The comment should tell the reader what it does, why it does it, and any special "features" the function has.

Please don't take the comments that I have made in the wrong way... all in all, you are on the right track and the fact that you asked says that you actually care enough to want to do it right. That is a good thing and you will thank yourself later.

Later,
John M
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Weirdan : Yeah, just realised i got my bits and bytes mixed up...DOH

Still, 3 spaces is 3 times larger than 1 tab

Mark
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Johnm: thanks for you input, the "more" comments were added before I posted just to avoid, "what the hell is Nay trying to do?" lol anyhow, and the braces..........well yeah, i need to get rid of that habbit. i normally type and press enter after a for() but not an if() or function() most of the time.

*sigh*

-Nay
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

If you don't mind some more criticism (constructive, I hope) I'd recommend not mixing up html & php in the same file.

There are several reasons for this.

Html designers can't work with the program because they have to deal with scary php scripts.

It's generally messy.

It ties the script to a single output method. The business logic (declaring vars) and presentation are mixed up together and that restricts your options.

Instead, a script can simply define a bunch of vars and then include an html template where they get echo'd out. Once you've got the vars you can output them in any format you like - xml, pdf etc. For example you could maybe have "presenter" objects to pick them up and do all the presentation work.

A very important design rule of thumb is to delineate separate tasks clearly. Lots of good things happen when you do that.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

wha? O_o
Instead, a script can simply define a bunch of vars and then include an html template where they get echo'd out. Once you've got the vars you can output them in any format you like - xml, pdf etc. For example you could maybe have "presenter" objects to pick them up and do all the presentation work.
clarify a little? No problemo for the critism, I am still a newbie nevertheless.

-Nay
Post Reply