WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: what coding style is prefered in tinycore?  (Read 605 times)

Offline nick65go

  • Sr. Member
  • ****
  • Posts: 383
what coding style is prefered in tinycore?
« 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.
« Last Edit: August 16, 2020, 06:48:59 AM by nick65go »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 8149
Re: what coding style is prefered in tinycore?
« Reply #1 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.

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 318
Re: what coding style is prefered in tinycore?
« Reply #2 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

Offline nick65go

  • Sr. Member
  • ****
  • Posts: 383
Re: what coding style is prefered in tinycore?
« Reply #3 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
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.

« Last Edit: August 16, 2020, 02:19:15 PM by nick65go »

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 318
Re: what coding style is prefered in tinycore?
« Reply #4 on: August 16, 2020, 04:13:34 PM »
.... until the cat walks over you keyboard.

aus9

  • Guest
Re: what coding style is prefered in tinycore?
« Reply #5 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
 

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 318
Re: what coding style is prefered in tinycore?
« Reply #6 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!

Offline GNUser

  • Hero Member
  • *****
  • Posts: 628
Re: what coding style is prefered in tinycore?
« Reply #7 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 :)
« Last Edit: August 17, 2020, 06:24:50 AM by GNUser »

Offline nick65go

  • Sr. Member
  • ****
  • Posts: 383
Re: what coding style is prefered in tinycore?
« Reply #8 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 ;)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 8149
Re: what coding style is prefered in tinycore?
« Reply #9 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);

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 618
Re: what coding style is prefered in tinycore?
« Reply #10 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.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 8149
Re: what coding style is prefered in tinycore?
« Reply #11 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.

Offline ladnar

  • Newbie
  • *
  • Posts: 18
Re: what coding style is prefered in tinycore?
« Reply #12 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.