[v2] Indentation instead of braces

Propose new features and changes
Gotjek
Posts: 18
Joined: 11 Aug 2022, 11:01

[v2] Indentation instead of braces

Post by Gotjek » 14 Sep 2022, 05:31

Hi !

I have thi piece of code :

Code: Select all

Item_Btn_Messed_Phoneme__Click(gui_obj, info){
    If phonemes_are_enabled = true{
        For btn_clicked in messed_phonemes_buttons_list{
            If (btn_clicked = gui_obj){
                phoneme_clicked := item_opt__messed_phonemes[A_Index]
                Global last_messed_phoneme_clicked
                last_messed_phoneme_clicked := phoneme_clicked
                Play_Phoneme(phoneme_clicked, PHONEMES_FOLDER)
            }
        }
    }
}
I thought all the ending braces are not very nice. Why not a Python-like behaviour ?

Code: Select all

Item_Btn_Messed_Phoneme__Click(gui_obj, info):
    If phonemes_are_enabled = true:
        For btn_clicked in messed_phonemes_buttons_list:
            If (btn_clicked = gui_obj):
                phoneme_clicked := item_opt__messed_phonemes[A_Index]
                Global last_messed_phoneme_clicked
                last_messed_phoneme_clicked := phoneme_clicked
                Play_Phoneme(phoneme_clicked, PHONEMES_FOLDER)
I think it would be lighter and more readable.
It's just an idea by a newbie. :)

guest3456
Posts: 3453
Joined: 09 Oct 2013, 10:31

Re: [v2] Indentation instead of braces

Post by guest3456 » 15 Sep 2022, 09:39

it would be breaking change, so unlikely to be accepted at this point in v2's progress

but i believe if the block only contains one line, you dont need braces. so your code could be shortened to this:

Code: Select all

Item_Btn_Messed_Phoneme__Click(gui_obj, info){
    If phonemes_are_enabled = true
        For btn_clicked in messed_phonemes_buttons_list
            If (btn_clicked = gui_obj){
                phoneme_clicked := item_opt__messed_phonemes[A_Index]
                Global last_messed_phoneme_clicked
                last_messed_phoneme_clicked := phoneme_clicked
                Play_Phoneme(phoneme_clicked, PHONEMES_FOLDER)
            }
}


Gotjek
Posts: 18
Joined: 11 Aug 2022, 11:01

Re: [v2] Indentation instead of braces

Post by Gotjek » 19 Sep 2022, 06:15

Thank you for this nicer code !

Lorien
Posts: 33
Joined: 10 Dec 2019, 11:16

Re: [v2] Indentation instead of braces

Post by Lorien » 19 Sep 2022, 06:35

guest3456 wrote:
15 Sep 2022, 09:39
but i believe if the block only contains one line, you dont need braces. so your code could be shortened to this:
While true, this has caused me many headaches over the years (with any brace style language). I will leave out the optional braces, then later come back and add another statement to a branch.

I think I have even confused Python's indentation system once or twice. (I thought it looked properly indented, but the parser didn't agree with me.)

User avatar
RaptorX
Posts: 368
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2] Indentation instead of braces

Post by RaptorX » 19 Sep 2022, 07:21

This is a great example of how you can clean your code by using "guard clauses".

I would write this piece of code like this:

Code: Select all

Item_Btn_Messed_Phoneme__Click(gui_obj, info){
	Global last_messed_phoneme_clicked

	; if your condition is not met just exit
	; no braces needed
	If !phonemes_are_enabled 
		return
	
	For btn_clicked in messed_phonemes_buttons_list{
		; if your condition is not met jump to the next loop
		; again, removed the unwanted braces
		If (btn_clicked != gui_obj)
			continue
		
		phoneme_clicked := item_opt__messed_phonemes[A_Index]
		last_messed_phoneme_clicked := phoneme_clicked
		Play_Phoneme(phoneme_clicked, PHONEMES_FOLDER)
	}
}
the result is the same as @guest3456 but it would work better in other conditions where his would introduce additional (probably unwanted) braces, for example if you have some if, else if, else blocks.

Having a guard clause eliminates unneeded braces in all cases.
Projects:
AHK-ToolKit

guest3456
Posts: 3453
Joined: 09 Oct 2013, 10:31

Re: [v2] Indentation instead of braces

Post by guest3456 » 19 Sep 2022, 09:33

RaptorX wrote:
19 Sep 2022, 07:21
This is a great example of how you can clean your code by using "guard clauses".

I would write this piece of code like this:

Code: Select all

Item_Btn_Messed_Phoneme__Click(gui_obj, info){
	Global last_messed_phoneme_clicked

	; if your condition is not met just exit
	; no braces needed
	If !phonemes_are_enabled 
		return
	
	For btn_clicked in messed_phonemes_buttons_list{
		; if your condition is not met jump to the next loop
		; again, removed the unwanted braces
		If (btn_clicked != gui_obj)
			continue
		
		phoneme_clicked := item_opt__messed_phonemes[A_Index]
		last_messed_phoneme_clicked := phoneme_clicked
		Play_Phoneme(phoneme_clicked, PHONEMES_FOLDER)
	}
}
the result is the same as @guest3456 but it would work better in other conditions where his would introduce additional (probably unwanted) braces, for example if you have some if, else if, else blocks.

Having a guard clause eliminates unneeded braces in all cases.
+1000

great example. this is a good point to always be thinking about code clarity and readability, even if the technical functionality is the same. what would make the code easier to understand for the READER? and keep in mind, YOU are usually the reader, when you come revisit this code in the future, and try to figure out what you were doing


Gotjek
Posts: 18
Joined: 11 Aug 2022, 11:01

Re: [v2] Indentation instead of braces

Post by Gotjek » 20 Sep 2022, 08:57

@RaptorX Your code is exactly what I need. When I code, I try to translate my mere human logic into program logic. But it's not always very efficient/nice/optimized/legible. It's difficult to consider other ways to think, to order conditions and loops. I save this "Guard clauses" technique to apply in other places.

@guest3456 I totally agree with you.

Write smarter code is my brain's responsibility and maybe I could discover how to do by myself. But I'm not very good at it and your example is precious to me. Have you got other example or patterns like this to write better code ? Like some common errors like this that could be avoided ? I would be fond of it.

User avatar
RaptorX
Posts: 368
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2] Indentation instead of braces

Post by RaptorX » 24 Sep 2022, 08:44

Gotjek wrote:
20 Sep 2022, 08:57
Write smarter code is my brain's responsibility and maybe I could discover how to do by myself. But I'm not very good at it and your example is precious to me. Have you got other example or patterns like this to write better code ? Like some common errors like this that could be avoided ? I would be fond of it.
Don't be too hard on yourself, coding goes in 3 different phases (for me at least)
  • Write something that works no matter how ugly
  • Refactor (create loops/functions for repeated code)
  • Test your changes
A quick example of this is when i do this:

Code: Select all

line := headers
line .= td.item[1].innerText '`t' ; Name
line .= td.item[2].innerText '`t' ; City
line .= td.item[3].innerText '`t' ; State
line .= td.item[4].innerText '`t' ; Zip
line .= td.item[5].innerText '`t' ; Phone
line .= td.item[6].innerText '`t' ; Email
line .= td.item[7].innerText '`t' ; Page
line .= td.item[8].innerText '`t' ; Custom
This is taking some info from some objects and appending it to a variable to be used later...
First I make sure is working perfectly and then I go ahead and refactor it to this:

Code: Select all

line := headers
loop 8
	line .= td.item[A_Index].innerText '`t'
Nicer cleaner code.
You'll get better at this with time, and sometimes you just skip the first step entirely. But you dont have to be hard on yourself because your code is "not readable"... you first write code that works, and then make it readable. :)
Projects:
AHK-ToolKit

Gotjek
Posts: 18
Joined: 11 Aug 2022, 11:01

Re: [v2] Indentation instead of braces

Post by Gotjek » 25 Sep 2022, 06:47

Thank you @RaptorX !

Sometimes I feel the "refactor" stage is like starting from scratch again and again.
At first, all is clear and simple. Then complexity sets in gradually and seems difficult to manage.
So seeing these smart pieces of code which resolves/avoids hours of hassle is great.

User avatar
RaptorX
Posts: 368
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2] Indentation instead of braces

Post by RaptorX » 25 Sep 2022, 10:45

Gotjek wrote:
25 Sep 2022, 06:47
Sometimes I feel the "refactor" stage is like starting from scratch again and again.
Its not, it is just a way to save time in the future making code more maintainable and understandable. it is an essential part of good programming practices.
Projects:
AHK-ToolKit

Post Reply

Return to “Wish List”