Transcendent Studios
« Your "Style" »

Welcome Guest. Please Login or Register.
May 19, 2013, 6:26am



Exist To Create

Transcendent Studios :: Coding Corner :: Coding Chat :: Your "Style"
   [Search This Thread] [Share Topic] [Print]
 AuthorTopic: Your "Style" (Read 370 times)
Bobby Hensley
Newb
*
[M:20]
member is offline

[avatar]


[homepage]

Joined: Feb 2009
Gender: Male
Posts: 27
Location: Keene, NH
Karma: 0
 Your "Style"
« Thread Started on Apr 10, 2009, 10:14am »

Everybody has their own style of programming. Details from variable naming convention to function naming to OOP vs. Imperative, etc. One could say it's what makes each of us unique from one another.

So here's the trial. Write your own version of a simple hit counter. This isn't a test to see who's works better or anything remotely close to that. It's just a simple thirty second script that provokes various areas that show off your programming style.

Here's mine:

<?php

class Counter
{
protected $num_hits;
protected $hits_file;

/**
* Assign the properties and increment the count
*
* @param string File to store hit count
**/
public function __construct ($file_name)
{
if (!empty ($file_name))
$this -> hits_file = $file_name;

$this -> num_hits = $this -> get_current_count () + 1;
$this -> update_count ();
}

/**
* Grab the current hit count
*
* @return function call Hit file doesn't exist
* @return integer Count property is already set
* @return integer Count isn't set, get it from the file
**/
public function get_current_count ()
{
if (!file_exists ($this -> hits_file))
return $this -> update_count;

if (isset ($this -> num_hits))
return $this -> num_hits;

return file_get_contents ($this -> hits_file);
}

/**
* Update the hit count file
*
* @return integer Boot back 1 incase get_current_count () called us
**/
private function update_count ()
{
$count_handle = fopen ($this -> hits_file, "w+");
fwrite ($count_handle, $this -> num_hits);
fclose ($count_handle);

return 1;
}
}

$count = new Counter ("counter.dat");
echo $count -> get_current_count ();

?>


Now note that if I was actually to use the above, I wouldn't comment it so heavily unless I was releasing it. :P But on actual projects that's exactly what I'll do in terms of commenting. Though for larger classes I tend to make just a single, very large comment at the top defining everything in the file.
Link to Post - Back to Top  IP: Logged
Blind
Administrator
*****
Head Coder[M:0]
member is offline

[avatar]

Paper Wings


[homepage]

Joined: Jan 2009
Gender: Male
Posts: 1,356
Karma: 1
 Re: Your "Style"
« Reply #1 on Apr 10, 2009, 11:01am »

Commented it so that any one can learn from this :)

Code:
<?php
/* This is flat file so we need to add the txt file the hits are saved in to a variable.
Variable name is storage, but of course you can call it anything you like.
hit.txt = 0, empty file untill the code registers new hits */
$storage = 'hit.txt';

/* Just uses an IF statemtn to make sure we are aloud to write to this file
If not we may need to CCHMOD it
Using PHP built in functions "is_writable" */
if (!is_writable($storage)) die('This file is not writable, you may want to fix this');

/* Again using 2 built in php functions
trim() is used to remove all white spaces. There is ltrim() and rtrim() which can be used to remove white spaces from the start or end of a string
We then use another built in function "file_get_contents" which reads any file into a string.
The +1 at the end is telling PHP to add another entry into the storage file */
$hits = trim(file_get_contents($storage)) + 1;

/* Again another built in function.
fwrite() is used to write the contents of a string
fopen() is used to open and bind a file
w is the mode for fopen, w stands for write() */
fwrite(fopen($storage, 'w'), $hits);

/* And this will just display the amount of hits on the page.
You can use echo or print for this */
print $hits;
?>


There are many ways of creating something like this.
Flat files is generally a widely used method of doing so instead of MySQL.
I wouldn't say this is the best way of creating a hit counter, but it is an easy and quick method.

And i wouldn't' normally comment a script at all, like this.
If i am doing a large project i normally have a large comment block at the top of each page.
Link to Post - Back to Top  IP: Logged

xcessive
Administrator
*****
[M:0]
member is offline

[avatar]



Joined: Jan 2009
Gender: Male
Posts: 1,171
Location: around
Karma: 0
 Re: Your "Style"
« Reply #2 on Apr 11, 2009, 9:15pm »

I never comment any code and i hate OOP, love precedureal... damn classes and types.
Link to Post - Back to Top  IP: Logged

[image]


iDesign
Easter Winners
*****
[M:155]
member is offline

[avatar]

No Cookie For Guessing Hobby.



Joined: Feb 2009
Gender: Male
Posts: 543
Karma: 0
 Re: Your "Style"
« Reply #3 on Apr 12, 2009, 4:58pm »

Here's my style.
xD
Link to Post - Back to Top  IP: Logged

[image]
DBuzzin
Junior Member
**
[M:150]
member is offline



[msn]

Joined: Apr 2009
Gender: Male
Posts: 90
Location: Durham
Karma: 0
 Re: Your "Style"
« Reply #4 on Apr 12, 2009, 5:49pm »

lol at iDesign :P

you guys ar making me want to get back into php, ive only got back into javascript this past week and im feeling the love like :P
Link to Post - Back to Top  IP: Logged

[image]
Blind
Administrator
*****
Head Coder[M:0]
member is offline

[avatar]

Paper Wings


[homepage]

Joined: Jan 2009
Gender: Male
Posts: 1,356
Karma: 1
 Re: Your "Style"
« Reply #5 on Apr 12, 2009, 10:24pm »

Get back into it!
Link to Post - Back to Top  IP: Logged

echnaret
Newb
*
[M:20]
member is offline




[homepage]

Joined: Mar 2009
Gender: Male
Posts: 47
Karma: 0
 Re: Your "Style"
« Reply #6 on Apr 20, 2009, 3:41am »

I haven't looked at anyone elses, I'll go back and do that after I write this.

Hmm, based on the stuff I use, I'd do something like this (not sure if it has bugs or not, I haven't actually tested it):

database.php
Code:
<?php

/* Class that deals with all
* Database connections
*/
class Database {
protected $connection;

/* Connect to Database
* (CONSTANT VARIABLES would be
* stored in a separate file and
* have been omitted for this
* example)
*/
function __construct() {
    $this->connection = mysql_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD or die(mysql_error());
    mysql_select_db(MYSQL_DB_NAME, $this->connection) or die(mysql_error());
}

/* Get the current number of hits from
* a table with two fields, "key" and
* "value"
*/
function get_number_hits() {
    $query = "SELECT value FROM ".TABLE_VARIABLES." WHERE key = 'number_hits'";
    $result = mysql_fetch_assoc(mysql_query($query));
    return $result['value'];
}

/* Increment the number of
* hits by 1
*/
function add_hit() {
    $query = "UPDATE ".TABLE_VARIABLES." SET value = value+1 WHERE key = 'number_hits'";
    return mysql_query($query);
}

$database = new Database();


index.php
Code:
<?php
include("database.php");
$database->add_hit();
?>

<html>
<head><title>My Hit Counter Example</title></head>
<body>
<h1>My Hit Counter Example</h1>
<p>Hello there sir, you are visitor number <?php $database->get_number_hits();?>!</p>
</body>
</html>


Yeah, I probably don't technically need two files as this is so simple, but that's the way I've been doing things thus far.

That was kind of fun. We should totally do some more of these.. ;)
« Last Edit: Apr 20, 2009, 3:46am by echnaret »Link to Post - Back to Top  IP: Logged

[image]
Abandoned Project - A creative writing project I made where you can read room descriptions that others have made, vote for the ones you like, and create some rooms of your
own!
robeck
Newb
*
member is offline





Joined: Mar 2010
Gender: Male
Posts: 29
Karma: 0
 Re: Your "Style"
« Reply #7 on Apr 5, 2010, 4:44am »


Nếu một mai Admin em có lỡ...神奈川県 賃貸 Delete rồi File chứa những yêu thương
Đôi mắt biếc Paint đầy nỗi vấn vương 座間市 賃貸 Đừng khóc nữa anh Restore trở lại.
Em giận hờn cưỡi IE chạy mãi 神奈川県 不動産 Anh hoảng hồn lấy Firefox đuổi theo
Đằng xa kia Netscape đá lông nheo エコ住宅 豊田市 Cancel luôn vì em là trên hết.
Em RAM ít nên Run nhiều sẽ mệt リフォーム 豊田市 Anh sẽ làm Physics Memory
Search cùng em trên khắp nẻo đường đi土壁 豊田市 Anh mạnh mẽ nhờ hai RAM cùng Bus. Robeck


Link to Post - Back to Top  IP: Logged
robeck
Newb
*
member is offline





Joined: Mar 2010
Gender: Male
Posts: 29
Karma: 0
 Re: Your "Style"
« Reply #8 on Oct 30, 2010, 10:28pm »

ポンプ | グラウトミキサー | パーカッションドリル | 流量計 | 北海道札幌市 | 株式会社大成建機
中古 ボーリングマシン 親切・丁寧・確実
中古 モルタルポンプ 大成建機は建設機械のレンタル・ リース及び販売会社です。お客様& #12398;ニーズに合った機種の選定、親&# 20999;・丁寧・確実な対応を心がけて 2362;ります。修理、メンテナンスも 362;気軽にご相談下さい。
中古 パーカッションドリル ボーリング機器専門に取扱いして おります!
レンタル取扱商品
ボーリングマシン、モルタルポ} 31;プ、グラウトミキサー、パーカӠ 3;ションドリル、流量計などボーリ ;ング機器全般を取り扱ってます
Link to Post - Back to Top  IP: Logged
robeck
Newb
*
member is offline





Joined: Mar 2010
Gender: Male
Posts: 29
Karma: 0
 Re: Your "Style"
« Reply #9 on Feb 8, 2011, 10:04pm »

アルマイト
アルマイト処理
アルマイト加工
Link to Post - Back to Top  IP: Logged
   [Search This Thread] [Share Topic] [Print]

Click Here To Make This Board Ad-Free


This Board Hosted For FREE By ProBoards
Get Your Own Free Message Boards & Free Forums!
Terms of Service | Privacy Policy | Notice | FTC Disclosure | Report Abuse | Mobile