ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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;
}
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Quote:
Originally Posted by devnull10
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.