LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-26-2010, 04:06 PM   #16
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76

Hi, gnashley.
Quote:
Originally Posted by gnashley View Post
makyo, what do you use for formatting shell scripts?
I use shtidy, a perl script, that a colleague wrote ... cheers, makyo
Code:
NAME
            shtidy - Re-format Bourne-shell family scripts.

VERSION
            1.0

USAGE
            shtidy [-h] files

REQUIRED ARGUMENTS
            None.

OPTIONS
            -h      Display this help message.

DESCRIPTION
            The shtidy program re-formats a shell script written in the
            Bourne shell dialect.  It does not process csh-family
            scripts.

            The reformatting is quite simple.  Loops and conditionals are
            aligned so that an attractive, yet-space-conserving result is
            obtained.

DIAGNOSTICS
            A warning issued for HERE documents which are in an indented
            section.  The closing string (typically "EOF") will need to
            be out-dented manually.

Last edited by makyo; 03-26-2010 at 04:09 PM.
 
Old 03-26-2010, 04:20 PM   #17
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by MrCode View Post
Ack, I've seen über-concatenated code like that before — although it might have just been to obfuscate it to discourage copying...?
Sometimes combining lines like this makes it more obvious that the two loops are nearly identical, e.g. iteration over a matrix. If we're talking about the sematics and not just the formatting of otherwise-equivalent code, concatenating operations can make a huge difference in speed for e.g. sorting algorithms.

One thing I find useful is formatting that makes scanning for sections easier. Not just annotations, but indentations and particular styles of formatting nested statements so you (I) can quickly find a part based on the "at a glance" appearance of something rather than having to read every comment.

I dislike indenting nested blocks quite a bit because eventually you run out of "rightness" on your screen. On the other hand, I indent nested blocks in scripting because there isn't much nesting and often no delimiters (bash, R, and MATLAB mostly.) For things like C, C++, and Java, I start most procedure lines one tab in and indent the {} by one space per nesting. I have a pretty good eye for how far in a {} is by glancing. Having a definite margin for procedural code makes it easy to find the beginning and end of classes and functions, and if I need to throw in debugging code I don't indent it so I can find it easily later, and so I can grep a source tree to make sure I don't miss any of it.
Kevin Barry

Last edited by ta0kira; 03-26-2010 at 04:26 PM.
 
Old 03-27-2010, 04:12 AM   #18
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
It depends what I'm doing... If I'm throwing something together quickly for myself then I'll generally do it a bit hacked and whatever I feel like. If I'm doing something for work etc then I'll tend to make it a bit easier to read! I usually always have the first opening brace on the same line as the function.
If declaring more than one temporary variable of the same type I'll usually keep them on the same line however it again depends on how it's being used.

Code:
#include<stdio.h>

int main(int argc, char ** argv) {
  int a, b=0;

  for (a=0;a<100;a++) {
    b+=2;
  }

  printf("This is a simple example - %i, %i\n",a,b);

  return 0;
}
 
Old 03-27-2010, 12:29 PM   #19
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
makyo, can you post a copy of shtidy? I don't seem to find it anywhere.
 
Old 03-27-2010, 01:32 PM   #20
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by devnull10 View Post
It depends what I'm doing... If I'm throwing something together quickly for myself then I'll generally do it a bit hacked and whatever I feel like. If I'm doing something for work etc then I'll tend to make it a bit easier to read! I usually always have the first opening brace on the same line as the function.
If declaring more than one temporary variable of the same type I'll usually keep them on the same line however it again depends on how it's being used.

Code:
#include<stdio.h>

int main(int argc, char ** argv) {
  int a, b=0;

  for (a=0;a<100;a++) {
    b+=2;
  }

  printf("This is a simple example - %i, %i\n",a,b);

  return 0;
}
ACK!
Two many blank lines makes the program too spread out to read.
 
Old 03-27-2010, 01:41 PM   #21
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
@smeezekitty

Code:
#include <stdio.h>
#include <stdlib.h>
short filelength(char *f){
    FILE *t = fopen(f, "rb");
    fseek(t, 0, SEEK_END);
    int x = ftell(t);
    fclose(t);
    return x;
}

int main(int argc, char *argv[0xFF])
{
    int f=0,z=0,r=0,p=0,e=0;
    if(argc != 3){printf("Usage: %s <input file> <output file>", argv[0]); return -1;}
    FILE *in = fopen(argv[1], "rb");
    FILE *out_ = fopen(argv[2], "wb");
    if(in == NULL){printf("ERROR: *** CANNOT OPEN FILE: %s", argv[1]); return -1;}
    if(out_ == NULL){printf("ERROR: *** CANNOT OPEN FILE: %s", argv[2]); return -1;}
    char *ind = (char *)malloc(f=  filelength(argv[1]) + 1);
    char *out = (char *)malloc(f+= filelength(argv[2]) + 1);
    z = filelength(argv[1]);
    if(ind == NULL || out == NULL){
        printf("ERROR: *** CANNOT ALLOCATE MEMORY ***: %d KB", f/1024);
        return -1;
    }
    p=0;
    while(!feof(in)){fread(&ind[p++], 1, 1, in);}
    fclose(in);
    p=0;
    for(r=0;r<z;r++){
        if(ind[r] == ind[r+1] || (unsigned char)ind[r] == (unsigned char)0xEA){
        e=0;
        while(ind[r+e] == ind[r]){e++; if(e > 126){break;}}
        //printf("Found run at offset (%d-%d): %d [%d]\n", r, r+e, e, ind[r]);
        out[p] = 0xEA;
        out[p+1] = e;
        out[p+2] = ind[r];
        r+=(e-1);
        p+=3;
        } else {
            out[p] = ind[r];
            p++;
        }
    }
        fwrite(out, 1, p, out_);
return 0;

}
No offence, but do you think this looks good?

For me, at least, it's almost illegible.
 
Old 03-27-2010, 01:50 PM   #22
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by MTK358 View Post
@smeezekitty

Code:
#include <stdio.h>
#include <stdlib.h>
short filelength(char *f){
    FILE *t = fopen(f, "rb");
    fseek(t, 0, SEEK_END);
    int x = ftell(t);
    fclose(t);
    return x;
}

int main(int argc, char *argv[0xFF])
{
    int f=0,z=0,r=0,p=0,e=0;
    if(argc != 3){printf("Usage: %s <input file> <output file>", argv[0]); return -1;}
    FILE *in = fopen(argv[1], "rb");
    FILE *out_ = fopen(argv[2], "wb");
    if(in == NULL){printf("ERROR: *** CANNOT OPEN FILE: %s", argv[1]); return -1;}
    if(out_ == NULL){printf("ERROR: *** CANNOT OPEN FILE: %s", argv[2]); return -1;}
    char *ind = (char *)malloc(f=  filelength(argv[1]) + 1);
    char *out = (char *)malloc(f+= filelength(argv[2]) + 1);
    z = filelength(argv[1]);
    if(ind == NULL || out == NULL){
        printf("ERROR: *** CANNOT ALLOCATE MEMORY ***: %d KB", f/1024);
        return -1;
    }
    p=0;
    while(!feof(in)){fread(&ind[p++], 1, 1, in);}
    fclose(in);
    p=0;
    for(r=0;r<z;r++){
        if(ind[r] == ind[r+1] || (unsigned char)ind[r] == (unsigned char)0xEA){
        e=0;
        while(ind[r+e] == ind[r]){e++; if(e > 126){break;}}
        //printf("Found run at offset (%d-%d): %d [%d]\n", r, r+e, e, ind[r]);
        out[p] = 0xEA;
        out[p+1] = e;
        out[p+2] = ind[r];
        r+=(e-1);
        p+=3;
        } else {
            out[p] = ind[r];
            p++;
        }
    }
        fwrite(out, 1, p, out_);
return 0;

}
No offence, but do you think this looks good?

For me, at least, it's almost illegible.
Perfectly readable...
 
Old 03-27-2010, 01:51 PM   #23
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864

Original Poster
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
Quote:
Two many blank lines makes the program too spread out to read.
I agree that too many blank lines can be distracting, but they can be a good tool for organizing your code.

For example (C++ here):

Code:
#include <stdio.h>
#include <stdlib.h>

int numA = 2,
    numB = 3,
    numC = 4;
                          //<-- blank space to separate variable types
float numAf = 0.0f,
      numBf = 1.25f,
      numCf = 3.1415926f;

int main()
{
    float numArray1 = new float [numA];
    float numArray2 = new float [numB];
    float numArray3 = new float [numC];

    numArray1[0] = numAf;
    numArray1[1] = numBf;
                          //<-- blank space to separate assignment operations to different variables
    numArray2[0] = numAf;
    numArray2[1] = numBf;
    numArray2[2] = numCf;

    numArray3[0] = numAf;
    numArray3[1] = numBf;
    numArray3[2] = numCf;
    numArray3[3] = sin(numCf / 2);
                                 //<-- blank space to separate assignment from procedure
    for(int i = 0; i < 1; i++)
        printf("%i",numArray1[i]);

    for(int i = 0; i < 2; i++)
        printf("%i",numArray2[i]);

    for(int i = 0; i < 3; i++)
        printf("%i",numArray3[i]);

    delete[] numArray1;
    delete[] numArray2;
    delete[] numArray3;

    return 0;
}
The same program, without spacing:

Code:
#include <stdio.h>
#include <stdlib.h>

int numA = 2,
    numB = 3,
    numC = 4;
float numAf = 0.0f,
      numBf = 1.25f,
      numCf = 3.1415926f;

int main()
{
    float numArray1 = new float [numA];
    float numArray2 = new float [numB];
    float numArray3 = new float [numC];
    numArray1[0] = numAf;
    numArray1[1] = numBf;
    numArray2[0] = numAf;
    numArray2[1] = numBf;
    numArray2[2] = numCf;
    numArray3[0] = numAf;
    numArray3[1] = numBf;
    numArray3[2] = numCf;
    numArray3[3] = sin(numCf / 2);
    for(int i = 0; i < 1; i++)
        printf("%i",numArray1[i]);
    for(int i = 0; i < 2; i++)
        printf("%i",numArray2[i]);
    for(int i = 0; i < 3; i++)
        printf("%i",numArray3[i]);
    delete[] numArray1;
    delete[] numArray2;
    delete[] numArray3;
    return 0;
}

Last edited by MrCode; 03-27-2010 at 01:55 PM. Reason: forgot to free pointers
 
Old 03-27-2010, 01:51 PM   #24
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by smeezekitty View Post
Perfectly readable...
I am in total disbelief...
 
Old 03-27-2010, 01:53 PM   #25
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
@MrCode

I do think that using blank lines to separate blocks of code that do a certain small task together is a good practice.
 
Old 03-27-2010, 01:56 PM   #26
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Quote:
Originally Posted by MTK358 View Post
@MrCode

I do think that using blank lines to separate blocks of code that do a certain small task together is a good practice.
Which is what I do. I wouldn't have a blank line after every statement however if the statement was logically separate from the previous then I generally would.
 
Old 03-27-2010, 02:34 PM   #27
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi, gnashley.
Quote:
Originally Posted by gnashley View Post
makyo, can you post a copy of shtidy? I don't seem to find it anywhere.
Yes ... cheers, makyo
Attached Files
File Type: txt shtidy.txt (4.4 KB, 7 views)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
e.g., BSD style (Slackware) vs. SystemV style startup scripts haertig Slackware 5 01-03-2009 10:52 PM
Compiling kernel Debian style or Native style ? Raynus Debian 1 06-16-2008 06:56 AM
C++ coding style question re: random access iterators spursrule Programming 10 03-03-2008 09:21 PM
gnu style coding...Do u agree? alaios Programming 18 09-01-2005 07:28 AM
Indian Hill C Style and Coding Standands liguorir Programming 0 05-25-2004 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration