|
 |
|
ไม่ทราบว่าพวกพี่ ๆ เขียนโค้ดกันอย่างไรคะ ที่ทำให้เป็นระเบียบ ง่ายต่อผู้อื่นเข้าใจในวันข้างหน้า เช่น |
|
 |
|
|
 |
 |
|
โดยส่วนตัว ผมจะใช้ย่อหน้าเอาครับ + กับ comment เช่นCode (PHP)
Code (PHP)
<?php
if($a==$b)
{
if($b==$c)
{
//do
}
else
{
//do
}
}
else
{
while(//)
{
//do
}//end while
}
//end if($a==$b)?>
|
 |
 |
 |
 |
Date :
2009-03-09 11:55:54 |
By :
aprodise |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สำหรับผมแล้วคิดว่าการเขียนโค๊ด ควรที่จะเขียนให้สามารถดูได้ง่าย คือ ให้เป็นระเบียบอ่ะครับ
เพื่อให้ง่ายต่อการแก้ไข และง่ายสำหรับผู้อื่นที่อาจจะต้องมาดูโค๊ดของเรา   
|
 |
 |
 |
 |
Date :
2009-03-09 12:58:36 |
By :
ochitae |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ควรจะวางแผนก่อนเขียน
และควรจะมีคอมเม้น อธิบาย ฟังก์ชั่นหรือคำสั่งไว้ด้วย
|
 |
 |
 |
 |
Date :
2009-03-09 15:59:21 |
By :
xboy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่เคยเขียน คอมเม้นเลยแฮะ ทำงานคนเดียว
|
 |
 |
 |
 |
Date :
2009-03-09 19:33:50 |
By :
plakrim |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตั้งชื่อ คลาส เมธอด ตัวแปร ให้เข้าใจง่ายๆ
เขียนโค้ดให้บรรทัดน้อย แต่พอเข้าใจ
บางทีงานซับซ้อนก็ต้องคอมเมนต์ไว้เหมือนกัน เราจะคอมเมนต์เฉพาะที่คิดว่าอีก ไม่นานเราจะลืมหรือ เขียนให้เข้าใจง่ายได้มากกว่านี้ เพราะบางที มาอ่านโค้ดเก่าๆของตัวเอง ก็ยังอดหัวเราะไม่ไหว(เมื่อก่อนเราเขียนอย่างนี้หรือนี่ )
ใช้ switch แทน if ถ้ามีจำนวนมาก
ใช้ operator ให้ถูกกับงาน
เว้น ไว้อย่างน้อย 1 บรรทัด ถ้าทำคนละโอเปอเรชั่นกัน
ต้องศึกษา ส่วนลึกของภาษา(จำเป็นต้องอ่าน แมนน่วน เพราะจะสามารถทำให้ฝีมือเราก้าวหน้าได้)
เขียนแบบ oop เพื่อรองรับงานแบบสามารถเติบโตได้ในอนาคต(อันนี้ต้องเขียนคอมเมนต์ ค่อนข้างเยอะ ไม่ก็ต้อง ออกแบบใว้ในกระดาษ เผื่อว่าจะแก้งานก็ดูที่จดไว้ก็จะแก้ได้ง่ายมาก)
เมธอด หรือ ฟังชั่นที่มี อาร์กรูเมนต์เยอะๆ และมี ดีฟอล์วาลู่ด้วย ก็ควรจะคอมเมนต์ไว้ด้วยว่า แต่ละตัวแปรหมายความว่าอย่างไร ต้องการชนิดไหน
ลองดูตัวอย่าง Javascript ของผมดูหน่อยละกัน
Code
/********************************
# Author : Tawatchai Wongtom (Hide)
# Last Update : 2008/11/16
# Email : [email protected]
# Web Site : http://gunner.freetzi.com
# File Name : gunner_script.js
# ทดสอบบน IE 6 และ Firefox 3.01
********************************/
//Ajax
var request = null;
try{
request = new ActiveXObject('Microsoft.XMLHTTP');
}catch(err){
try{
request = new XMLHttpRequest();
}catch(err2){}
}
var view_behavior = {
initialize : function(){//web constructor
if(!document.getElementById || !document.createElement || request == null){return;}//เช็คดอมซัพพอร์ท
//ตำแหน่งของเลเยอร์
var def_width = 545;
var scr_width = screen.availWidth;
var new_left = parseInt( (scr_width - def_width) / 2 ) + "px";
var new_left2 = parseInt( (scr_width - 285) / 2 ) + "px";//login layer
document.getElementById('employ_layer').style.left = new_left;
document.getElementById('help_layer').style.left = new_left;
document.getElementById('login_layer').style.left = new_left2;
ajax.showWork(1);
},
showEmploy : function(select_index){
document.getElementById('help_layer').style.visibility = 'hidden';
document.getElementById('login_layer').style.visibility = 'hidden';
view_behavior.hideOtherLang();//ลบ
document.getElementById('employ_layer').style.visibility = 'visible';
document.getElementById('select_box').selectedIndex = select_index;
document.getElementById('detail').focus();
},
hideEmploy : function(){
view_behavior.hideOtherLang();
document.getElementById('employ_layer').style.visibility = 'hidden';
},
showHelp : function(){
document.getElementById('employ_layer').style.visibility = 'hidden';
document.getElementById('login_layer').style.visibility = 'hidden'
document.getElementById('help_layer').style.visibility = 'visible';
},
hideHelp : function(){
document.getElementById('help_layer').style.visibility = 'hidden';
},
showLogin : function(){
document.getElementById('employ_layer').style.visibility = 'hidden';
document.getElementById('help_layer').style.visibility = 'hidden'
document.getElementById('login_layer').style.visibility = 'visible';
document.getElementById('uname_textbox').focus();
},
hideLogin : function(){
document.getElementById('login_layer').style.visibility = 'hidden';
},
selector : function(field){//เลือกภาษาจ้างงาน
if(field.value == 'other'){
document.forms['employ_form'].other_lang.style.visibility = 'visible';
document.forms['employ_form'].other_lang.focus();
}else{
view_behavior.hideOtherLang();
}
},
hideOtherLang : function(){
document.forms['employ_form'].other_lang.style.visibility = 'hidden';
}
}//view_behavior
var controller = {
submitEmploy : function(form){
//เช็คข้อมูล
var error_text = '';
var first_occure;
if(form.language.value == 'other'){
if(form.other_lang.value == ''){
error_text += '\nลักษณะงาน';
first_occure = form.other_lang
}
}
if(form.detail.value == ''){
error_text += '\nรายละเอียด';
first_occure = first_occure == null ? form.detail : first_occure;
}
if(form.contact.value == ''){
error_text += '\nที่อยู่ติดต่อ';
first_occure = first_occure == null ? form.contact : first_occure;
}
if(form.attach.value != ''){
var ext = form.attach.value.split('.');
ext = ext[ext.length-1];
var accept_type = new Array('zip', 'rar', 'doc', 'pdf', 'jpg', 'jpeg', 'gif', 'png');
var accept = false;
for(var i=0; i < accept_type.length; i++){
if(ext.toLowerCase() == accept_type[i])
accept = true;
}
if(!accept){
error_text += '\nชนิดเอกสารให้ถูกต้อง';
first_occure = first_occure == null ? form.attach : first_occure;
}
}
//ตรวจสอบและซับมิตฟอร์ม
if(first_occure != null){
alert("โปรดระบุ: " + error_text);
first_occure.focus();
}else{
form.submit_button.value = ' กำลังส่งข้อมูล ';
form.submit_button.disabled = true;
try{
form.submit();
}catch(err){
alert("โปรดระบุ:เอกสารให้ถูกต้อง");
form.submit_button.value = ' ส่งข้อมูล ';
form.submit_button.disabled = false;
form.attach.focus();
}
}
}//submitEmploy()
}
var ajax = {
showWork : function(id){
var dest = document.getElementById('work_detail');
request.open('GET', 'work_detail_ajax.php?id='+id);
request.onreadystatechange = function(){
if(request.readyState == 1){
dest.innerHTML = '<img src="Pic/loading.gif" width="16" height="16" align="absmiddle">กำลังโหลด..';
}else if(request.readyState == 4){
if(/200|301/.test(request.status))
dest.innerHTML = request.responseText;
else
dest.innerHTML ='เกิดข้อผิดพลาดในการโหลด <a onClick="ajax.showWork('+ id +')">ลองใหม่</a>';
}
}
request.send(null);
}
}
แต่ทำไม indent ไม่มีหว่า
|
 |
 |
 |
 |
Date :
2009-03-09 20:54:26 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|