Re: upper/lower case

"Daniel Goldman [email protected] [sed-users]" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
Anyone who finds this off topic or over-long, please ignore...

In response to your question about "obscurity index", I would say the 
code scores maybe 8/10, higher being worse.

Of course, you have the right to think the code is not obscure, and to 
code however you choose. Since the code is clear to you, perhaps no 
problem would arise. However, I believe if someone else, even someone 
very familiar with sed (like me), had to maintain or fix what you had 
done, it would be a problem for them.

Of course, code readability is not an exact science. If you have not 
already done so, I would suggest you read (several times) what "Code 
Complete" first edition has to say about code readability. If you have 
read it, maybe read it again.

You asked for my reasons. All right. The script has two major 
constructs, and both have a "jumble of stuff" and seem confusing.

#!/bin/sh -u

  c=3;
  d=\;;

  code="
     ${c}{
        s/.*/\L&/;
        s/\<[a-z]/\u&/g;
     }
     H;1h;\$!d;
     g;y/\\n/$d/;
  "

  flag=
  while IFS= read -r Line; do
     case $flag in '' ) printf '%s\n' "$Line"; flag=1; continue;; esac;
     set -f;
     IFS=$d; set X $Line; shift;
     printf '%s\n' "$@" | sed -e "$code";
  done < input.csv

#1 - code script - If you asked 100 seders what the "code" script does, 
I think very few would be able to quickly answer. In other words, what 
exact transformation does "code" carry out? In my estimation, the great 
majority of seders could only figure it out after tedious analysis, if 
even then. It's hard to understand because it packs so many (I believe 
seven) sed commands into one script, including three "tricky" hold space 
commands. Now, if say 90% of people on the group can glance at the 
"code" variable, *** without previous knowledge ***, and quickly tell 
exactly what it does, I'm wrong.

#2 - while loop - If you asked 100 shell scripters what the while loop 
does, exactly what the logic is, including the tricky code script, I'm 
pretty sure few could give a correct answer. Limited to the loop, there 
are many confusing items that most shell scripters don't commonly use 
within a while loop, such as "read -r", "IFS=", "set X", "set -f", 
"shift", etc. I'm not saying these are grammatically wrong, they are 
just not commonly seen, and the accumulation of all these somewhat 
uncommon constructs makes it hard to figure out what the loop does. 
Again, if say 90% of people on the group can glance at the while loop, 
*** without previous knowledge ***, and quickly tell exactly what it 
does, I'm wrong.

There are other minor confusions that raise red flags and make the code 
a little more obscure. *** For example, you use ${c} curly brace var 
syntax immediately before the sed {} curly brace expression. It throws 
the reader off stride, slows the reader down, confuses the reader. Is 
the {} a shell variable (YES), or something related to sed? Why not just 
say $c instead? *** Another example, the script starts with "sh -u", 
whatever "-u" means here, I don't know. The reader is left guessing. *** 
Another minor thing is the cryptic $d and $c vars. Again, refer to Code 
Complete - $delim and $col would be better. *** I know you were trying 
to protect against yahoo groups glomming lines, but you know shell 
script lines are not normally written ending with semicolon, it's just 
another distraction for the reader, it all adds up, the sum of a lot of 
little obscurities is overall obscure code.

Of course, obscurity is relative. Look at the cut-paste example. Maybe 
3/10 obscurity? It's about the same length. I believe each step is 
clear. The only semblance of a jumble, and the most difficult section is 
the sed script. But it only has two commands (not seven), so is much 
easier to understand.

Of course, the cut-paste script requires a basic knowledge of cut and 
paste. However, I think those are so generally useful that any decent 
shell scripter should know them. Also, they are used in a very standard 
way. But perhaps I'm wrong, perhaps cut and paste are known by few. If 
that is the case, that would increase the "obscurity index".

-----------
input_file=$1
col_to_sed=$2

prev_col=`expr $col_to_sed - 1`
next_col=`expr $col_to_sed + 1`

cut -d ";" -f $col_to_sed $input_file > old_col.txt
cut -d ";" -f 1-$prev_col $input_file > cols_before.txt
cut -d ";" -f $next_col- $input_file > cols_after.txt

sed "1! {s/.*/\L&/; s/\<./\u&/g}" < old_col.txt > new_col.txt

paste -d ";" cols_before.txt new_col.txt cols_after.txt
----------

Concerning the lack of comments in your code that you asked about, that 
has little or nothing to do with my opinion. If you have to comment 
code, that's a bad sign. Again, from "Code Complete" - Don't comment 
"tricky code". Instead, make it simpler and self-commenting.

Yes, I realize the script was not for speed reasons. And again, like 
Thierry, I applaud the technical aspects.

Anyway, I've gone out on a limb trying to provide honest feedback you 
requested... Please don't chop off the limb. :) A key attribute of any 
really good programmer is to take criticism, be self-critical, and keep 
learning all the time. I learn from this group, including from you.

Daniel

On 12/12/2015 9:41 PM, [email protected] [sed-users] wrote:
> Can you please lay out the reasons you thought that my posting scores on the obscurity index. Could it be that there were no accompanying comments? Also , this post was not for speed reasons, rather to ameliorate the while-loop construct to be able to skip processing the first line of input.
>
>   >
>   >
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.