Tiny Core Linux

General TC => Programming & Scripting - Unofficial => Topic started by: nick65go on August 16, 2020, 06:45:21 AM

Title: what coding style is prefered in tinycore?
Post by: nick65go on August 16, 2020, 06:45:21 AM
I am just curious about preferred style of writing code in sh language, used in tinycore. Mostly is about using space instead of tabs or ignored them, for indentation of: for case, if, while etc. Or if is recommended, or correct/preventive syntax to use space after function() {.
Example extracted from tinycore: /etc/init.d/tc-functions, no space, no identation
Code: [Select]
checkbootparam(){
stringinstring " $1" "$CMDLINE"
return "$?"
}
 
which could be like (_ means space here)
checkbootparam()_{
_stringinstring " $1" "$CMDLINE"
_return "$?"
}
I mean like it is already a little down in the code:
Code: [Select]
find_mountpoint() {
  MOUNTPOINT=""
i understand that is mainly esthetic/visual, with  no speed code gain.
Title: Re: what coding style is prefered in tinycore?
Post by: Rich on August 16, 2020, 08:21:14 AM
Hi nick65go
My preferences are to use tabs, and to place opening braces on a separate line with the same indentation as its closing brace.
Title: Re: what coding style is prefered in tinycore?
Post by: Greg Erskine on August 16, 2020, 01:53:11 PM
hi nick65go,

When I look at this:

Code: [Select]
checkbootparam(){
stringinstring " $1" "$CMDLINE"
return "$?"
}

I would add the space, then add 2 tab indents. I would also wonder if $? needs the quotes around it. Isn't $? always an integer not a string.

You are going to do you head in worrying about other people's code.

Like you I browse through Tiny Core's scripts and notice the formatting inconsistencies. I always feel like fixing them but I have learnt to control myself.  :)

tc-functions has crazy indenting, using 0,1,2,3,4 or 8 spaces.  :o

There is also another function format.

Code: [Select]
parentOf()
{
        PID=$(pidof $1) || return
        PPID=$(awk '/^PPid:/{print $2}' /proc/$PID/status)
        awk '/^Name:/{print $2}' /proc/$PPID/status
}

Generally, what about the use of  "==" instead of "=" and the use of "=" when it should be "-eq" and back ticks.

regards
Greg
Title: Re: what coding style is prefered in tinycore?
Post by: nick65go on August 16, 2020, 02:17:24 PM
Generally, what about the use of  "==" instead of "=" and the use of "=" when it should be "-eq" and back ticks.
Until now what we have in tinycore "just works". I do not know well about speed optimization of sh operations.
For this stuff maybe better info is at http://landley.net/toybox/design.html (http://landley.net/toybox/design.html)
I just made TC code more"beautiful" (with TAB indent) for me to understand it. looking in git, because there are only few new scripts, so no danger that my silly "work" will be over-written massively in a new TC version.

Title: Re: what coding style is prefered in tinycore?
Post by: Greg Erskine on August 16, 2020, 04:13:34 PM
.... until the cat walks over you keyboard.
Title: Re: what coding style is prefered in tinycore?
Post by: aus9 on August 16, 2020, 06:26:55 PM
SNIP I browse through Tiny Core's scripts and notice the formatting inconsistencies. I always feel like fixing them but I have learnt to control myself.  :) SNIP regards Greg

Greg, altho you may not have directed that comment to anyone in particular. I am willing to learn if its not beyond my payscale. Do you have any links to share or mind starting a new post to reveal all? Also some info files contain TAB indents which cause issues. And I think coders prefer indents I think to show no missing if fi sets
Code: [Select]
if blah blah
   something
     if blah blah
        something
     fi
fi
 
Title: Re: what coding style is prefered in tinycore?
Post by: Greg Erskine on August 16, 2020, 10:20:33 PM
I'm not a programmer. :( I don't have the deep understanding of programing concepts and semantics.

But when I am writing programs I like consistency. So I always use tabs for indentations, and I set a tab to equal 4 spaces.

Code: [Select]
if blah blah; then
    something
    if blah blah; then
        something
    fi
fi

It's a bit hard to show tabs in between code tags.

I have been caught out in the past of blindly "fixing things" only to find there were embedded awk scripts or python scripts within shell scripts.

I've been doing a lot of YAML stuff recently, just after I worked out how to get Visual Studio Code to work nicely with tabs, ouch!
Title: Re: what coding style is prefered in tinycore?
Post by: GNUser on August 17, 2020, 06:04:43 AM
Hi, nick65go. Coding style is a big topic. Two main elements are formatting (indent style and spaces vs. tabs) and readability.

There are many indent styles. K&R and Allman are the two most popular. I personally prefer Allman. See here for examples of 7 indent styles:
http://www.terminally-incoherent.com/blog/2009/04/10/the-only-correct-indent-style/

Spaces vs. tabs is a big flame war. I personally prefer tabs set to 4 spaces, but some people think that tabs are an abomination.

As for readability, code is written once but--depending on the code, licensing, and how many users/contributors the project has--the code can be read and edited thousands of times. Books could be (and have been) written about readability. I think it's safe to say that it encompasses knowing when to use comments (i.e., don't comment on things that are obvious but do comment on anything that another person--or you 6 months after writing the code--might be confused about) and writing code that's sufficiently explicit without being too verbose. Writing readable code is an art and it seems that one is never quite done learning how to do it better.

I think your question was specifically about formatting. This is true both for TCL as well as any other  FOSS project: If you intend to submit a patch for a file, it is best to use the formatting (indent style and spaces vs. tabs) that's being used in that file. Even if there are no spaces where you'd expect to see some, stick with the formatting used in the file. If the file is inconsistent (tsk, tsk!), then I guess I'd use the formatting that's most prevalent in the file. As for formatting your own projects, the most important thing is to be consistent.

I hope that's helpful or at least interesting :)
Title: Re: what coding style is prefered in tinycore?
Post by: nick65go on August 17, 2020, 02:35:35 PM
Hi GNUser. Yes, it was both helpfully and interesting. I did not expect this to be like a can of worms. Thank you for the tips ;)
Title: Re: what coding style is prefered in tinycore?
Post by: Rich on August 17, 2020, 06:19:04 PM
Hi GNUser
I didn't realize there were so many named coding styles, or that they were even named. So I basically use the Allman style. However, I'm
not as generous with adding spaces around keywords and some operators (if, while, for , =, etc.):
Code: [Select]
int remainder;

while(Numerator != 0)
{
remainder=Denominator % Numerator;
Denominator=Numerator;
Numerator=remainder;
}
return(Denominator);
Title: Re: what coding style is prefered in tinycore?
Post by: Paul_123 on August 17, 2020, 07:48:58 PM
I’m a K&R guy, although never knew it had a name.  Allman wastes too much vertical space.

But I agree with keeping the same format as the original code was written.
Title: Re: what coding style is prefered in tinycore?
Post by: Rich on August 17, 2020, 10:01:29 PM
Hi Paul_123
... Allman wastes too much vertical space. ...
That might have been a valid argument back when displays were limited to 24 or 25 lines, but it's pretty lame by todays standards.
I find it very distracting trying to match opening braces to the right of the page with closing braces to the left of the page. Constantly
having to scan side to side makes it difficult for me to focus when examining someone else's code.
Title: Re: what coding style is prefered in tinycore?
Post by: ladnar on September 28, 2020, 07:06:41 PM
As a newbie and a biologist, I have no right to offer an opinion. But just in case you didn't know, FYI, there was a whole episode or two of the HomeBoxOffice series, "Silicon Valley", devoted to this "flame war"; this "running gag". I AM trying to learn a bit more about coding, but I am going to read all the above, only half-seriously.
Title: Re: what coding style is prefered in tinycore?
Post by: ferran on November 14, 2020, 10:38:56 AM
Didn't you know me yet? I'm Tab-man 8)

Code: [Select]
[Requirements]
[Definitions]
[Settings]
    [Flow algorithmic instructions]
       [Flow algorithmic instructions (nested)]
            [evaluation process]
            [I/O process]
      [end flow]
   [end flow]
[final & exit]
[Functions]
Title: Re: what coding style is prefered in tinycore?
Post by: Greg Erskine on November 14, 2020, 11:30:41 AM
Your tab spacings in the above example shows 3, 4 or 5 spaces?
Title: Re: what coding style is prefered in tinycore?
Post by: andyj on November 14, 2020, 01:13:08 PM
Asking about style is like asking which is the best dessert. It's a subjective question so just go for readability and more importantly consistency.
Title: Re: what coding style is prefered in tinycore?
Post by: ferran on November 14, 2020, 01:37:21 PM
Your tab spacings in the above example shows 3, 4 or 5 spaces?
I always configure the editor to 4 spaces per tab, in my opinion it's enough to see a fine structured coding.
Title: Re: what coding style is prefered in tinycore?
Post by: Rich on November 14, 2020, 01:47:50 PM
Greg Erskine
Your tab spacings in the above example shows 3, 4 or 5 spaces?
When entering text between code tags the font is proportional. If you look at alignment instead of counting spaces, you
will get it wrong. When you click the  Post  or  Preview  button, the text between the code tags gets displayed using a
fixed font, and will look correct if you counted spaces. I'm guessing ferran looked at alignment.
Title: Re: what coding style is prefered in tinycore?
Post by: Rich on November 14, 2020, 01:49:25 PM
Hi ferran
4 spaces per tab is my preference too.
Title: Re: what coding style is prefered in tinycore?
Post by: dentonlt on December 21, 2020, 01:56:57 AM
Asking about style is like asking which is the best dessert. It's a subjective question so just go for readability and more importantly consistency.

Hahahahahah - but you're the only one that has to eat your dessert  ;D

I take spaces over tabs since the latter vary per software/editor/user, but maybe that's equal justification for tabs over spaces ... using spaces takes control from the recipient. Hmm ... maybe I should use tabs in the future ...

If I had to choose tab size, I'd take only two spaces. I think that's what I did in submitqc. I think.  ???