Mailinglist Archive: yast-devel (144 mails)

< Previous Next >
Re: [yast-devel] Simple example of refactoring
# jreidinger@xxxxxxx / 2013-08-21 09:38:08 +0200:
On Tue, 20 Aug 2013 12:48:26 +0200
Lukas Ocilka <lukas.ocilka@xxxxxxxx> wrote:

On 08/19/2013 05:19 PM, Josef Reidinger wrote:
Hi,
because we have now ruby I want to show really simple way how we can
refactor existing code. I choose just simple example to be easy to
understand and on other hand full working. [1]

There is two interesting points in it.
1) using minitest for testing, its structure ( so you can imagine
how it looks ) and how it can be easy integrated with automake
( nice part is that if you run `make check` then both testsuite is
run, old and new one
2) Using ruby builtin regex, so we no longer need our own.

Just to add some more info on this refactoring piece. Josef has used
a standard Ruby Resolv [1] class to replace our built-in
functionality. This will save our time in the future as we don't need
to care about our own implementation and saves some space as well as
our implementation was a duplicate to standard Ruby library which
will be installed anyway.

I like the idea and I expect that you will identify and replace
more ;) This is actually something that anyone with a basic Ruby
knowledge can do. It's could even be marked as, so called, junior job.


Well, it is questionable if it is junior job, as there can be small
differences between ruby and our implementation, so it needs a lot of
testing of corner cases and good knowledge both implementations if
there is differences.

it's not exactly a groundbreaking job, and anybody doing it will need to
know (through tests) behavior of both implementations.

And to be honest in this change I heavily believe in tests because
compare these two regexp is too much for my brain :) [1][2]

expanding the quantifiers into branches, we get

resolv.rb:

/0
|1
|1[0-9]
|1[0-9][0-9]
|2
|2[0-4]
|2[0-4][0-9]
|25
|25[0-5]
|2[6-9]
|[3-9]
|[3-9][0-9]
/x

IP.rb:

/[0-9]
|[1-9][0-9]
|1[0-9][0-9]
|2[0-4][0-9]
|25[0-5]
/x

with a bit of shuffling around, resolv.rb version becomes

/0
|1
|2
|[3-9]
|1[0-9]
|2[0-4]
|25
|2[6-9]
|[3-9][0-9]
|1[0-9][0-9]
|2[0-4][0-9]
|25[0-5]
/x

which reduces to

/[0-9]
|[1-9][0-9]
|1[0-9][0-9]
|2[0-4][0-9]
|25[0-5]
/x

=> they're the same.

[1] https://github.com/ruby/ruby/blob/ruby_2_0_0/lib/resolv.rb#L2172
[2]
https://github.com/yast/yast-yast2/blob/0e08c9b93c2c730e79ea36451ea715a5746dc5b2/library/types/src/modules/IP.rb#L63

--
roman
--
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: yast-devel+owner@xxxxxxxxxxxx

< Previous Next >