Simplified Format() Command

Propose new features and changes
egocarib
Posts: 86
Joined: 21 May 2015, 18:21

Simplified Format() Command

18 Oct 2017, 13:42

I suggest that the Format() command be simplified, particularly if you are only formatting a single string. As it stands now, I find it very confusing to use. I have to look up the documentation every time that I want to use it, and even then, it takes a while to figure out exactly what I need to do to get the desired result.

For me, the main problem with this command is that it is designed for the use case of formatting multiple values at once. Is that really the most common use case for a format command? In my experience, I only ever use this command to format a single string. For example, I often convert a decimal value to a hex value, trim off extra significant digits from the decimal side of a float, or pad a number with spaces.

To format a single string, I must include this extra syntactic sugar: Format("{1:0.2f}", myString). These extra characters are meaningless when formatting a single string - it is not intuitive why I would need them.

Would it be possible to make this command support a simplified format string without curly braces? Maybe just detect if the format string does not begin with {, then treat it as a request to only format a single string. I also think the documentation should be updated to treat this as the default use case (and explain the more complicated {Index:Format} behavior in the Remarks section, to keep it from confusing typical users.
User avatar
jeeswg
Posts: 3591
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simplified Format() Command

18 Oct 2017, 15:17

The options are:
{} {Index} {:Format} {Index:Format}
From experience, I think it might have been better to make it:
{} {Index:} {Format} {Index:Format}

It's far more common to specify a format than an index. At least for me personally, I've used Format hundreds of times, and haven't needed or wanted to specify an index, but I almost always want to specify a format.

Such a change, regarding colons, is potentially both costly and worthwhile, although it's a bit extreme and I'm not necessarily advocating for it. Format has been one of the most useful AHK functions ever for me, so I don't want to say anything bad about it.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



So I might want to change how colons are handled, but I think the curly brackets are worth keeping, because they distinguish between text based on the variables and literal text, and generally I think that 'smart' behaviour is a bad thing. Although I am somewhat sympathetic to the original proposal that commonly the curly brackets are in effect redundant.

[see also: 'FORMAT FUNCTION EXAMPLES' at:]
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486
egocarib
Posts: 86
Joined: 21 May 2015, 18:21

Re: Simplified Format() Command

18 Oct 2017, 15:37

Thanks for the linked thread. I think it would be good to add some more examples like you have provided there to the documentation. The documentation doesn't even currently have an example of how to format a single, simple string. All the examples have multiple formats or multiple arguments which makes it hard to figure out how to do a simple string reformat for a user unfamiliar with the function.

Being able to omit the index & colon, e.g. "{.2f}" would be an improvement. IMO I still think it'd be best to be able to provide a format string without the curly braces.
User avatar
jeeswg
Posts: 3591
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simplified Format() Command

14 Nov 2017, 11:52

- It turns out that colons may be in the right place after all, if you allowed variables or object references as well as indexes, then it would work like a Deref function, with an optional colon followed by a format string to specify a format. E.g. Format("dec: {var}, hex: 0x{var:X}"). See the example at the bottom of this post:
AHK v2 and strings - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=36833&p=169545#p169545
- Btw you could quite easily create an alternative Format function, to meet your requirements. Cheers.
SOTE
Posts: 75
Joined: 15 Jun 2015, 06:21

Re: Simplified Format() Command

27 Dec 2017, 03:45

egocarib wrote:I suggest that the Format() command be simplified, particularly if you are only formatting a single string. As it stands now, I find it very confusing to use. I have to look up the documentation every time that I want to use it, and even then, it takes a while to figure out exactly what I need to do to get the desired result.

For me, the main problem with this command is that it is designed for the use case of formatting multiple values at once. Is that really the most common use case for a format command? In my experience, I only ever use this command to format a single string. For example, I often convert a decimal value to a hex value, trim off extra significant digits from the decimal side of a float, or pad a number with spaces.

To format a single string, I must include this extra syntactic sugar: Format("{1:0.2f}", myString). These extra characters are meaningless when formatting a single string - it is not intuitive why I would need them.

Would it be possible to make this command support a simplified format string without curly braces? Maybe just detect if the format string does not begin with {, then treat it as a request to only format a single string. I also think the documentation should be updated to treat this as the default use case (and explain the more complicated {Index:Format} behavior in the Remarks section, to keep it from confusing typical users.


In your example you have converting decimal to hex. What's wrong with using SetFormat, Integer, Hex?
lexikos
Posts: 5686
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Simplified Format() Command

27 Dec 2017, 18:29

These extra characters are meaningless when formatting a single string - it is not intuitive why I would need them.
The documentation doesn't even currently have an example of how to format a single, simple string.
Perhaps it's not meant for formatting a single value. :P

You can omit the 1.
Maybe just detect if the format string does not begin with {, then treat it as a request to only format a single string.
Firstly, that would break "0x{:x}", "Current value: {}" and numerous other format strings. A format string is not required to start with a brace, regardless of how many placeholders it contains.

Secondly, there cannot be a { anywhere inside a valid format specifier.

It could detect if the format string contains {, or if the entire format string is one valid format specifier. I have suggested it before and no one was in favour (but only two users replied).

Allowing any shortcuts adds inconsistency, and could cause confusion - or generally reduce flexibility - such as if the format string and the code calling format() aren't written at the same time and in the same place, by the same person. For instance, the format string can come from a configuration or localization (i.e. language translation) file, written by a "translator".
  • Format strings aren't required to contain placeholders. Maybe the programmer provides multiple input values, but the "translator" does not necessarily require any of them. If the behaviour depends solely on the presence of braces, it may become impossible for the "translator" to produce a literal string without including a placeholder.
  • If the behaviour depends on the validity of the format string as a single specifier, there are still some strings that can be used verbatim and other, possibly similar, strings which cannot. For example, Format("Y", ...) produces "Y" but Format("U", ...) does not produce "U". There are also less obvious strings such as "0000001", which is a valid format specifier, but is more likely to be intended literally. It doesn't seem likely to be an issue for the translation scenario, but I'd rather not make any assumptions about how the function will be used.
  • If the behaviour depends on the number of values, the behaviour could change unexpectedly when a new value is added (such as for optional use by the "translator").
User avatar
jeeswg
Posts: 3591
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simplified Format() Command

27 Dec 2017, 18:58

Here's a working function.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 7 guests