#816 PR merged: Determine EFI virtual disk size automatically (take 2)

Labels: enhancement, bug, cleanup, fixed / solved / done

gozora opened issue at 2016-04-05 15:30:

Fix for manual detection of EFI virtual image (efiboot.img) size.

gozora commented at 2016-04-05 15:33:

@jsmeix your rework of function efiboot_img_size() was implemented (for me a bit harder to read, but it does its job well after all ;-)).

jsmeix commented at 2016-04-06 07:58:

@gozora
many thanks for your feedback about readability of
my efiboot_img_size code from your point of view.

Can you describe why my code is harder to read for you.

I like to provide code that is well understandable by others.

Background information:

I am very interested in comprehensibility of source code.

For me the primary purpose of source code is
to tell others what the author of the code had in mind
what the computer should do
and how and why it should do it
from the author's point of view.

This way others who read the author's code
can easily understand what the author had in mind
so that others can - as neded - easily fix issues
or adapt and enhance the author's code.

For me it is only a secondary condition that code
correctly tells the computer what to do.

If code fulfills its primary purpose (telling others
what it does and why), then even code that is
not correct can be easily corrected (by others).

In contrast if code only fulfills the secondary condition
(it only works correctly) but for others it is hard to
understand what is meant with that code, then
such code cannot be well maintained so that
in the end such code will die out (and be completely
replaced with code that can be easily maintained
by others).

gozora commented at 2016-04-06 08:17:

Don't take my self as some reference for coding style, I'm rather hobby programmer than professional developer, so I certainly lack routine in reading other peoples code and more importantly I often have trouble to understand other people intentions in code. So if something is hard to read for ME, it doesn't mean author did something wrong, it means that I need to work on my skills ;-).
C uses similar shortcuts (I think it is called ternary if) e.g. result = a > b ? x : y; but I don't like them much as they are quite unreadable (or you must boost your brain thinking what will happen in which case). I much rather like if [else if] else as they can be read as a book and you have output from somehow smooth ...

Please take all this as my personal opinion, maybe you could discuss this with more experienced programmers or maybe some who actually teaches programming, because such discussion certainly needs someone with academical/teaching skills.

gozora commented at 2016-04-06 08:21:

@jsmeix hope the pull request is OK now ;-)

jsmeix commented at 2016-04-06 09:38:

Just for the fun of it:

Lucky you! - when you only "often have trouble to understand
other people intentions in code".

Poor me! - because I almost always have trouble to understand
other people intentions in code.

Usually I do understand what code does
(i.e. the low level implementation details)
but I do not understand the reasoning behind.

E.g. what is the intention behind:

#! /bin/bash
j=0
for i in $( seq 1 2 $(( 2 * $1 - 1 )) )
do (( j += i ))
done
echo $j

gozora commented at 2016-04-06 10:22:

Well I guess you encounter much more alien code daily as I do...
Anyhow that code looks like bad joke interview question :-).

gozora commented at 2016-04-06 10:22:

Pull requested was updated.
Is that light that I see at the end of the tunnel? :-)

jsmeix commented at 2016-04-06 11:00:

With great pleasure I merge your pull request.

gozora commented at 2016-04-06 11:02:

:-)
Thanks for your help as well!

jsmeix commented at 2016-04-06 11:12:

This pull request should fix https://github.com/rear/rear/issues/810

jsmeix commented at 2016-04-06 11:16:

@gozora
only FYI:
My above code is no "bad joke interview question".
I made it myself while I wrote https://github.com/rear/rear/pull/816#issuecomment-206260967
Just try it out - it does a useful thing.

gozora commented at 2016-04-06 11:25:

It sums odd numbers, nice!
Did you use this in some project?

jsmeix commented at 2016-04-06 11:28:

That it sums odd numbers is the low level implementation detail
(i.e. what the code does) but what is the intent behind?
I.e. why should one sum odd numbers?

gozora commented at 2016-04-06 11:31:

No idea ... (except that it is homework ;-))

jsmeix commented at 2016-04-06 11:31:

I did not use it before. I made it new from scratch here while I wrote https://github.com/rear/rear/pull/816#issuecomment-206260967

jsmeix commented at 2016-04-06 11:33:

A hint why one should sum odd numbers:

1

A

1 + 3

B B
A B

1 + 3 + 5

C C C
B B C
A B C

1 + 3 + 5 + 7

D D D D
C C C D
B B C D
A B C D

gozora commented at 2016-04-06 11:51:

brute force password cracker?

gozora commented at 2016-04-06 11:55:

:-) I can see the pattern, but I can't figure out what is it good for ...

gozora commented at 2016-04-06 11:59:

or is this some strange way how to calculate power of some number?

jsmeix commented at 2016-04-06 12:01:

Yes!

The purpose is calculating the square number of its input "$1":

# cat -n ./sq.sh
     1  #! /bin/bash
     2  j=0
     3  for i in $( seq 1 2 $(( 2 * $1 - 1 )) )
     4  do (( j += i ))
     5  done
     6  echo $j
# ./sq.sh 1
1
# ./sq.sh 2
4
# ./sq.sh 3
9
# ./sq.sh 123
15129

See https://en.wikipedia.org/wiki/Square_number#Properties

gozora commented at 2016-04-06 12:18:

I didn't know about this property, it is really interesting.
Thanks for pointing out!

jsmeix commented at 2016-04-06 13:00:

You are welcome!

And finally here same functionality implemented
with meaningful code:

# cat -n ./square_number.sh
     1  #! /bin/bash
     2  # calculate the square number N^2 of its input N
     3  # by summing up the first N odd numbers 1 3 ... 2*N-1
     4  # where each nth partial sum is the nth square number
     5  # see https://en.wikipedia.org/wiki/Square_number#Properties
     6  N=$1
     7  if ! [[ $N =~ ^[0-9]+$ ]]
     8  then echo "Input must be non-negative integer." 1>&2
     9       exit 1
    10  fi
    11  square_number=0
    12  for odd_number in $( seq 1 2 $(( 2 * N - 1 )) )
    13  do (( square_number += odd_number ))
    14  done
    15  echo "$N^2 = $square_number"
# ./square_number.sh 
Input must be non-negative integer.
# ./square_number.sh 0
0^2 = 0
# ./square_number.sh 1
1^2 = 1
# ./square_number.sh 2
2^2 = 4
# ./square_number.sh 3
3^2 = 9
# ./square_number.sh 123
123^2 = 15129
# ./square_number.sh -123
Input must be non-negative integer.

[Export of Github issue for rear/rear.]