เขียนยังไงกันเหรอครับ

tags:

กลุ่มที่ 1 {}

function hello() {

}

หรือ

function hello()

{

}

กลุ่มที่ 2 for

for(i=0;i<10;i++)

for(i = 0; i < 10; i++)

for ( i = 0; i < 10; i++)

ส่วน if, while เอาทั้งสองมารวมๆ กัน ไม่ทราบว่าเพื่อนๆ เขียนแบบไหนกันเหรอครับ ผมเขียนแบบ ที่ 1 กับ 3 ไม่ทราบว่า คนอื่นๆ เขียนกันแบบไหนครับ

ตอนนี้เริ่มไป python กำลังเซ็ง space กับ tab อ่ะครับ มึนตึ๊บ แต่ภาษามันสวยดี

wklk's picture

ผมเขียนงี้ครับ
function hello(){
   ...
}

for(i=0; i<10; i++)

Nohohon's picture

function hello() { … } for(i=0; i<10; i++)

kaze's picture

function hello() {
    ...
}

for (i = 0; i < 10; i++)

หลัง keyword มี 1 space แต่ถ้าเป็น function วงเล็บติดเลยอ่ะครับ
ผมไว้ใช้ regular expression เพื่อหาว่า function อะไรถูก call กี่ครั้งบ้่างอ่ะครับ ^^

Silence's picture

function hello()
{

}

for( i = 0; i < 10; i++ )


Without Wax - T.J.M. Potter

iake's picture

function hello() {
...
}

for (i = 0; i < 10; i++)

sugree's picture
int main(int argc, char *argv[])
{
        int i;

        for(i=0; i<10; i++) {
                if (i < 3) {
                        printf("%d < 3", i);
                } else if (i < 5) {
                        printf("%d < 5", i);
                } else {
                        printf("%d >= 5", i);
                }
        }
        return 0;
}

tab = 8 เสมอ ชื่อจริง K&R ชื่อเล่น Linux Kernel Coding Standard

ยังมีแบบอื่นอีก http://en.wikipedia.org/wiki/Indent_style

mk's picture

ผมตั้ง tab=4 ถ้าเป็น html ใช้ tab=2

เปิดกระทู้ .vimrc ดีกว่า

kaze's picture

เหมือนกัน make up language ใช้ tab 2 ที่เหลือเป็น 4

moniguna's picture

เอา .vimrc มาโชว์กันเลย :p

Ford AntiTrust's picture

4 space / tab ครับ ทุกภาษาที่ผมใช้จะแบบนี้หมดเลย

int main(int argc, char *argv[]){
    int i;
    for(i=0; i<10; i++){
        if(i < 3){
            printf("%d < 3", i);
        }else if (i < 5){
            printf("%d < 5", i);
        }else{
            printf("%d >= 5", i);
        }
    }

    return 0;
}

Ford AntiTrust’s Blog | PHP Hoffman Framework

noyzilla's picture
int main(int argc, char *argv[]) { //หลังวงเล็บ ปกติผมจะเคาะเสมอ
    for(i=0; i<10; i++) {
        if(i<3) {
            printf("%d < 3", i);
        }
        else if (i < 5) {  //ผมชอบ else มาที่บรรทัดใหม่ อ่านง่ายดี
            printf("%d < 5", i);
        }
        else {
            printf("%d >= 5", i);
        }
    }

    return 0;
}

อยากให้ vi พิมพ์ภาษาไทยได้จัง

Noyzi!!a’s Blog

moniguna's picture

ผมใช้ MacVim ก็พิมพ์ภาษาไทยได้นะครับ แต่เว้นวรรคไม่สวยแค่นั้นเอง :p

moniguna's picture

โดยรวมแล้วเขียนเหมือนผมเลย 4space/tab เหมือนกันด้วย แต่ตรง for ผมจะเขียนแบบนี้
for(i = 0; i < 10; i ++){

}
ปกติแล้ว Operator ผมจะเว้นวรรคหน้าหลัง

Ford AntiTrust's picture

กรำ ผมลืมเว้นส่วนนี้ ตอน copy จากคุณ sugree -_-’ ;P (จะสังเกตว่าตัวอื่น ๆ ก็เว้น ท่าทางจะตาลาย)

<

pre>

for(i=0; i < 10; i++){
    …
}
</pre>

Ford AntiTrust’s Blog | PHP Hoffman Framework

ipats's picture

แทบจะเหมือนกันเลยครับ
ต่างกันนิดเดียวแค่ บล็อก { } ของฟังก์ชัน ผมเอา { ขึ้นไป เหมือนบล็อกของ for กับ if
ส่วนแท็บ ปกติผมใช้แค่ 4 (เยอะเกินมันล้นอ่ะ)

----------
iPAtS

ABZee's picture

ทำเป็นโพลเลยได้ไหมเนี่ย อยากรู้เหมือนกัน

PoomK

sugree's picture

จริงๆ แล้วผมไม่ค่อยใส่ใจว่าเขียนยังไงนะ ถ้าเป็นโค้ดคนอื่นผมปรับตัวให้เขียนแบบอื่นได้ตราบใดที่มี indent

ถ้าไม่มี indent ก็ลาขาด

HyBRiD's picture

function hello() {

}

for(i=0;i<10;i++)


NERD GOD
lancaster's picture

function hello() {

}

for (i = 0; i < 10; i++) …

ชอบแบบนี้อะ

Ford AntiTrust's picture

กระทู้นี้น่าจะเป็นตัวอย่างที่ดีกับคนที่เขียนโปรแกรมรุ่นใหม่ ๆ นะ ว่าจะใช้ style code แบบไหน แล้วถ้าเขียนเป็นทีมก็ควรกำหนด style ซะแต่เริ่มต้นไปเลย จะได้เป็นทิศทางเดียวกัน

อย่างผมจะมีคู่มือ style ผมไว้เลย เวลาโยน code ให้คนอื่นเขียนต่อก็เอา style docs ผมไปด้วย เวลาแก้ไข จะได้ไม่ต้องมานั่งจัด style ใหม่ โดยเฉพาะเรื่อง tab กับ space นี่สำคัญมาก ๆ ยิ่ง Python นี่ต้องใส่ใจเลย ไม่งั้นโปรแกรมหลุดแถม bug ตรึมแน่ ๆ

ส่วน tab ตอนแรก ๆ ที่เขียน code นี่ใช้ 8 space นะ แต่ไป ๆ มา ๆ รู้สึกว่ามันเยอะเกินเลยปรับเหลือ 4 space ส่วน 2 space นี่ไม่ไหว บางครั้งมันหาความแตกต่างยากไปหน่อยนึง เลยเอา 4 ดีกว่า แล้วก็ใช้ Editor ที่ทำ Folding Brace กับ Matching Brace ได้ด้วยเพราะช่วยได้เยอะ เวลาหา scope ของการทำงาน ไม่ต้องมานั่งเพ่ง ๆ หรือ scroll ขึ้น-ลง (ใน EditPlus รุ่นใหม่ ๆ มีความสามารถนี้อยู่แล้ว) เลยทำให้ทำงานได้เร็วขึ้นลดปัญหาไปได้เยอะครับ

Ford AntiTrust’s Blog | PHP Hoffman Framework

few's picture

นี่เลย

function hello() {
..
}

กับ

for(i=0; i<10; i++)

my ifew

pococyber's picture

function hello()

   {

       …

   }

อิอิ ชอบเขียนแบบมี Indent ครับ ^^

rocketbomb's picture

ทำตาม code conventions ของ c กับ java ครับ

sugree's picture

ตามมาตรฐานไหนครับ อยากยลโฉม

anu's picture
lancaster's picture

พึ่งรู้ว่าตัวเองเขียนตาม code convention ของ java

IceDagger's picture

4 space เสมอ ไมใช้ tab

int main(int argc, char** argv)
<code>{
<code>&nbsp;&nbsp;&nbsp;&nbsp;int a = 0;
<code>&nbsp;&nbsp;&nbsp;&nbsp;int b = 0;

&nbsp;&nbsp;&nbsp;&nbsp;if (a == 0)
<code>&nbsp;&nbsp;&nbsp;&nbsp;{
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;....
<code>&nbsp;&nbsp;&nbsp;&nbsp;}

<code>&nbsp;&nbsp;&nbsp;&nbsp;for (int i=0; i < 10; ++i)
<code>&nbsp;&nbsp;&nbsp;&nbsp;{

<code>&nbsp;&nbsp;&nbsp;&nbsp;}
<code>}

pawinpawin's picture

int main(){ }

for (i=0; i<10; i++)

เอ แต่ผมเขียนโปรแกรมไม่เป็นนี่นา อิอิ ชแว้ปป

willwill's picture

function test(){
    print 'a';
}

(จริงๆ ใช้ tab ครับ แต่โดนมันตัด)

ส่วน for ไม่ได้ใช้ครับ (ใน PHP ใช้ foreach กับ while แบบ

foreach($var as $i => $n){
    do();
}
$i=0;
while($i <= 10){
    print $i;
}
ใน python ใช้ for i in var ตลอดครับ)

sugree's picture

ใครเป็น Hungarian บ้าง

tekkasit's picture

เฮอะๆ อ่านแล้วผมนึกถึงนี่เลยครับ

How to Write Unmaintainable Code

ตอนนี้มันแยกเป็นส่วนๆแล้ว ไม่ได้เรียงในแผ่นเดียว ลองอ่านที่นี่ก็ได้ครับ ยังเป็นรุ่นแผ่นเดียวอยู่ DevX: How to Write Unmaintainable Code

crucifier's picture

All man style ครับ

Site Search

 
Web blognone.com

Poll

User login