Jump to content
Sign in to follow this  
dummzeuch

EmptyString constant

Recommended Posts

Does a constant called EmptyString with the value '' make any sense to you?

 

I think it is just confusing. I mean, when I see it used somewhere I assume that it has some special value that is *not* '', otherwise the programmer would have used a literal '', wouldn't he?

Share this post


Link to post

I'm not confused, but I think it's a bit pointless though. It's like having a named constant with a value of nil. Or a constant named Zero. 

 

What else could EmptyString be? 

  • Like 1

Share this post


Link to post
47 minutes ago, dummzeuch said:

would have used a literal

I use the system defined 'EmptyStr' and find it easier to scan over lines with it..

Share this post


Link to post
18 minutes ago, David Heffernan said:

What else could EmptyString be? 

That's the point: Why have such a constant if it's much more to type and more difficult to read than the literal?

 

The only case I would use such a constant is to have it symbolize an empty string e.g. in a configuration storage where for whatever reason using a literal '' is not possible.

Edited by dummzeuch
  • Like 1

Share this post


Link to post

But there's nothing else that it could be. I personally don't find it useful. I don't think it's confusing, I just think it is silly. 

Share this post


Link to post

There is even a const named Empty in TStringHelper, right accompanied by the function IsEmpty. I agree that the value of both is questionable at least.

 

  • Confused 1

Share this post


Link to post
Guest
24 minutes ago, Uwe Raabe said:

There is even a const named Empty in TStringHelper, right accompanied by the function IsEmpty. I agree that the value of both is questionable at least.

 

I thought it is a field ... and even the docs say so ...

Share this post


Link to post

Introducing a duplicate constant instead of leveraging a built-in system constant is bad code unless there's a strong reason for the duplication.

 

To add a wrinkle....for years I used a method IsEmptyString() which would look for '' or just space(s).  We considered space character(s) to qualify as an "empty" string.   It all depends on what your definition of an EmptyString is within the current context.

 

Share this post


Link to post
51 minutes ago, Schokohase said:

I thought it is a field ... and even the docs say so ...

  public
    const Empty = '';

From Delphi 10.3.2 Rio.

Share this post


Link to post

BTW, strictly speaking, the EmptyStr in SysUtils is not a constant. It is declared as an initialized variable. I think this goes back to either Borland Pascal 6 or Delphi 1 when empty string and nil pointer were two different things, and the compiler/linker had to allocate precious memory to store each constant. If one had many empty string references in the code, using a predefined one saved some valuable space. Today, of course, this is totally irrelevant, that's why the documentation says "Empty string and null string pointer. These constants (sic!) are provided for backwards compatibility only."

 

Share this post


Link to post

The separate constant doesn'T make much sense, but is also not bothering me.

In the TStringHelper together with this in the link below, it could be getting more useful though:

http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.SysUtils.TStringHelper.IsNullOrEmpty

 

Depending if the strings would be nullable.

The description hier doesn't really give a clue what null should really mean:

Quote

Is a static class function that returns whether the given string is empty or not (does not contain any characters).

This will be maybe useful in a later version

Edited by Rollo62

Share this post


Link to post
2 hours ago, Rollo62 said:

The description hier doesn't really give a clue what null should really mean:

This will be maybe useful in a later version

It looks like a dumb copy of the .net String.IsNullOrEmpty method

public static bool IsNullOrEmpty (string value);

which, according to the Microsoft docs, " Indicates whether the specified string is null or an empty string (""). "

What it has to do with the Delphi string implementation and why was it implemented? I do not know.

Edited by Alexander Elagin

Share this post


Link to post

Using '' is the way to go. It works since pascal days, and compiles as a `nil` pointer. Using `EmptyString` or `string.empty` does not make any sense, in the pascal language context.

 

There is no such thing as a null  string. An empty string is stored as a nil pointer, and there is never some allocated memory with a length prefix of 0. When the length reaches 0, the string is always unallocated by the RTL.

 

Note: I usually see people writing if length(something)=0 then and still thinking that it is faster than if something='' then. This is plain wrong. The faster version is the latest: if somestring='' - it just checks if the variable pointer is nil, whereas calling length(), even if it is inlined, generates more assembly, and is slower (dual comparison, and an additional jump if the string is void).

Edited by Arnaud Bouchez
  • Like 3

Share this post


Link to post
15 minutes ago, Arnaud Bouchez said:

An empty string is stored as a nil pointer, and there is never some allocated memory with a length prefix of 0. When the length reaches 0, the string is always unallocated by the RTL.

... unless the string is a ShortString, which was the default and only string type in Turbo Pascal days ;-). Yes, I am that old. Then having predefined constants for the empty string and pointer made sense. Since the introduction of AnsiString/UnicodeString, all these constants became useless and are kept in the RTL due to sentimental reasons, I think. And the IsNullOrEmpty is a C#ism which does not belong to the Pascal world and makes no sense at all.

Share this post


Link to post
20 minutes ago, Alexander Elagin said:

And the IsNullOrEmpty is a C#ism which does not belong to the Pascal world and makes no sense at all.

Besides making C# people coming to Delphi feel a bit more familiar. The concept of Delphi strings tends to stay a mystery for non-Delphi developers.

Share this post


Link to post
2 hours ago, Uwe Raabe said:

The concept of Delphi strings tends to stay a mystery for non-Delphi developers.

To me it makes most sense of all the languages I know. That includes Python, Java and C/C++

I can't wrap my brain around how people could stand handling Win32 API calls with those pesky PChar thingys where you have to know the length of returning strings of a function call...for more than a decade....ugh!

Share this post


Link to post

For me, I found it useful in the following cases: 

- The Sync mode utility works great with EmptyStr.

- I edit a lot of my pas files manually using Notepad, so I just select one EmptyStr and I can clearly see all places where the EmptyStr was implemented. 

- Last thing, it's a mind trick, I pay much more attention for arguments that use EmptyStr.

  Blabla(... , EmptyStr);    // focus on that call.
  Blabla(... , '');          // usually I ignore it.

 

  • Like 1

Share this post


Link to post
48 minutes ago, Mahdi Safsafi said:

usually I ignore it

Agree, once you are actually reading a line it makes no difference but scanning blocks of code with Emptystr is var easier.

 

Your mileage may vary..

Share this post


Link to post
6 hours ago, Mahdi Safsafi said:

I can clearly see all places where the EmptyStr was implemented

Bit you can't see the string.Empty or '' instances. Having lots of different ways to say the same thing just makes life harder. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×