Beginners OOP with AHK

Helpful script writing tricks and HowTo's
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Beginners OOP with AHK

12 Jan 2018, 14:53

so you can bind the reference. Duh... Too bad my brain couldn't stretch that far....
try it and see
...
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Beginners OOP with AHK

12 Jan 2018, 18:53

derz00 wrote:I must read this sometime. Am also in a edX course, learning Python and the science in general. Coming back to AHK it seems really loose, etc. But just the other day I wrote my first class. So I am wondering if you can give any tips? It works well, and does what I want. But it seems like a lot of this. :P Is that just the way it is? Or there would be better ways to design it?

(It is to replace SplashText (gone from v2) in one of my applications.)

Code: Select all

class splash {
	__New() {
		this.v:=GuiCreate("-Caption")
		this.v.SetFont("s16")
		this.v.AddText("r1 x5 w390 center","You have punched " status)
		this.v.SetFont("s18")
		this.st:=this.v.AddText("r1 x5 w390 center",status)
		this.p:=this.v.Add("Progress", "x50 w300 h20 cBlack")
	}
	splash_icon(status, timeout := 1600) {
		global In_Icon, Out_Icon
		TraySetIcon(status?In_Icon:Out_Icon)
		this.st.Text:=status?"IN":"OUT"
		this.st.Opt(status?"cRed":"cBlack")
		this.p.Opt(status?"cRed":"cBlack")
		this.v.Show("w400 h160")
		time:=A_TickCount+timeout
		while A_TickCount < time {
			this.p.Value := ceil(100-(time-A_TickCount)/timeout*100)
			sleep 125
		}
		this.v.Hide()
	}
	__Delete() {
		this.v.Destroy()
		this.v:=""
	}
}
Well you are wrapping around an object so you use a lot of this.v to access that object
I don't quite see why you use a class for this. A class is something that should be general - so why are you using globals instead of a parameter for In_Icon and Out_Icon.
I also feel that your class lacks more methods and that your splash_icon method should actually be the constructor.
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Beginners OOP with AHK

12 Jan 2018, 19:03

I added quite a bit to the second part - I think I will make a clean cut soon and start a second tutorial which ill cover slightly more advanced topics, that can really make the difference between a good useable class and some attempt at doing something.
The third part is used for a special category I will call "Tips & Tricks and Ticks and Trips" which will summarize some of the things I have said, give additionaly information and suggest projects you can do by yourself or others have created that you can play around with.
Recommends AHK Studio
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Beginners OOP with AHK

13 Jan 2018, 08:34

Thank you for your input. Is "constructor" the __New() method?

Yes this is not a general class. I mostly created it just to see if I could. Maybe I should expand it and generalize it, to share as a SplashText type of thing.
try it and see
...
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Beginners OOP with AHK

18 Jan 2018, 18:14

Yes the constructor is the __New method.
I also added quite a bit to the second part - I think I'm almost finished with this tutorial.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Beginners OOP with AHK

19 Jan 2018, 12:46

Hello I read the second part now, it is very well composed :thumbup:.
I have minor disagreements,
Avoid variable names that sound like they would make good class names
There is no need to think like this, especially not since force local was recently added.
Avoid byref
Inside the called function it is not an issue, the caller can declare the byref variable as local. Byref is very useful, eg when passing big strings.
Put your classes inside another class and name your file after that class
It is good advise :thumbup:

I like that the tutorial led to a real piece of usable code :thumbup:

Cheers.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Beginners OOP with AHK

19 Jan 2018, 14:38

Helgef wrote:Hello I read the second part now, it is very well composed :thumbup:.
I have minor disagreements,
Avoid variable names that sound like they would make good class names
There is no need to think like this, especially not since force local was recently added.
Avoid byref
Inside the called function it is not an issue, the caller can declare the byref variable as local. Byref is very useful, eg when passing big strings.
I have to say that I didn't have a look at recent developments regarding AHK in depth - towards me this local mode is news - it looks like good news.
However within this tutorial I present design decision making and coding behaviour that has become a habit to me.
Also I will still stand my word that it is better to avoid byref as an output variable in the context of OOP.
You cannot expect the user of your class to use proper coding in order to avoid the drawbacks of byRef.
You still could use byRef for the speedy input of data into a function or a method - however that speed gain is only possible for very limited cases where a variable is provided directly as the input.
In other cases byRef doesn't provide any advantage. Working with variables that contain strings directly is very rare in my OOP style - I can imagine that it is the same for everyone and the same for OOP in general.
In order to create fast OOP we will probably have to create a speedy String class. Actually testing it out convinced me - if you want fast strings with AHK OOP you will have to use a string class:
https://autohotkey.com/boards/viewtopic ... 72#p195372
It is good advise :thumbup:
I like that the tutorial led to a real piece of usable code :thumbup:
Cheers.
Yeah I like that tip too.
And I ill spend a lot of time on this class, because it is something very basic that I want people to built upon.
I just plan to write this class and take you guys along for the ride.
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Beginners OOP with AHK

24 Jan 2018, 16:37

I have added a bit to the third part - I thought I would finish this tonight but apparently it was more than I expected.
Recommends AHK Studio
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Beginners OOP with AHK

24 Jan 2018, 20:47

There, I read part 1. Time to celebrate.

derz00 slaps himself on the back sarcastically
try it and see
...
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Beginners OOP with AHK

25 Jan 2018, 13:44

Thanks for reading it.
Did you have trouble understanding anything?
Were there any parts that confused you?
Recommends AHK Studio
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Beginners OOP with AHK

25 Jan 2018, 15:59

@nnnik: Thanks for taking the time to do this tutorial, well done.
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Beginners OOP with AHK

26 Jan 2018, 09:11

nnnik wrote:Thanks for reading it.
Did you have trouble understanding anything?
Were there any parts that confused you?
No, it all made sense to me. The concept of the this. reference is not new to me, so I can't really give feedback on your explanation. But it is one of the confusing parts of the syntax. Just because it's difficult to put it into words, I guess.

If I hadn't been watching this thread and seen you were actively working on it, I probably wouldn't have read it. The first part laid things out well, now I want to get into the next parts as I have time, to learn the skills of it.
try it and see
...
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Beginners OOP with AHK

27 Jan 2018, 18:55

I have finished adding the last part.
In the next few days I might go over this tutorial and correct a few mistakes or add things I forgot - but not many things will change.
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Beginners OOP with AHK

06 Feb 2018, 11:07

I added local to the modularity tips and added 2 more Tricks to the last section.
I also fixed many of the sentences in the first part. You should be able to read it more easily now.
Recommends AHK Studio
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Beginners OOP with AHK

13 Dec 2018, 15:47

This tutorial is one year old today :-) I'm not finished with it but I want to say right now that it is really a great piece ! Looking forward to complete it.

Thanks @nnnik !
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Beginners OOP with AHK

14 Dec 2018, 10:00

Thank you for your kind words :)
Recommends AHK Studio
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Beginners OOP with AHK

14 Dec 2018, 12:21

I'm finished with your tutorial. There are a few concepts that will become more clear with some experience. Again, it was of great help.

Next topic for me is Collections and Classes. As homework, I could use your FileSystemClass and develop the methods and objects required to collect the content of a given folder (with subfolders recursion). Any lead on this would be appreciated.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Beginners OOP with AHK

14 Dec 2018, 13:19

@JnLlnd: If you want more info, you can try this. Cheers.
jeeswg's object classes tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=54588
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Beginners OOP with AHK

14 Dec 2018, 14:09

Since AHK already offers the Loop, Files and Folder loop I would just use that.
From there you just need to wrap the results of that loop in an object and put it isndie an array.

@jeeswg Im not sure how this relates to the current discussion.
This about practices not the specific behavior of AHK objects.
Recommends AHK Studio
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Beginners OOP with AHK

17 Dec 2018, 15:25

jeeswg wrote:
14 Dec 2018, 13:19
@JnLlnd: If you want more info, you can try this. Cheers.
jeeswg's object classes tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=54588
Thanks for the reference. It is not directly what I wanted to do but it gives good background on AHK classes and objects.
nnnik wrote:
14 Dec 2018, 14:09
Since AHK already offers the Loop, Files and Folder loop I would just use that.
From there you just need to wrap the results of that loop in an object and put it isndie an array.
Yes. But I want to use it as a practice topic. I added some methods to your class to store the files and directory objects in an "Items" container inside the FileContainer sub class. I do not pretend this has any real life application but it was an interesting exercice, extending your class tutorial and putting classes at work with recursion.

I'm not sure it is all done in the best way but if you have comments for improvements, it will be appreciated. I will post it in this thread shortly.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 31 guests