3 Replies - 403 Views - Last Post: 04 April 2012 - 04:50 PM

#1 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

What Are Your Coding Habits?

Posted 04 April 2012 - 04:15 PM

What are your coding habits? In Java, I tend to make methods with weird spacing:
//My normal methods, notice that there are no extra lines between
//opening and closing brackets.
public void doStuff(String blah) {
    System.out.println(blah);
}

//Methods that have code in them that take up three or more lines
//They will have new lines between opening and closing brackets.
public int calculateTotal(int one, int two) {

    System.out.println(one);
    System.out.println(two);
    return one + two;

}

//If statements, I always put a space after if()
if (thisHappens) {
    //doThis;
}


Now share some of your coding habits.

Is This A Good Question/Topic? 0
  • +

Replies To: What Are Your Coding Habits?

#2 xxxjj18  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 53
  • View blog
  • Posts: 167
  • Joined: 30-November 11

Re: What Are Your Coding Habits?

Posted 04 April 2012 - 04:31 PM

I generally just put a whole bunch of code/ideas together until it all works and I understand the concept of what I'm doing. Then I rewrite it all to work in a procedural manner that is clean, easy to read and understand, and well commented.

Then I rewrite it in a class-based format.

I know that there are a lot of steps in this process but hey, practice makes perfect :) lol.

I also make sure each line of code that doesn't pertain to the previous line of code is separated by whitespace to the top and bottom;

class Article { 
  
public 
	   $id,
	   $title,
	   $summary,
	   $content,
	   $publicationDate;

public function__construct($row) {
	
	$this->title = $row['title'];
	$this->summary = $row['summary'];
	$this->content = $row['content'];
	$this->publicationDate = $row['publicationDate'];
	
}

};

class Log {

	public $articles = Array();
	
	public function displayArticle($id) {
		
		//Display the Article
		
		if(!empty($this->articles[$id])) {
			
			$display = "
							
							<h1>{$this->articles[$id]->title}</h1>
							\n
							
							\n<p class='article'>{$this->articles[$id]->content}</p>
							
							\n
							
							\n<p class='article' style='font-weight: 700;'>{$this->articles[$id]->publicationDate}</p>
							
					   ";
			
		}
	
	}

};



In the above example I wrote the comment "//Display the Article" on its own line as well as each paragraph tag that contained the article's contents

Edit:

And here's an example of code that was just thrown together :X

<?php

	/**
	  * Load includes
	  */
	include("classes/template.class.php");
	//include("includes/database.php");
	
	/**
	  * Declare variables and globals
	  */
	  
	  //global $database;
	  //$result = mysql_query("SELECT * FROM `".DB_TBL_NEWS."` ORDER BY `id` DESC") or die(mysql_error());
	  //if($result != null){ $row= mysql_fetch_array($result); }else{ print "<span class=\"error\">Error retrieving data from '".DB_TBL_NEWS."'.</span><br />"; }
	/**
	 * Creates the index page
	 * Load a basic overview of actions applied to the database recently
	 */
	 
	$admin = new Template("template/layout.tpl");
	$admin->set("head.title", "GX Tutorials");
	
	$menu = Array(
					"Main" => Array("Home" => "index.php", "Test" => "test.php"),
					"Test" => Array("Test" => "test.php", "Home" => "index.php", "Test3" => "test.php", "Test4" => "test.php"),
					"Test2" => Array("Test3" => "#", "test2" => "#")
					);
	
	$last = count($menu) - 1;
	$j = $last;
	
	foreach($menu as $key => $value) {
			$sbar[$j] = "<li><a href=\"#\">$key</a><ul>";
			$j--;
		}
	
	$j = $last;
	
	foreach ($menu as $i => $row)
	{
		$isFirst = ($i == 0);
		$isLast = ($i == $last);
		
		foreach($row as $key => $value) {
			
			$sbar[$j] .= "<li><a href=\"$value\">$key</a></li>";
			
		}
		
		$sbar[$j] .= "</ul></li>";
		$j--;
	}

	$sbar1 = '
					<ul id="menu">
						<li><a href="#">Main</a>
							<ul>
								<li><a href="index.php">Home</a></li>
								<li><a href="test.php">Test</a></li>
							</ul>
						</li>
					</ul>
				';
				
	$sbar2 = "<ul id=\"menu\">";
	
	foreach($sbar as $key => $value) {
		$sbar2 .= $sbar[$key];
	}
	
	$sbar2 .= "</ul>";
	
	$testPage = '
					<p>This is a test</p>
				';
	
	$admin->set("head.stylesheet","template/css/default.css");
	$admin->set("body.content.contents","<h3>Test</h3>".$testPage);
	$admin->set("body.sidebar.contents",$sbar2);
	
	/**
	 * Outputs the page with the user's profile.
	 */
	print $admin->output();
	
?>


This post has been edited by xxxjj18: 04 April 2012 - 04:36 PM

Was This Post Helpful? 2
  • +
  • -

#3 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: What Are Your Coding Habits?

Posted 04 April 2012 - 04:35 PM

View Postxxxjj18, on 04 April 2012 - 04:31 PM, said:

I generally just put a whole bunch of code/ideas together until it all works and I understand the concept of what I'm doing. Then I rewrite it all to work in a procedural manner that is clean, easy to read and understand, and well commented.

Then I rewrite it in a class-based format.

I know that there are a lot of steps in this process but hey, practice makes perfect :) lol.

I also make sure each line of code that doesn't pertain to the previous line of code is separated by whitespace to the top and bottom;

That's what I do most of the time. Also, if I find a code snippet of something I couldn't figure out how to do, I won't just copy/paste. I will write out each line so that I understand the flow of things.
Was This Post Helpful? 1
  • +
  • -

#4 xxxjj18  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 53
  • View blog
  • Posts: 167
  • Joined: 30-November 11

Re: What Are Your Coding Habits?

Posted 04 April 2012 - 04:50 PM

Yeah, same here. I'm trying to move away from looking at snippets though; I'll usually try to write my own script when I have a problem and think logically about how I should go about it and how I can solve the problem ( though many times this results in failure :X). I figure if somebody else could imagine a solution to my problem, then I can too..

Though looking at a snippet would probably save me hours of headaches and death threats to my computer lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1