Explicit Self for method calls

In the same week, I saw (something like) this, first on a recently published YouTube video, and second in the Uk Dev Group from someone very experienced :

procedure TMyClass.CallDoSomething();
begin
  Self.DoSomething();
end;

I don’t think I have ever seen self used this way ever before, myself.
The argument put forward was that it should be explicit where your functionality is coming from.
A reasonable point.

Initially when I encountered functional programming, and the ideas around ‘referential transparency’, my first thought was that from that perspective, procedures make no sense, because regardless of inputs, they don’t return anything.
They can only operate via side-effects, which are by definition not visible by looking at the function signature.

So, functionally, you would naively expect any function to take as argument every programming element that it needs to operate on to return a value.

It took quite a while before I reconsidered that

  • we had eg out parameters, that approximate returning “sum types” (like a C-union or variant record), and
  • we have objects, and method procedures operating in the (semi-unwritten) context of an instance

I’m still working out how I feel about these things.

Do other people use explicit Self in method calls ?

1 Like

I used to work with a young lady Delphi programmer that did that. Her explanation was that Delphi would suggest auto-completion method names from the same class after you typed in “Self.”

I think it is a waste of time.

“Self” becomes “Self Evident” when you hover your mouse over a method name not prefixed with “Self.”

Most of the time you don’t really care if DoSomething() is a unit-level Function, Procedure or class method. To dig deeper, hover your mouse for full details, but this is rare. You can also ^LMB to view the method source; Last GExperts Procedure List is pretty good at tracking down method names.

Self.yyyy, With yyyy do are two of my pet peeves.

No, however: I would say it does makes it much faster to read the code and understand where implementions are. IMHO requiring you hover the mouse or using the IDE/tooling to jump to definition/implementation is slowing a code reader down and so personally would not say it is a waste of time. Particularly when for an old code base with a mix of global variables and functions mixed with more moden practices.

Another thing is to use prefix notations on methods (maybe v_ for private, t_ for protected) to make reading code faster and simpler.

I’ll sometimes use them in with statements in existing code. Sometimes it is difficult to work out where the variable is coming from, so using self makes it explicit. My preferred option would be to remove the with statement completely though.

1 Like

We got “with” shoe horned into the debate :-). I saw and interesting post recently where “with” was being used as an optimising hint for the compiler. I never use them.

I can see self being beneficial to make scope really easy to see, but I don’t use them, unless there is a chance of ambiguity. I might if it was s. as typing self 50 times would mean fixing 40 of them to get the spelling right :-).

I guess it is what you are used to. For the talk Paul is referring to, it felt a bit weird, but I would get used to it before my second cup of tea I guess. Easier than getting used to how some people put their begin and ends together.
JAC.