Recent

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

guest65109

  • Guest
Re: What is the Pascal convention?
« Reply #15 on: January 10, 2020, 05:50:56 pm »
Well don't do it then. You don't have to do it, as several people have explained to you it's a convention rather than a hard-and-fast syntactic rule.

But if you don't do it you're making life more difficult for people from whom you ask help. And if you don't stick to project conventions you're making extra work for whoever processes your contributions.

MarkMLl

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  ;)

guest65109

  • Guest
Re: What is the Pascal convention?
« Reply #16 on: January 10, 2020, 05:52:51 pm »
I rarely use pointers, let alone double pointers. There might be use cases for double pointers but I cannot even imagine one.

For low level API converted from C without an OOP wrapper. These double pointers are very popular and sometime we have no choice but having to use these libraries  8)

rvk

  • Hero Member
  • *****
  • Posts: 4397
Re: What is the Pascal convention?
« Reply #17 on: January 10, 2020, 06:02:50 pm »
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  ;)
Ok, you can do it your own way.

Create a type Foo = class.
Now how are you going to name the instance of Foo (which is different from the class-definition) ????

In the mentioned convention it would be
type
  TFoo = class;
var
  Foo: TFoo;

Not to mention when you are declaring and instantiating the class
Convention:
  Foo := TFoo.Create;

It's clear here that TFoo is the class and Foo is the instance (variable).
When using
  myFoo := Foo.Create;
It is not clear that Foo is a class (it could as easily be a variable).
That's why there is a T before the class-names in this convention.

But if you work alone... (and nobody sees your code) it's entirely up to you.

devEric69

  • Sr. Member
  • ****
  • Posts: 384
Re: What is the Pascal convention?
« Reply #18 on: January 10, 2020, 06:13:00 pm »
Quote
Why Class name must be prepend with T?

Because it's also a type, and the most important of all: so capital T at minimum (lowercase T would be incongruous) ^^.

Quote
I also found people do not use Button1 as name but rename it to btnClick or something like that. What is the convention used there?

As it has already been said, this is what is called the "Hungarian" notation.
See http://www.sourceformat.com/coding-standard-delphi-econos.htm for the component's name.
And see http://web.archive.org/web/20170124021121/http://delphi.about.com/od/standards/l/bldnc.htm for the variable's names.

A trick I took from a site, is to declare the names of oFoo objects when they were created to be destroyed in the same "scope" (within a function).
And to declare them o_Foo when it's not in the scope where they are to be destroyed.

ex..:
- Fo_Foo, or frm_This: field that stores a pointer created in the remote, and it's in the remote that created it, that it should be destroyed.
- oFoo or frmThis as var, in a procedure that creates it, and must then destroy it.
- and goFoo for global variables (like those created in intialization, and detroyed in finalization.
I also use this for parameter's name declarations.

Quote
Yes. I know. Bad practices build up over time and it's very hard to change. I can't get up early for more than ten years despite how hard I tried.

From an information theory point of view, I agree with you: the conventions of the disappeared Borland are from another age, amho.
« Last Edit: January 13, 2020, 07:14:02 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8900
  • FPC developer.
Re: What is the Pascal convention?
« Reply #19 on: January 10, 2020, 07:46:38 pm »
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 ?


ASerge

  • Hero Member
  • *****
  • Posts: 1694
Re: What is the Pascal convention?
« Reply #20 on: January 10, 2020, 08:14:35 pm »
Quote
Spaces

Don't use spaces around operators, colons, parentheses etc.

{ correct }
p:=p+i;

{ incorrect }
p := p + i ;
I really don't like it if all code is written without spaces. It reads terrible.
Don't forget that this is for FPC developers. For Lazarus, just the opposite is accepted (https://wiki.freepascal.org/DesignGuidelines).

lainz

  • Hero Member
  • *****
  • Posts: 3809
  • Leandro Diaz
Re: What is the Pascal convention?
« Reply #21 on: January 11, 2020, 12:05:02 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).
https://lainz.github.io/
Download LazPaint https://lazpaint.github.io/
Download BGRABitmap and BGRAControls https://github.com/bgrabitmap

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8900
  • FPC developer.
Re: What is the Pascal convention?
« Reply #22 on: January 11, 2020, 12:05:56 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).

The point is that all that, even the casing is also just a convention.

lainz

  • Hero Member
  • *****
  • Posts: 3809
  • Leandro Diaz
Re: What is the Pascal convention?
« Reply #23 on: January 11, 2020, 12:23:55 am »
The point is that all that, even the casing is also just a convention.

I'm not sure if everything human is a convention, at least the languages are conventions, the ones we speak and write.
https://lainz.github.io/
Download LazPaint https://lazpaint.github.io/
Download BGRABitmap and BGRAControls https://github.com/bgrabitmap

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8900
  • FPC developer.
Re: What is the Pascal convention?
« Reply #24 on: January 11, 2020, 12:45:14 am »
The point is that all that, even the casing is also just a convention.

I'm not sure if everything human is a convention, at least the languages are conventions, the ones we speak and write.

But the human semantics and syntax aren't fixed as a compilers. But the identifier naming isn't.

lainz

  • Hero Member
  • *****
  • Posts: 3809
  • Leandro Diaz
Re: What is the Pascal convention?
« Reply #25 on: January 11, 2020, 01:12:34 am »
We need Jarvis (iron man assistant) to talk with it and write code that's like a real language. Not fixed. I'm dreaming I know.
https://lainz.github.io/
Download LazPaint https://lazpaint.github.io/
Download BGRABitmap and BGRAControls https://github.com/bgrabitmap

440bx

  • Hero Member
  • *****
  • Posts: 2048
Re: What is the Pascal convention?
« Reply #26 on: January 11, 2020, 02:34:31 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.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: What is the Pascal convention?
« Reply #27 on: January 11, 2020, 03:25:18 am »
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.
-Bee-

A long time pascal lover.

Handoko

  • Hero Member
  • *****
  • Posts: 3913
  • My goal: build my own game engine using Lazarus
Re: What is the Pascal convention?
« Reply #28 on: January 11, 2020, 06:24:48 am »
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.

+1

PascalDragon

  • Hero Member
  • *****
  • Posts: 2427
  • Compiler Developer
Re: What is the Pascal convention?
« Reply #29 on: January 11, 2020, 11:10:01 am »
The thing I hate most is prepending a T before any class name. Why? We already know it's a Type. In many languages, Types are equivalent with Classes. It doesn't make any sense at all. The same for prepending a P before pointer name. I have seen many examples of how stupid and ridiculous it is. The most obvious example: double pointer (PP). By following this brain dead convention, you have to do a lot of typedefs. These typedefs are useless. You accuse C of using so many typedefs, but you don't see you are, too.

For example, we have a record Foo.

Pointer to Foo: type PFoo = ^Foo;

Pointer to pointer to Foo: type PPFoo = ^PFoo;

How many `P` will you prepend to it if you have to make a pointer to a pointer to a pointer to Foo?

As 440bx wrote above, it's convenient. If I have a variable declaration x: PFoo; I don't need to guess what type x might be, instead I know it's a pointer and thus I can allocate it using New and free it using Dispose and work with it using the + operator and the […] accessor. Similar for e.g. classes, parameters, fields: I know right away what is a type, what is a parameter and what is a field. Code can get complex. And if I can spot such things without even needing the IDE to help me, that greatly simplifies things.

I rarely use pointers, let alone double pointers. There might be use cases for double pointers but I cannot even imagine one.

Double pointers are mainly relevant when dealing with C code. E.g. if you pass in memory for an "array" of pointers:
Code: C  [Select][+][-]
  1. void dumparray(int** arr, int count);
In Pascal there are two possible translations (assuming that dumparray does not modify arr):
Code: Pascal  [Select][+][-]
  1. // 1
  2. procedure dumparray(arr: PPCInt; count: CInt); cdecl; external;
  3. // 2
  4. procedure dumparray(constref arr: PCInt; count: CInt); cdecl; external;
The first declaration is closer to the original C declaration, the second can be considered more convenient.

 

TinyPortal © 2005-2018