Bad Coding Habits?
|
Author | Content |
---|---|
rht Apr 01, 2010 7:57 PM EDT |
Perhaps it's because I am self taught that I peruse all scripting HOWTOs to pick up tips on both coding and style. I am a little concerned about some of the style points used by the author in a lesson obviously aimed at newbies. 1. Variables should always be defined without anything on either side of the"=", yet this author often takes a newline. Also, I always quote variables, even if they are a number or the output of a command. 2. I like to put stuff echoed in quotes: it's good practice. 3. The conditional "if" often takes the "then" on the same line. Newbies should be advised that this is acceptable (and usual) practice. 4. But, most importantly, the author has failed to explain how the conditional works (viz, by checking exit status). If he had done so, he could neatly have transitioned from (a) grep, followed by (b) test exit status to simply "if $(grep -q ...); then". An explanation that the "q" switch to grep changed the function of grep's exit status would then have made sense, and would have introduced a better style of coding. Or am I nitpicking? |
dinotrac Apr 01, 2010 9:15 PM EDT |
rht -- The script jarred me. I didn't even think that the code in the example would work. I thought (and it seems to work that way on my box) that, instead of setting ADMIN to the email address, it set ADMIN to ''. Unless, I guess, there is a missing line continuation... I didn't see one, but it might have gotten swallowed. |
gus3 Apr 01, 2010 11:13 PM EDT |
The value of ADMIN is being hidden under JavaScript. |
dinotrac Apr 02, 2010 1:11 AM EDT |
g3 - Ahhhhhhhhhh! |
TxtEdMacs Apr 02, 2010 11:02 AM EDT |
rht, [serious] Quoting: 1. Variables should always be defined without anything on either side of the"=" [...]Do you really mean what this says? Do you mean typing? While I have not done much scripting in the shell language other, better scripting tools* certainly put content after the equals sign as an assignment of value [and can even set the data type with the use of quotes or their absence]. So perhaps you should consider writing an article on LXer or on your site to elucidate the proper coding style and rules of shell scripting. I would certainly read your articles on the topic. Txt. * e.g. perl, python or php [/serious] |
phsolide Apr 02, 2010 5:54 PM EDT |
Unix/Linux shells are unique in that you *can't* have white space between variable name, '=' sign, and value: SHVAR="some dern string" # No spaces before or after the '='. I believe that's what rht attempted to say. SHVAR = "some dern string" Just won't work. I think this arises from the shell parsing lines into tokens based on whitespace. If it parses a line into more than one token, it tries to run "SHVAR" as a command, not assign a value to it. |
TxtEdMacs Apr 02, 2010 6:13 PM EDT |
phsolide [serious] Thank You! I thought I was hallucinating or even more ignorant than I suspected. Txt. [/serious] |
rht Apr 02, 2010 7:30 PM EDT |
@phsolide
@TxtEdMacs TxtEdMacs is quite correct. I did mean no whitespace on either side of the "=". Thnx for pointing that out. However, on reflection, I think that so far as the author of the article is concerned my "uninterpreted" comment is probably valid. As for coding style, I have a simple two-part test: In six months time will I be able to read my script in one pass; and will anyone else be able to read it at all. A good test of "coding style" is: Write an eight-deep nested conditional (with loops) in awk. If you can read it when you have finished debugging it then you have good coding style. |
TxtEdMacs Apr 02, 2010 8:09 PM EDT |
rht, [serious] You mixed us up, I was the confused one, it was phsolide that explained your intent. Txt. [/serious] P.S. Something is badly skewed when I am forced to write serious notes. |
Posting in this forum is limited to members of the group: [ForumMods, SITEADMINS, MEMBERS.]
Becoming a member of LXer is easy and free. Join Us!