Jump to content
dummzeuch

formatting private const identifier = value

Recommended Posts

How do you think the following should be formatted?

1. Like this:

type
  TClass = class(TObject)
  private
    FSomeField: integer;
  private const SomeConstant = 5;
  end;

2. Like this:

type
  TClass = class(TObject)
  private
    FSomeField: integer;
    private const SomeConstant = 5;
  end;

Note: This is only about code where the 'private const SomeConstant = 5;' is in one line without a line feed in between.

 

And what about the following:

1. Like this:

type
  TClass = class(TObject)
  private
    FSomeField: integer;
  private const SomeConstant = 5;
    FSomeOtherField: string;
  end;

2. Like this:

type
  TClass = class(TObject)
  private
    FSomeField: integer;
    private const SomeConstant = 5;
    FSomeOtherField: string;
  end;

Note: This is only about code where the 'private const SomeConstant = 5;' is in one line without a line feed in between.

Share this post


Link to post

Are you aware that your second example won't compile? It needs an additional var keyword between the constant declaration and the following field declaration.

  • Like 1
  • Thanks 1

Share this post


Link to post
type
  TClass = class(TObject)
  private
    FSomeField: integer;
  private const 
    SomeConstant = 5;
  private type
    SomeType = integer;
  private class var
    SomeClassVar: integer;
  end;

etc. etc.

  • Like 2

Share this post


Link to post
1 hour ago, Uwe Raabe said:

Are you aware that your second example won't compile? It needs an additional var keyword between the constant declaration and the following field declaration.

No, I missed that one.

 

This is a contrived example not real code, personally I always put a line feed after the visibility modifier and rather than using var I start a new visibility section.

Share this post


Link to post
57 minutes ago, Anders Melander said:

type
  TClass = class(TObject)
  private
    FSomeField: integer;
  private const 
    SomeConstant = 5;
  private type
    SomeType = integer;
  private class var
    SomeClassVar: integer;
  end;

etc. etc.

 

My preference would be similar, but I'd start with private const and end with private fields.

 

 

  • Like 1

Share this post


Link to post

Just to be clear: I'm not asking whether you like the 'private const bla=blub;' in one line syntax, but how it should be indented:

 

1. like 'private' on its own line -> same indentation as the class/record

2. like other constants / fields / types -> one indentation more than the class/record

 

I assume that most (including myself) would like to split that line (whether between 'private' and 'const' or between 'const' and the identifier would be yet another point for debate.

  • Like 1

Share this post


Link to post
16 hours ago, Darian Miller said:

I'd start with private const and end with private fields.

Me too - and I would most often also start with the class vars and types. Of course it depends on the size and structure of the class; For a large class I would probably try to keep associated stuff close together as an overriding principle.

Share this post


Link to post

I think more consistent with Embarcadero style will be following, since there could be multiple consts/types/vars

 

  TClass = class(TObject)
  private
    FSomeField: integer;
  private
    const
      SomeConstant = 5;
      SomeOtherConstant = 6;
  private
    type
      SomeType = integer;
      SomeOtheType = string;
  private
    class var
      SomeClassVar: integer;
      SomeOtherClassVar: string;
  end;

 

  • Like 1

Share this post


Link to post

I will write in that way, I mean that I add identation for everything is inside the class definition.

I find it very comprensive simple to view, with very long definition too.

 

But surely it is 'cause I'm old ...

 

type
  TClass = class(TObject)
    private
      const
        SomeConstant = 5;
        SomeOtherConstant = 6;
    private
      type
        SomeType = integer;
        SomeOtheType = string;
    private
      class var SomeClassVar: integer;
      class var SomeOtherClassVar: string;
    private
      FSomeField: integer;
  end;

 

 

Share this post


Link to post
7 hours ago, EugeneK said:

I think more consistent with Embarcadero style will be following, since there could be multiple consts/types/vars

 


  TClass = class(TObject)
  private
    FSomeField: integer;
  private
    const
      SomeConstant = 5;
      SomeOtherConstant = 6;
  private
    type
      SomeType = integer;
      SomeOtheType = string;
  private
    class var
      SomeClassVar: integer;
      SomeOtherClassVar: string;
  end;

 

This is the style that pasfmt uses. It's most consistent to break after every visibility specifier because it does start a new section, and there may not be a visibility specifier before the next section:

type
  TClass = class(TObject)
  private
    const
      SomeConstant = 5;
    type
      SomeType = integer;
  end;

 

  • Thanks 1

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
×