Jump to content
JohnLM

Suggestion of naming convention for enumerators

Recommended Posts

Specs:  Win7,  Delphi 11.2,  VCL 

 

I wrote some functions to convert string to byte and byte to string.  These functions work. For testing, I added several tbuttons and a memo on the form and began testing my ideas shown below.

 

function str2byte(str: string): TBytes;
begin
  setlength(str,length(str));
  result := TEncoding.utf8.getbytes(str);
end;

function byte2str(byt: TBytes): string;
begin
  result := TEncoding.ascii.getstring(byt);
end;

usage: 

procedure TForm1.Button1Click(Sender: TObject);
var strs: string; bytes: TBytes;
begin
  bytes := str2byte('ABC');         // string
  strs  := byte2str(bytes);         // bytes
  memo1.Lines.Add(byte2str(bytes)); // show it

  strs  := byte2str([68,69,70]);    // bytes
  bytes := str2byte(strs);          // string
  memo1.Lines.Add(byte2str(bytes)); // show it
end;

 

Now, I'd like to extend that to include an additional parameter to the encoding format (ansi, ascii, utf-16-big-endian unicode, utf-16 unicode, utf-7 and utf-8) as found here: 

https://docwiki.embarcadero.com/Libraries/Alexandria/en/System.SysUtils.TEncoding

 

(And then later, possibly extend upon it by building a class component of it (and other library functions/procedures that I build)). 
 

The issue I am having now is how to give the parameters their name.  I do not know if there are already built-in types for these and don't want to cause issues later on.  But so far, I thought about using these: (_ansi, _ascii, _utf7, _utf8, _utf16be and _utf16u).  Or, maybe create an enumerated list, i.e., 

type TMyEncoding = (_ansi, _ascii, _utf7, _utf8, _utf16be, _utf16u);

 

And rewriting the above output snippet: 

type TMyEncoding = (_ansi, _ascii, _utf7, _utf8, _utf16be, _utf16u);

function _byte2str(byt: TBytes; enc: TMyEncoding): string;
begin
  case integer(enc) of
    0: result := TEncoding.ascii.getstring(byt);
    1: result := TEncoding.ansi.getstring(byt);
    2: result := TEncoding.utf7.getstring(byt);
    3: result := TEncoding.utf8.getstring(byt);
    // ...
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  strs: string; bytes: TBytes;
begin
  bytes := str2byte('_ansi');              // string
  strs  := _byte2str(bytes,_ansi);         // bytes
  memo1.Lines.Add(_byte2str(bytes,_ansi)); // show it

//  strs  := byte2str([68,69,70]);    // bytes
//  bytes := str2byte(strs);          // string
//  memo1.Lines.Add(byte2str(bytes)); // show it
end;

So I am looking for some advice/suggestions on how to implement the names for each of the encodings that will not interfer with Delphi's built-in names.  TIA. 
 

Share this post


Link to post

There is no point to all of this effort in my opinion, just use methods of the TEncoding "class" directly. If you insist on writing your own wrappers do not declare your own enumeration for the encodings, just let your methods take a parameter of type TEncoding, to which you can then pass TEncoding.UTF8 or one of the other predefined encodings TEncoding offers.

  • Like 1

Share this post


Link to post

I agree with Peter, but on other hand there a scenario where i did similar thing, i needed to serialize many data in compact structure on disk, big data and they needed to be loaded and saved fast, so yes, sometimes it is needed, where the data will have first byte as the index of the encoder then the 4 bytes for the length then the data itself, even if this not case, as process of experimenting and learning it is good to do it once at least in life.

 

now to the OP question

15 hours ago, JohnLM said:

 


function str2byte(str: string): TBytes;
begin
  setlength(str,length(str));
  result := TEncoding.utf8.getbytes(str);
end;

function byte2str(byt: TBytes): string;
begin
  result := TEncoding.ascii.getstring(byt);
end;

 

Here i have few thing to point

1) SetLength is useless.

2) both function should have their parameters as const, both String and TBytes are managed types, it is better to tell the compiler that they are constant.

3) you are building your own library which is good thing in the long run, here i suggest to use distinctive functions for your own, prefix their names with something you will use and recognize, something like JmStr2Bytes, JmBytes2Str do this for all your code and you will have good time with autocomplete.

4) refrain if possible from using underscore _ , it is ugly, and later you will start to hate it, also the lowercase is something personal but why not give Camel a chance and see if it will click with you.

 

Really i am not sure about the question, is it about the functions/procedures names, the enums or both? but .... my opinion is one for all

use prefixes, Jm Jl jL Jm Lm Ml ML mL Mj , just pick any two letters, because it is nice and almost never conflict with other libraries you will use.

 

Share this post


Link to post

Yes, this is for process and learning.  I am learning about how to build Classes and Generics, and this idea came to me and I went with it to see how far I would get. 

Share this post


Link to post
On 10/21/2023 at 6:47 PM, Kas Ob. said:

3) you are building your own library which is good thing in the long run, here i suggest to use distinctive functions for your own, prefix their names with something you will use and recognize, something like JmStr2Bytes, JmBytes2Str do this for all your code and you will have good time with autocomplete.

I'm against this. These prefixes just add useless noise to final code and for big libs make coder to type more chars to get proper suggestion. IOW, they could be handy for lib developer but not for those who use that lib. However, prefixing do solve name clashes.

Edited by Fr0sT.Brutal

Share this post


Link to post
On 10/21/2023 at 3:26 AM, JohnLM said:

The issue I am having now is how to give the parameters their name.  I do not know if there are already built-in types for these and don't want to cause issues later on.  But so far, I thought about using these: (_ansi, _ascii, _utf7, _utf8, _utf16be and _utf16u).  Or, maybe create an enumerated list, i.e., 


type TMyEncoding = (_ansi, _ascii, _utf7, _utf8, _utf16be, _utf16u);

Why underlines? Just define enum items with any prefix, like encUtf8, encANSI,... Delphi strong typing won't let you use a member of another enum with the same prefix (type mismatch)

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

×