is it not possible to have another version of left but pass a boolean that tells it to pass whatever is there?
str = “123”
str.left(4) // = exception
str.left(4,true) = 123
is it not possible to have another version of left but pass a boolean that tells it to pass whatever is there?
str = “123”
str.left(4) // = exception
str.left(4,true) = 123
So far I’m not either… but might it be because we know the classic framework so well?
That said I do have to say it seems to take more (often less obvious/intuitive) code as compared to the classic framework, to get things done, which does not seem very RAD.
Just another idea, LeftUpTo(4)
. I too couldn’t come up with any I really liked.
That is my favorite so far.
LeftWithDisregardForToes
One thing to consider is the whole function signature, i.e. what would would be in docs or the bottom of the IDE
Function LeftAtMost(Extends value As String, max As Integer) As String
Function LeftMax(Extends value As String, max As Integer) As String
Function LeftMaxChar(Extends value As String, max As Integer) As String
Function LeftUpTo(Extends value As String, max As Integer) As String
// or
Function LeftUpTo(Extends value As String, atMost As Integer) As String
because the parameter name says a lot about the method as well.
@Norman Palardy No, I don’t check for errors that way, because it never matters how many chars are returned. There is no possibility that getting fewer chars than I asked for would cause a problem. The new functions just make the code more verbose, without actually benefitting me at all.
But it is doing exactly what I expect.
That is usually the case for me as well… I suspect for most real world uses it’s that way as well…
Explicit anything is generally more code, but does reduce errors/surprises. For example, one could use Variants for every variable, but we explicitly type our code because we get better error handling. The same for these new Left/Mid/Right functions. We now have the means to explicitly say what we want to do.
i.e. Norman gave an example of how to act like the Classic Framework and Kem has created helper methods to do exactly that. So, now in code, if we want exactly 4 characters off the left of a string, we can do so:
dim firstFour as Text = myText.Left(4)
and if we want up to 4 characters off the left of the string, we can do that too…
dim uptoFour as Text = myText.Left(Min(myText.Len, 4))
// or with a very simple helper method using extends
dim uptoFour as Text = myText.LeftUpTo(4)
This is a big win in my book. I, the programmer, have control over what is to be done. Sure, I could have done it in the past:
if myText.Len < 4 then
dim ex as new OutOfBoundsException
ex.Message = "myText only contains " + Str(myText.Len) + " characters, but we need 4"
raise ex
end if
dim uptoFour as Text = myText.Left(4)
but the new is so much nicer, IMHO of course.