LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 01-05-2022, 01:46 PM   #1
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Seeking advice from old friends here


Hello to all friendly Slackers, whether you remember me, or not. I'm writing a new project and had already asked elsewhere for testers/bug-hunters.

It IS a pretty crazy idea and implementation, so of course I got the Why this?, Have you tried That? Why in That language?, etc. And then they started looking at the code that typically looks something like this:
Code:
# tsst  - numeric comparison of decimal numbers using shell 'test' syntax
# depends on: 'cmp3w'
tsst() { case $1 in ''|-h|-s*) tsst_help >&2 ; return 1 ;;esac
    tstret=$( cmp3w $1 $3 )
    case $2 in
        '-lt') [ "$tstret" = '<' ] ; return $? ;;
        '-le')  case $tstret in '<'|'=') return 0 ;; *) return 1 ;;esac ;;
        '-eq') [ "$tstret" = '=' ] ; return $? ;;
        '-ge')  case $tstret in '>'|'=') return 0 ;; *) return 1 ;;esac ;;
        '-gt') [ "$tstret" = '>' ] ; return $? ;;
        '-ne')  case $tstret in '=') return 1 ;; *) return 0 ;;esac ;;
    esac
} ## tsst
Usually it has at least Bread-crumb comments in functions, but not here.

So, the first guy says:
Quote:
Your code is too dense -all squished together!
So, I do this:
Code:
tsst () 
{ 
    case $1 in 
        '' | -h | -s*)
            tsst_help 1>&2;
            return 1
        ;;
    esac;
    case $2 in 
        -lt | -le | -eq | -ge | -gt | -ne)
            :
        ;;
        *)
            echo "tsst: Invalid operator '$2'" 1>&2;
            return 1
        ;;
    esac;
    tstret=$( cmp3w $1 $3 );
    case $2 in 
        '-lt')
            [ "$tstret" = '<' ];
            return $?
        ;;
        '-le')
            case $tstret in 
                '<' | '=')
                    return 0
                ;;
                *)
                    return 1
                ;;
            esac
        ;;
        '-eq')
            [ "$tstret" = '=' ];
            return $?
        ;;
        '-ge')
            case $tstret in 
                '>' | '=')
                    return 0
                ;;
                *)
                    return 1
                ;;
            esac
        ;;
        '-gt')
            [ "$tstret" = '>' ];
            return $?
        ;;
        '-ne')
            case $tstret in 
                '=')
                    return 1
                ;;
                *)
                    return 0
                ;;
            esac
        ;;
    esac
}
And then the second guy says:
Quote:
Dude! That's way too much whitespace!
So then I do this:
Code:
tsst ()
{
case $1 in
'' | -h | -s*)
tsst_help 1>&2;
return 1
;;
esac;
case $2 in
-lt | -le | -eq | -ge | -gt | -ne)
:
;;
*)
echo "tsst: Invalid operator '$2'" 1>&2;
return 1
;;
esac;
tstret=$( cmp3w $1 $3 );
case $2 in
'-lt')
[ "$tstret" = '<' ];
return $?
;;
'-le')
case $tstret in
'<' | '=')
return 0
;;
*)
return 1
;;
esac
;;
'-eq')
[ "$tstret" = '=' ];
return $?
;;
'-ge')
case $tstret in
'>' | '=')
return 0
;;
*)
return 1
;;
esac
;;
'-gt')
[ "$tstret" = '>' ];
return $?
;;
'-ne')
case $tstret in
'=')
return 1
;;
*)
return 0
;;
esac
;;
esac
}
Then the third guy -veerrry smart this one- he says:
Quote:
Actually, my fine fellow, at least on the page, that last example shows _rather_ the same amount of white space!
By this time, it's getting tough to find a middle ground, so I do this:
Code:
tsst ();{;case $1 in;'' | -h | -s*);tsst_help 1>&2;;return 1;;;;esac;;case $2 in;-lt | -le | -eq | -ge | -gt | -ne);:;;;;*);echo "tsst: Invalid operator '$2'" 1>&2;;return 1;;;;esac;;tstret=$( cmp3w $1 $3 );;case $2 in;'-lt');[ "$tstret" = '<' ];;return $?;;;;'-le');case $tstret in;'<' | '=');return 0;;;;*);return 1;;;;esac;;;;'-eq');[ "$tstret" = '=' ];;return $?;;;;'-ge');case $tstret in;'>' | '=');return 0;;;;*);return 1;;;;esac;;;;'-gt');[ "$tstret" = '>' ];;return $?;;;;'-ne');case $tstret in;'=');return 1;;;;*);return 0;;;;esac;;;;esac;};
The question is this: What would you do with so much eeeexxcellent advice, and which coding style did you find easiest to read and understand?
 
Old 01-05-2022, 02:27 PM   #2
z80
Member
 
Registered: Jul 2019
Location: Europe
Distribution: Slackware64-current
Posts: 136

Rep: Reputation: 99
Quote:
Originally Posted by gnashley View Post
The question is this: What would you do with so much eeeexxcellent advice,
ignore it - it's your project to you define the rules / coding style

Quote:
and which coding style did you find easiest to read and understand?
#1 - but my opinion doesn't matter (see above)
 
6 members found this post helpful.
Old 01-05-2022, 02:33 PM   #3
Jan K.
Member
 
Registered: Apr 2019
Location: Esbjerg
Distribution: Windows 7...
Posts: 773

Rep: Reputation: 489Reputation: 489Reputation: 489Reputation: 489Reputation: 489
In the previous millenium writing tons of AutoCAD Lisp code, I learned there's no such thing as too much whitespace...

And pretty code is easier to read in a glance...

Comments were also found to be _very_ important...


Your three "per viewers" could all be said to be correct. Give them a friendly middle finger salute and do as you please and like...


Is your project public available?
 
3 members found this post helpful.
Old 01-05-2022, 02:34 PM   #4
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Hey gnashley !

What z80 said

-- kjh
 
Old 01-05-2022, 03:06 PM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Original Poster
Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
So, Jan K., you liked example #2 best. That's a bash 'declare - tsst' which shows the internal storage format of a function in bash. I wondered, once, if saving them pre-formatted would make them run faster, but of course it did not.
Thanks, kjh!
 
1 members found this post helpful.
Old 01-05-2022, 03:43 PM   #6
Ilgar
Senior Member
 
Registered: Jan 2005
Location: Istanbul, Turkey
Distribution: Slackware64 15.0, Slackwarearm 14.2
Posts: 1,157

Rep: Reputation: 237Reputation: 237Reputation: 237
Hi, gnashley (I do remember you ). #2 looks best to me, but #1 wasn't bad either, only the "case $1..." line looked a bit crowded. I'm a C/Rust person, so I am more inclined to like the indentation practices in those languages. As the others said, it is your project, and I think you should use what is most comfortable for you. But if you expect others to contribute to or maintain the code in the long run, it might be a good idea to choose a style that makes the others feel comfortable, too.
 
Old 01-05-2022, 04:30 PM   #7
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,147
Blog Entries: 6

Rep: Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834
White space with remarks is a good thing.

Code:
#function that does something
funct() {
    #This does something
    echo "Hi"
    
    #This does something
    echo "There"
}
Especially if you stop working on the project and come back to it in a month.
 
1 members found this post helpful.
Old 01-05-2022, 05:48 PM   #8
cowpoke
LQ Newbie
 
Registered: Dec 2018
Distribution: Slackware
Posts: 17

Rep: Reputation: Disabled
My vote is for code block #1.

The condition "siblings" are all under the same glance. Additionally, you don't have to look far to compare the outcomes.

My colleagues enjoy getting into these exercises and some of us have many more years worth of opinion to throw around. Personally coming from a good chunk of COBOL work, I really appreciate minimizing eye travel. I feel like it's easier on my dusty short-term memory.
 
Old 01-05-2022, 06:48 PM   #9
Jan K.
Member
 
Registered: Apr 2019
Location: Esbjerg
Distribution: Windows 7...
Posts: 773

Rep: Reputation: 489Reputation: 489Reputation: 489Reputation: 489Reputation: 489
Quote:
Originally Posted by gnashley View Post
So, Jan K., you liked example #2 best. That's a bash 'declare - tsst' which shows the internal storage format of a function in bash...
Yeah maybe, but #1 *is* nice and sleak...


But it might as well be egyptian. Do recognize "something" but bash is unknown territory...
 
Old 01-05-2022, 07:08 PM   #10
mlangdn
Senior Member
 
Registered: Mar 2005
Location: Kentucky
Distribution: Slackware64-current
Posts: 1,845

Rep: Reputation: 452Reputation: 452Reputation: 452Reputation: 452Reputation: 452
Absolutely remember, and I still use src2pkg, albeit more rarely now.

That said, I go with Block #1
I am not a code writer, but I like the linear relationship of the code. Don't have my eyes dancing all over the place. But still, do what YOU like!
 
Old 01-06-2022, 02:33 AM   #11
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Original Poster
Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Jan K. The project was on github til days ago, but I removed the repo in order to start over.
cowpoke -with a handle like that, you probably come from a lonely place near where I grew up...
For those who remember src2pkg, now in advanced bit-rot, if you ever had a peek at the code(of course you did), you'll find my recent work refreshing.

teckk, ummm you should have remembered the code in example #1, since you happen to be 'guy #1' -you said this:
Quote:
1000 lines of bash all squished together
Apparently you found the shebang too confusing, since it says '#!/bin/sh' and not '#!/bin/bash' and I had explicitly said that it was posix-shell language. Do you have another mode you can run in?

Back to the subject -I admit that I've already decided how to format my code, so that what the reader might want to understand, is commented and just roomy enough to expose the meat of the function. And actually, since it is a script, all unneeded characters do cost something to process -even white space and quote-marks. I just wanted to test the waters a bit here before announcing anything. Recently I've noticed a wanna-be Slacker around here terrorizing even those whose projects are in the standard Slackware software selection. He hasn't shown up here yet, but I expect him as soon as I create a thread with 'testers wanted' in the title. And I was also wondering if we'd get to post #5 without someone bringing up the 'Icon Issues' in my profile. This is Day #2 and those things have not happened, so the future looks bright...

I'll start a new thread within a couple of days, and simply attach the script which currently is:
Code:
amigo@HP:$ ../count_code_lines iq_1.71.sh
Code lines=477
Comment lines=217
Blank lines=48
Total lines=742
 
Old 01-06-2022, 02:55 AM   #12
marav
LQ Sage
 
Registered: Sep 2018
Location: Gironde
Distribution: Slackware
Posts: 5,421

Rep: Reputation: 4160Reputation: 4160Reputation: 4160Reputation: 4160Reputation: 4160Reputation: 4160Reputation: 4160Reputation: 4160Reputation: 4160Reputation: 4160Reputation: 4160
Hi,

I for one, I always heavily indent my bash codes (for readability)
More or less like your #2

but as others have said, it's a very personal way of scripting, so it's up to you.

Note: I put this in my .vimrc
Code:
autocmd FileType sh setlocal et ts=2 ai sw=2 nu sts=0
autocmd FileType bash setlocal et ts=2 ai sw=2 nu sts=0

Last edited by marav; 01-06-2022 at 03:01 AM.
 
Old 01-06-2022, 05:57 AM   #13
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Original Poster
Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
As mentioned, white space, quote marks -in a script every character slows it down. Once I tried a style-suggestion from shellcheck about double-quoting some vars -a construct I was using throughout- anyway, the startup latency went up by 5X -just for about 1K worth of "".

Since Irony has already visited this thread: After the quoted response above, from Guy#1, I thought Oh boy! here we go. And then the discussion fell into arguments about what "posix-compliant" consists of, and suggestions of cleaner/faster(sic) alternate usages -everyone of them a bashism, mind you this on the LQ Programming forum.

Anyway, I decided to ask for the same help on another forum where -let's put it plainly- everyone is _always_ root.
I still had not written in any idiot/cat-checking clauses, but was having trouble duplicating rare errors when operating on numbers above 20-digits -the project is a calculator. So, I posed the same simple question -Can you get wrong answers from this with sane inputs. You'd think that people who are running as 'root' would have no interest in trying dumb stuff, but righ away I got this, from Guy#1b:
Code:
# why does 'div bla t/ blad' return this:
blad.0
I kindly pointed out that he could not prove that the answer was incorrect, after all, the 'stupid' program got these all correctly:
Code:
div 0 / bla   = 0.0 
div bla / 0   = division by zero error
div bla / 1   = bla
div bla / bla = 1.0
All perfectly correct, no? Then, unsurprisingly, said thread degenerates into such:
I have no idea what this does or how it does it, but this could be speeded 100X up by using:
'bashism' instead of 'my construct'. Two weeks later, Guy#1b is already looking to do a hostile-fork and take credit for my nearly two years of work -writing a calculator in pure-shell is no piece of cake -especially when you've been retired 5 years from coding, and haven't had a math class in 45 years.

And I had an even steeper learning curve because my goal was to be able to solve logistic-sigmoid equations. Basically I needed to solve: e^(-x.xxxx) and had never heard of Euler and had no idea what a logarithm was. By now, Said Calculator has an advanced module which can solve problems like this: 0.014^10000000 I give that specific example because ^10,000,000 was where the Casio online calculator would crash my browser.

So I asked the guys on the Programming forum if they could verify the answer. It took them awhile to find _any_ way to confirm that answer with one of perl, awk, python or whatever. They also tried Galculate and others. Meanwhile I had ratcheted-up the problem to ^10,000,000,000 -and beyond! They spent two days trying various bignum libraries for perl, awk and python, before finally finding something that worked -and the answer was correct. They were a bit hot under the collar by then, so I didn't point out that my 'pow' would only fail at:
X^10,000,000,000,000,000,000,000
and that a 3-line fix should let it do: X^trillion-digit-exponent -but who needs to go there and who would wait for such an answer?? My 'epow' solves X^10million in .3 seconds -if you ask for output in scientific notation. Using SigFig scaling would take a very long time -the calculation is done in the same time, but concatenating and printing all those leading zeros -18538500 exactly, would take quite a while.

Last edited by gnashley; 01-06-2022 at 06:33 AM.
 
Old 01-06-2022, 07:46 AM   #14
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
I prefer the way as per the comment from teckk
>> White space with remarks is a good thing.
I used to write the coding:
New joinee can use it, when I resign current job.

Sample comments:
Code:
#!/bin/bash
#This script is a sample script for sharing my comments.
#Before defininig the function unalias any such name.
unalias RequiredFunction >/dev/null 2>&1
#Unsdefine RequiredFunction function which will over write the same function if this function used by other scripting.
unset -f RequiredFunction
RequiredFunction ()
{
	if [ "-bash" = "$0" ]
	then
		echo "Executing this script inside current shell not allowed"
		Ret=1
		return $Ret
	fi
	if [ 0 -eq $# ]
	then
		echo handle usage exceptions
		Ret=1
		return $Ret
	fi
	# Handle parameter using if/fi or case/esac having related tabs.
	echo "$@"
	#I used to save $? to a variable and I prefer return $Ret
	# Handle the value of $0 being -bash/bash/... based on OS to prevent current user using:
	# . ./scriptname.sh
	# => Executing script at current shell not allowed.
	# use export variable if I need it using gawk.exe/awk using ENVIRON["myvariable"];
	Ret=$?
	return $Ret
}
# I feel to use % when placing the cursor at { or } to go to beginning/end of blocks.
# It can be used to use copy/paste blocks from current file to other file or inside the same file using markers.
RequiredFunction $@
Ret=$?
if [ 0 -eq $Ret ]
then
	echo "Before_a unset Ret [$Ret]"
	#Always unset the variables used inside the script
	unset Ret
	echo "After_a  unset Ret [$Ret]"
	# unset myvariable when scripting exiting any where.
else
	echo "Before_b unset Ret [$Ret]"
	#Always unset the variables used inside the script
	unset Ret
	echo "After_b  unset Ret [$Ret]"
	# unset myvariable when scripting exiting any where.
fi
 
Old 01-06-2022, 08:18 AM   #15
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,147
Blog Entries: 6

Rep: Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834
I assumed the question was about readability of code. By someone else, and by the author, if you have left it for a month or two and then came back to it.

Both of these do the same thing. Which one is easier to understand.

Code:
log_time=120
counter() {
    sleep_time=("$log_time")
    while [ "$sleep_time" -gt 0 ]; do echo -ne "x to stop or Update in "$sleep_time" seconds \r"
        read -t 1 -s -n 1 key
        if [ "$key" = "x" ]; then sleep_time=("$log_time"); clear && "$0" && exit; fi
        sleep_time=$(($sleep_time - 1)); 
    done
}
Code:
#Retrieve interval for log 
log_time=120

#Counter for next log display, or press x to restart script
counter() {
    #Reset time for counter
    sleep_time=("$log_time")
    
    #Do until counter runs out
    while [ "$sleep_time" -gt 0 ]; do
        echo -ne "x to stop or Update in "$sleep_time" seconds \r"
        
        #Look for key press every second
        read -t 1 -s -n 1 key
        
        #Look for x key to reset
        if [ "$key" = "x" ]; then
            sleep_time=("$log_time")
            clear && "$0" && exit
        fi
        
        #Decrease counter by one
        sleep_time=$(($sleep_time - 1))
    done
}
Write it however you want it.
 
  


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
LXer: Public warning: Windows 10 will share your Wi-Fi password with your friends' friends LXer Syndicated Linux News 0 07-01-2015 12:33 AM
Hello,everybody.I'm glad to make friends with you here. lalahu007 LinuxQuestions.org Member Intro 1 07-16-2009 06:08 AM
Price check from my friends here at LinuxQuestions... ooagentbender General 7 08-08-2004 02:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 01:35 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