Recent

Author Topic: [CLOSED] What is the Pascal convention?  (Read 4658 times)

guest65109

  • Guest
Re: What is the Pascal convention?
« Reply #30 on: January 11, 2020, 11:32:28 am »
I'm on my own so I'm free to choose whatever I like. The fact is I'm from a Java foundation I like to do the Java style. Everything is plain English, class Foo is Foo but no TFoo or something like that, very easy to read and intuitive to understand. But I feared I have to follow your convention, because I need to post snippets on this forum when asking for help  ;)

So how do you call the instantiation of class Foo?  AFoo? Or do you call the type FooClass ?

Imagine we have a class Person. We will create Person1, Person2,... but why do we have to create something whose name counter intuitive like AFoo or FooClass after all?

I also don't know why you can't let variable name just be the variable's name but prepending an `A` before it?

Take this example:

Code: Pascal  [Select][+][-]
  1. procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  2. AResponse: TResponse; var Handled: Boolean);
  3. begin
  4.   AResponse.Content := 'Hello, World!';
  5.   Handled := true;
  6. end;

Why it has to be ARequest but not simply Request, request or simply req? What is the meaning of this `A` after all? ARequest = A request? or ARequest = Abstract request?

All the these prefixes were intended to improve the cleanliness of the code but indeed only cause it to be more obscure and difficult to read because they are all very counter intuitive.

guest65109

  • Guest
Re: What is the Pascal convention?
« Reply #31 on: January 11, 2020, 11:45:17 am »
I'm on my own so I'm free to choose whatever I like. The fact is I'm from a Java foundation I like to do the Java style. Everything is plain English, class Foo is Foo but no TFoo or something like that, very easy to read and intuitive to understand. But I feared I have to follow your convention, because I need to post snippets on this forum when asking for help  ;)

So how do you call the instantiation of class Foo?  AFoo? Or do you call the type FooClass ?

Remember than in Java a class named Foo can be instantiated into foo, because the case matters. Here is not possible (but it saves some headaches when writing without care... sometimes I type FOo by mistake).

It's plain ridiculous! For example: we have the class Person. We would call Person1, Person2,... but never person. Even though the language is case sensitive, it's not recommended to do something like that.

p/s: with the Foo class, it should be myFoo, not AFoo.

guest65109

  • Guest
Re: What is the Pascal convention?
« Reply #32 on: January 11, 2020, 11:53:06 am »
I feared I have to follow your convention, because I need to post snippets on this forum when asking for help  ;)
Fear is not a good reason to follow the T prefix convention for types.  It's a convention but, there is a reason it exists (a good one too!.)  The reason is that it is _often_ useful and important to _know_ you are dealing with a type and not something else. 

Consider this code:
Code: Pascal  [Select][+][-]
  1. var
  2.   a : set of char;
  3.   i : integer;
  4.   x : char;
  5.  
  6. type
  7.   b         : 1..10;   // apparently in FPC ":" can be used instead of "=" in type declarations ???
  8.   c         = 1..10;
  9.  
  10.   TSOME_RANGE  : 1..10;
  11.   Direction : (north, east, south, west);   // accepts ":" instead of "="  ???
  12.  
  13.   TDIRECTION_ENUM1 = (north, east, south, west);      // "=" as expected
  14.  
  15.   // these are not in accordance with standard Pascal type declarations
  16.  
  17.   TDIRECTION_ENUM1 : (north, east, south, west);      // :   ???
  18.   TDIRECTION_ENUM1 =  : (north, east, south, west);   // = : ???
  19.  
  20.  
  21. var
  22.   d : Direction;
  23.   e : TDIRECTION;
  24.  
  25. begin
  26.   for x in a do ;
  27.   for i in b do ;
  28.   for i in c do ;
  29.  
  30.   for i in TSOME_RANGE do ;
  31.  
  32.   for d in Direction do ;
  33.   for e in TDIRECTION_ENUM1 do ;
  34.  
Look at the lines that start with for <varname> in <something> do ;

The problem is the <something>.  In those statements it is difficult to ascertain at a glance what the extent of the "for" is.  This is because a, b and c could be one of an enumeration, a set or a range. 

It's much nicer and clearer when not only the type is prefixed with a T but also when it is postfixed with something along the lines of "RANGE", "SET", "ENUM" because it's much easier to have at least some idea of what the statement is doing.

Aside from the examples above.  Consider you have a "Database" type.  Most programmers would like to have a variable named "Database" to refer to the type "Database" but, because the name of the variable has to be different than the name of the type and, because Pascal is not case sensitive, it makes sense to have some way to identify the type.  A very common convention used by Pascal programmers is to prefix the type with T for that reason.  That way you can have a "var Database : TDATABASE;" which is logical and sensible.  In C that is not necessary only because the language is case sensitive and just using different case for the variable name is enough to distinguish it from an, otherwise, identically named datatype.

Talking about conventions, one I wish Pascal programmers would follow is to _always_ put () after function names that require no parameters.  When you see a statement like this:
Code: Pascal  [Select][+][-]
  1. a := b;
b could be some global variable located somewhere or a function, also located "somewhere".  Now to figure out what that statement does, the programmer has to dig out from who knows where what b is.  People who are obsessed with typing less will name the function with a single letter or few letters, this usually causes a simple text search for it to return a large number of invalid hits the programmer has to wade through.  Too bad the obsession with typing less cannot be converted into obsessing about making the program easy to understand (of all obsessions, that one might be a good one.)

You shouldn't complain about Pascal being wordy.  For functions that take no parameters and for simple logical expressions, Pascal saves you from typing two characters (the parentheses that C requires but Pascal doesn't.)

Now, on a completely different note.  The lines that contain ??? deviate from the expected way types are declared in Pascal.  I don't know if that is intended in FPC but, the reference manual does not show that ":" (by itself) and "= :"as acceptable in a type definition.

HTH.

This is a useless discussion to begin with. Convention is not law. It's just common agreements of some people upon something. It's not about right or wrong. It's about you agree or follow it, or not, which is not mandatory to anyone. That's all. Everyone could say any conventions don't make any senses for whatever reasons, and vice versa. For example, case sensitive languages (like Java) don't make any senses. How could 'foo' is different from 'Foo'? It's weird.

AFAIK, anyone who disagree with Pascal convention is mostly not a longtime Pascal programmer so s/he doesn't understand or unable to understand the reasons behind the convention. My advice is… study Pascal with Pascal mindset and paradigm. Don't ever judge Pascal convention using Java mindset and paradigm, or any other languages for that matter. Every language has its own philosophy. Deal with it.

Long story short this naming convention is indeed a workaround for the limitations of this language. If I want to use this language, I have to follow this discipline. If I don't want to follow this discipline, I simply shouldn't use the language at all. The same thing also true for C. This thread should be closed.

rvk

  • Hero Member
  • *****
  • Posts: 4227
Re: What is the Pascal convention?
« Reply #33 on: January 11, 2020, 11:53:56 am »
We would call Person1, Person2,... but never person.
Hahahahaha  :D Isn't that a convention too. Adding 1, 2 or 3 after a variable name?
Why would that be any different from prefixing T before the class?

Prefixing and adding a letter to variable and classes do make your code more readable and (what's more important) easier to maintain and bugfix.

I think you need to get some programming experience with bigger programs (and preferably more programmers). You'll find out conventions are there for a reason.

But again, if you are on your own and you don't share code, you can do whatever you want.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8612
  • FPC developer.
Re: What is the Pascal convention?
« Reply #34 on: January 11, 2020, 12:00:10 pm »
Why it has to be ARequest but not simply Request, request or simply req? What is the meaning of this `A` after all? ARequest = A request? or ARequest = Abstract request?

Well, that is the point.  If instance and class have the same default name, you get a lot of clashes, or the unnatural situation with multiple namespaces (or other hacks) to mask that the same name can mean multiple things.

But again, if you are on your own and you don't share code, you can do whatever you want.

Like Martin of MSEGUI fame, who suffixed -ty :-)
« Last Edit: January 11, 2020, 12:01:43 pm by marcov »

guest65109

  • Guest
Re: What is the Pascal convention?
« Reply #35 on: January 11, 2020, 12:05:13 pm »
We would call Person1, Person2,... but never person.
Hahahahaha  :D Isn't that a convention too.

I do not against convention but stupid and counter intuitive convention, man.

p/s: about Person1, Person2,... it's just natural way of doing thing, no convention needed.

guest65109

  • Guest
Re: What is the Pascal convention?
« Reply #36 on: January 11, 2020, 12:07:50 pm »
Why it has to be ARequest but not simply Request, request or simply req? What is the meaning of this `A` after all? ARequest = A request? or ARequest = Abstract request?

Well, that is the point.  If instance and class have the same default name, you get a lot of clashes, or the unnatural situation with multiple namespaces (or other hacks) to mask that the same name can mean multiple things.

But again, if you are on your own and you don't share code, you can do whatever you want.

Like Martin of MSEGUI fame, who suffixed -ty :-)

What exactly the `A` mean? `A` or `Abstract` or... `Arbitrary`?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8612
  • FPC developer.
Re: What is the Pascal convention?
« Reply #37 on: January 11, 2020, 12:13:09 pm »
What exactly the `A` mean? `A` or `Abstract` or... `Arbitrary`?

It is the natural language article as in "a man" or "a house".  So not to different from suffixing 1. Just using words instead of numerals. (and shorter and with a bit less emphasis than the direct equivalent of "1", "one")

So strictly speaking it should be AnIdentifier, not AIdentifier :-) But the capitalization already makes the distinction, and natural language was of course never designed to be written anyway. Its origins are speech.

I do not against convention but stupid and counter intuitive convention, man.

Personally I get the feeling you never got the convention, no more, Pascal as a whole much chance. If you are determined to find fault with something, it is easy.
« Last Edit: January 11, 2020, 12:27:04 pm by marcov »

BrunoK

  • Sr. Member
  • ****
  • Posts: 256
  • Retired programmer
Re: [CLOSED] What is the Pascal convention?
« Reply #38 on: January 11, 2020, 04:04:12 pm »
Code: Pascal  [Select][+][-]
  1. procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  2. AResponse: TResponse; var Handled: Boolean);
  3. begin
  4.   AResponse.Content := 'Hello, World!';
  5.   Handled := true;
  6. end;

Why it has to be ARequest but not simply Request, request or simply req? What is the meaning of this `A` after all? ARequest = A request? or ARequest = Abstract request?
The leading A in ARequest and AResponse convey, when referenced in the body of the implemented procedure/function, that we are  dealing with a passed parameter, not a procedure var or protected fields. If dealing with more than 10 lines in a procedure, this hint is very useful.

Sadly, this convention is not applied everywhere thus you find declaration like :
Code: Pascal  [Select][+][-]
  1. function TWin32WidgetSet.EnableWindow(HWnd: HWND; BEnable: Boolean): Boolean;
This makes it difficult to differenciate, in the  EnableWindow function, if one is dealing with a parameter or a type, this did (maybe still so) confuse the debugger.

Had it been defined :
Code: Pascal  [Select][+][-]
  1. function TWin32WidgetSet.EnableWindow(aHWnd: HWND; aEnable: Boolean): Boolean;
It would be much clearer in the body of the function that aHWnd is accessing the passed parameter value.

I have a personal convention to define, for example, any record definition this way :
Code: Pascal  [Select][+][-]
  1. type
  2.   RUInt128 = record
  3.   Private
  4.     FValue : record
  5.       case boolean of
  6.       false: (CurrencyA : array[0..1] of Currency);
  7.       true: (QW64a : array[0..1] of QWord);
  8.     end;
  9.     FNeg : boolean;
  10.     function  GetValue : Currency;
  11.     procedure SetValue(aCurrency :  Currency);
  12.   public
  13.     property AsCurrency : Currency read GetCurrency write SetValue;
  14.   end;
  15.  
This gives the following informations when dealing with this type :
The leading R for the record name means, in a procedure that would declare it, that it exists on stack storage of the procedure and will disappear when the procedure exits.

FValue, FNeg (private) are 2 fields that should only be accessed/modified directly in the unit implementation. This is generally enforced in delphi / FPC / Lazarus.

A private convention is that the A / a trailing character in CurrencyA / QW64a are arrays fields, it makes things easier to figure out when they appear in the code.

These conventions, once you figure which ones are applied since they are just naming conventions, help a great deal in understanding code and long term may make the difference between maintainable code or seemingly obfuscated code that will die if buggy.

In any case, having GOOD NAMES for class / variable / field / methods / etc... is not easy and often takes time in development. If the application to write is not trivial, determining names is part of a project analysis, they may be defined by the designers before the first line of code is written.

soerensen3

  • Full Member
  • ***
  • Posts: 211
Re: [CLOSED] What is the Pascal convention?
« Reply #39 on: January 11, 2020, 04:26:09 pm »
I don't know for sure but I think the A is for attribute. P like parameter was already taken for pointers.
I think it makes sense to prepend the A in some situations where you have a property a field and a parameter which are quite common:
Code: Pascal  [Select][+][-]
  1. type
  2.   TSomeObject = class ( TObject )
  3.     private
  4.       FParent: TSomeObject;
  5.  
  6.     public
  7.       constructor Create( AParent: TSomeObject );
  8.  
  9.     published
  10.       property Parent: TSomeObject read FParent write FParent;
  11.   end;
  12.  
  13. [...]
  14.  
  15. constructor TSomeObject.Create ( AParent: TSomeObject );
  16. begin
  17.   inherited Create;
  18.   FParent:= AParent;
  19. end;
  20.  
  21.  
So Parent is the property, FParent is the field and AParent is the parameter of a function/procedure.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

howardpc

  • Hero Member
  • *****
  • Posts: 3488
Re: [CLOSED] What is the Pascal convention?
« Reply #40 on: January 11, 2020, 05:23:33 pm »
I don't know for sure but I think the A is for attribute.
No, as Marco writes, it is simply the indefinite article prefixed without a space since identifiers cannot contain space characters.
There is a lot to be said for sticking to universal conventions.
For most of my adult life I moved an indicator stick on the right of the steering column to signal before turning the car.
On a more recent model, with steering column sticks reversed, whenever I signal to turn, I turn the windscreen wipers on instead.  :)
« Last Edit: January 11, 2020, 05:27:46 pm by howardpc »

soerensen3

  • Full Member
  • ***
  • Posts: 211
Re: [CLOSED] What is the Pascal convention?
« Reply #41 on: January 11, 2020, 07:52:55 pm »
I don't know for sure but I think the A is for attribute.
No, as Marco writes, it is simply the indefinite article prefixed without a space since identifiers cannot contain space characters.

I like my version better  :P
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

howardpc

  • Hero Member
  • *****
  • Posts: 3488
Re: [CLOSED] What is the Pascal convention?
« Reply #42 on: January 11, 2020, 08:29:44 pm »
I like my version better  :P
Why not indeed?
How a particular naming convention may have arisen is unimportant.

The value of such naming systems is the support they provide in producing readable and easily maintainable code, code that is less likely to contain bugs because of ambiguity and so on.
All languages (not just Pascal) benefit from care taken over naming of variables and routines and data structures.

Self-explanatory names, and names that encode other useful at-a-glance information (this is a pointer, this is an array, this instance needs to be manually freed, this is a type not a variable, etc.) allow you to write almost self-documenting code, requiring little in the way of additional comments to be immediately comprehensible by other programmers.
In a forum such as this where code offered is going to be read by many eyes, adopting a few common coding conventions is a courtesy to others.

Handoko

  • Hero Member
  • *****
  • Posts: 3660
  • My goal: build my own game engine using Lazarus
Re: What is the Pascal convention?
« Reply #43 on: January 11, 2020, 09:45:36 pm »
I have seen many examples of how stupid and ridiculous it is.

Please do not say it is stupid or ridiculous before you really understand the reasons. It's only make you seem like a troll. I don't know why you gone but came back again and again. But I'm glad to see, your language now 'better' than previously. That's a good improvement.

Actually there is no a 'perfect' language or dev tool that for everyone. What works for others may not work for you. And there is no a single one suitable for all kinds of projects.  Each tools have its own advantages and disadvantages. You do not have to pick the tool because others say it is good. And you do not have to limit yourself using only 1 tool.

Honestly I envy about you, in a positive way. You know some basic of various languages and tools, C, Java, Python, etc. And you're still young, you have a very long time for the journey to absorb lots of new things. A good programmer should be able to use various of tools. Instead of forcing yourself to 'accept' Pascal, why don't you just use them all.

Each of the things you complained, we have the reasons to counter. I read the answers, they really answered well. But I think, you're unable to fully understand and still not agree.

My suggestion is, try to develop a 'real' software instead of a hello world program. Maybe a simple game, stock control, personal todo and note book. By doing a real project, then you will see the beauty of Pascal, you will understand why readability and maintainability is important and why the conventions are needed. Experience it yourself is best way to learn.

Maybe later you will find that, Pascal is not for you. That's okay. A while ago, there was a user in the forum asking a lot of questions to find out is Lazarus/FPC suitable for him. Later he left for RemObjects Oxygene. Also a kid who was able to write a simple game using FPC after learning Pascal in weeks. He privately contacted me, telling me C++ is awesome because you type less. Great to hear he found the thing that suitable for him, I didn't try to convince him Pascal is better. We do not always pick the best but we usually choose the one we like most.

You were lucky to get lots of answers from 'real' programmers here. Most of them really built huge projects and have decades of experience.

Have a nice weekend.
« Last Edit: January 11, 2020, 09:53:29 pm by Handoko »

valdir.marcos

  • Hero Member
  • *****
  • Posts: 993
Re: What is the Pascal convention?
« Reply #44 on: January 12, 2020, 04:04:05 am »
I have seen many examples of how stupid and ridiculous it is.

Please do not say it is stupid or ridiculous before you really understand the reasons. It's only make you seem like a troll. I don't know why you gone but came back again and again. But I'm glad to see, your language now 'better' than previously. That's a good improvement.

Actually there is no a 'perfect' language or dev tool that for everyone. What works for others may not work for you. And there is no a single one suitable for all kinds of projects.  Each tools have its own advantages and disadvantages. You do not have to pick the tool because others say it is good. And you do not have to limit yourself using only 1 tool.

Honestly I envy about you, in a positive way. You know some basic of various languages and tools, C, Java, Python, etc. And you're still young, you have a very long time for the journey to absorb lots of new things. A good programmer should be able to use various of tools. Instead of forcing yourself to 'accept' Pascal, why don't you just use them all.

Each of the things you complained, we have the reasons to counter. I read the answers, they really answered well. But I think, you're unable to fully understand and still not agree.

My suggestion is, try to develop a 'real' software instead of a hello world program. Maybe a simple game, stock control, personal todo and note book. By doing a real project, then you will see the beauty of Pascal, you will understand why readability and maintainability is important and why the conventions are needed. Experience it yourself is best way to learn.

Maybe later you will find that, Pascal is not for you. That's okay. A while ago, there was a user in the forum asking a lot of questions to find out is Lazarus/FPC suitable for him. Later he left for RemObjects Oxygene. Also a kid who was able to write a simple game using FPC after learning Pascal in weeks. He privately contacted me, telling me C++ is awesome because you type less. Great to hear he found the thing that suitable for him, I didn't try to convince him Pascal is better. We do not always pick the best but we usually choose the one we like most.

You were lucky to get lots of answers from 'real' programmers here. Most of them really built huge projects and have decades of experience.

Have a nice weekend.
Nice words. Thanks.

 

TinyPortal © 2005-2018