Refactoring

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2886
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Refactoring

Post by richmond62 » Sun Jan 06, 2019 10:28 am

I have a very, very clunky thing called "Devawriter Pro" which I have been developing for about 9 years.

It is clunky in these ways:

1. Orphaned code left over from earlier recensions
[Mainly focussed on the numToChar/ numToCodePoint switchover].

2. Horrible reduplication of code [think 50 buttons using 99% the same code].

3. Other "side-projects" that never got "there" but still lying around under the hood.

According to a conservative estimate I could reduce the size of my main stack by at least 50%.

Questions that inevitably pop up are:

1. Is the time that will be involved in refactoring going to be reflected in terms of a financial increase.
[This question is a load of old "sh*t" as I've never made more that $400 a year for the thing:
I just shoved it in here for completeness.]

2. How much of this can be done automatically?
[Probably in my case, NONE, as LiveCode does not understand the "joys" of
ancient Indian writing systems.]

3. How can I ensure that as I change bits of code in various places it does not have unseen and
unsuspected consequences elsewhere?

4. Version tracking? Should I build an autosave script into the stack prior to doing anything else?

5. Why am I even bothering to think about this and write these questions?

WELL, at least I can answer #5:

AND it comes in 2 parts . . . :D

Because I am well aware that if I really want to keep going with this thing [think "bottomless pit"]
the cracks are going to start appearing very, very soon if I don't address the fact that, like MacOS 9,
my current version (2.6.0) is floating around on code from version 0.1 which should have gone
the way of the woolly mammoth a long, long time ago.

People are writing all sorts of "jazzy, technical stuff" about refactoring over on the Use-List
in "jazzy, technical" language that makes me feel unco queer because it seems
(possibly unnecessarily jargonised) very difficult for "simple" folk like me to follow.

bogs
Posts: 2582
Joined: Sat Feb 25, 2017 10:45 pm

Re: Refactoring

Post by bogs » Sun Jan 06, 2019 1:39 pm

1. Is the time that will be involved in refactoring going to be reflected in terms of a financial increase.
[This question is a load of old "sh*t" as I've never made more that $400 a year for the thing:
I just shoved it in here for completeness.]
I think the financial increase would be reflected in 'dollars per hour', instead of yearly dollars. In other words, refactoring / re-writing / removing useless code, all of those things cut down on how much you have to dredge through each time, so your productivity increases compared to someone that leaves all that cruft in, so you accomplish more when you sit down to code.

If your doing in 10 minutes what used to take 12, you've increased your per hour financial gain.
2. How much of this can be done automatically?
Depends on how good you are at writing tools, or whether you have the tools available. Things like finding duplicate code, well finding it is easy. Telling whether it is needed in the entire context of the program is a little trickier. It would also depend on how comfortable you are with the IDEs built in tools for this, like the profiler, or some of the tools made by others.
3. How can I ensure that as I change bits of code in various places it does not have unseen and
unsuspected consequences elsewhere?
Well, there are lots of ways to do that. I use both styles of comments myself. For a code change, I use the block comments to wrap the date of the change and the previous code. Once it is commented, you are sure going to know if you haven't been using it for a while.

A side benefit is that it indicates the biggest problem areas in what your trying to accomplish.
4. Version tracking? Should I build an autosave script into the stack prior to doing anything else?
I think so. I rarely only ever have one revision of a program in storage.

Hope that helps a little :D
Image

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2886
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: Refactoring

Post by richmond62 » Sun Jan 06, 2019 6:39 pm

Thanks "bogs": quite a lot of useful stuff there.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4756
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Refactoring

Post by jacque » Sun Jan 06, 2019 6:49 pm

It's always wise to simplify as much as possible, if for no other reason than just to save your own sanity later.

The 50 buttons need to use a behavior, which you can automate. Put the script into a hidden button and use that as the script storage container. Write a temporary handler that loops though all the buttons and sets their script to empty, and sets their behavior to the long ID of the hidden button. There should be no repercussions from that change.

If you need to see if a handler is unused and superfluous, put a breakpoint in it, work with the stack normally, and see if it triggers. Or just use the Find and Replace dialog to do a stack-wide search for the handler name and see if it occurs anywhere outside of itself.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2886
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: Refactoring

Post by richmond62 » Sun Jan 06, 2019 7:32 pm

I am trying to work out what is the difference between Refactoring
and just rewriting some code so it is more efficient.

bogs
Posts: 2582
Joined: Sat Feb 25, 2017 10:45 pm

Re: Refactoring

Post by bogs » Sun Jan 06, 2019 10:46 pm

The difference is you can use one word, 'refactoring', to replace 8 words, 'rewriting some code so it is more efficient'.

It probably comes out to a lot more words, since the code that results from refactoring is (usually) more condensed as well. From someone a lot smarter than I ~
https://en.wikipedia.org/wiki/Code_refactoring wrote: Code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior. Refactoring is intended to improve nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity; these can improve source-code maintainability and create a more expressive internal architecture or object model to improve extensibility.

Typically, refactoring applies a series of standardised basic micro-refactorings, each of which is (usually) a tiny change in a computer program's source code that either preserves the behaviour of the software, or at least does not modify its conformance to functional requirements. Many development environments provide automated support for performing the mechanical aspects of these basic refactorings. If done extremely well, code refactoring may help software developers discover and fix hidden or dormant bugs or vulnerabilities in the system by simplifying the underlying logic and eliminating unnecessary levels of complexity. If done poorly it may fail the requirement that external functionality not be changed, introduce new bugs, or both.

By continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you'll find that it is easier to extend and maintain code.
— Joshua Kerievsky, Refactoring to Patterns[1]
Image

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2886
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: Refactoring

Post by richmond62 » Sun Jan 06, 2019 11:00 pm

Yes: I read the Wikipedia stuff as well. 8)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2886
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: Refactoring

Post by richmond62 » Sat Jan 12, 2019 5:22 pm

AUTOSAVE seems to be a "pain in the bum" on Macintosh.

LC 8.1.10, macOS 10.14.3 beta 4

This:
on openStack
set the defaultFolder to "/Volumes/X1/DWP autosaves"
backSave
end openStack

on backSave
save this stack as ("SAVER" && the time)
send "backSave" to this stack in 120
end backSave
should be saving time-stamped backups into my external drive "X1"
bit, instead is saving backups inside my Macintosh app-folder (i.e. inside the LC 8.1.10 app).

Klaus
Posts: 10554
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Refactoring

Post by Klaus » Sat Jan 12, 2019 6:14 pm

Does it work if you provide the complete pathname?

Code: Select all

on openStack
   backSave
end openStack

on backSave
   save this stack as ("/Volumes/X1/DWP autosaves"/SAVER" && the time)
   send "backSave" to this stack in 120
end backSave
Works here for me, I NEVER EVER rely on "the defaultfolder". 8)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4756
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Refactoring

Post by jacque » Sat Jan 12, 2019 6:20 pm

Klaus beat me to it but here's what I was writing when he posted:

Use the full file path in the backsave handler instead of setting the defaultfolder earlier. Or alternately, set the defaultfolder inside the backsave handler itself. The defaultfolder may be changing between openstack and the time your save handler executes.

Also, are you sure you want to save every 2 seconds? Seems like that would interfere a lot with everything else.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 10554
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Refactoring

Post by Klaus » Sat Jan 12, 2019 6:26 pm

And be prepared to find your stack named: SAVER 6/23 PM on your HD!
The : (colon) is not a valid character for filenames on Mac, it will get replaced by / (backslash)

However if you select htis file with:
answer file "yadda";put it
You will get: SAVER 6:23 PM again. :D

Klaus
Posts: 10554
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Refactoring

Post by Klaus » Sat Jan 12, 2019 6:29 pm

jacque wrote:
Sat Jan 12, 2019 6:20 pm
The defaultfolder may be changing between openstack and the time your save handler executes.
Yes sure, but it is hard to understand why this should happen in this tiny nanosecond before calling the "backsave" routine 8) . So the first saving SHOULD go into the right place! Not?

Code: Select all

on openStack
  set the defaultFolder to "/Volumes/X1/DWP autosaves"
  ## No place/time for changing the defaultfolder again right here:
  backSave
end openStack

on backSave
  save this stack as ("SAVER" && the time)
  send "backSave" to this stack in 120
end backSave

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4756
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Refactoring

Post by jacque » Sat Jan 12, 2019 7:33 pm

Klaus wrote:
Sat Jan 12, 2019 6:29 pm
jacque wrote:
Sat Jan 12, 2019 6:20 pm
The defaultfolder may be changing between openstack and the time your save handler executes.
Yes sure, but it is hard to understand why this should happen in this tiny nanosecond before calling the "backsave" routine 8) . So the first saving SHOULD go into the right place! Not?
I'd think so, but there are several other open* messages and they may take precedence. I found out a while ago that the opening stack behavior isn't what I thought it was.

At any rate, handling the file path only when needed should work. Like you, I almost never use defaultfolder because it can change unexpectedly.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 10554
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Refactoring

Post by Klaus » Sat Jan 12, 2019 7:41 pm

Yes, true, I forgot how busy the IDE is in the background.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2886
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: Refactoring

Post by richmond62 » Sat Jan 12, 2019 8:00 pm

Well, I started off by editing one line:

Code: Select all

save this stack as ("/Volumes/X1/DWP autosaves"/SAVER" && the time)
HOWEVER:

Code: Select all

on openStack
   backSave
end openStack

on backSave
   save this stack as ("/Volumes/X1/DWP autosaves/SAVER" && the time)
   send "backSave" to this stack in 120
end backSave
does NOTHING.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”