Recent

Author Topic: Best name for procedure  (Read 447 times)

LemonParty

  • Hero Member
  • *****
  • Posts: 554
Best name for procedure
« on: July 07, 2026, 01:58:06 pm »
I building library and there I have a procedure. This procedure look like this:
Code: Pascal  [Select][+][-]
  1. procedure <Name>(constref Buf: ShortInt; LH: Word; Count: SizeUInt; var Mask);
What it does? It take Count ShortInts from buffer and create a mask where each bit is set if element from buffer is in range set by parameter LH. The question is what is the better name for this procedure? I have a few variants:
1. MaskInRange;
2. MaskRange;
3. MaskInRng;
4. MaskRng;
5. InRange;
6. Range;
7. InRng;
8. Rng.
Maybe you have your variant.
« Last Edit: July 07, 2026, 02:11:58 pm by LemonParty »
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

runewalsh

  • Full Member
  • ***
  • Posts: 130
Re: Best name for procedure
« Reply #1 on: July 07, 2026, 02:34:59 pm »
Do you have any use cases, or is this just a function easy to implement in assembly? :)
In particular, the LH parameter looks downright human-hostile and assembly-centric.
Int8InRangeToBits.

rvk

  • Hero Member
  • *****
  • Posts: 7061
Re: Best name for procedure
« Reply #2 on: July 07, 2026, 04:08:52 pm »
 :P :P
Code: Pascal  [Select][+][-]
  1. procedure foo(constref Buf: ShortInt; LH: Word; Count: SizeUInt; var Mask);

Hey... it's better than Rng  ;)

Quote
What it does? It take Count ShortInts from buffer and create a mask where each bit is set if element from buffer is in range set by parameter LH.
5 to 8 can be eliminated directly because it doesn't mention Mask.
It creates a mask. So... something like CreateMaskFromRange() or CreateMaskInRange().

This will be an external library? Otherwise why not just use Array as parameter (then there is no need for Count parameter).

If you need to keep Count, then I would switch the LH:Word and Count:SizeUInt parameters (Count directly after Buf).

And what is LH? A High AND Low parameter in one? Yeah... not really intuitive  ;)

Weiss

  • Full Member
  • ***
  • Posts: 247
Re: Best name for procedure
« Reply #3 on: July 07, 2026, 05:29:32 pm »
intToBitMask

procedure takes integer (int) and converts (To) into (bitMask).

It would make sense if you had a collection of procedures doing same thing (intToBitMask) but from different criteria. THEN you would need to specify "if int is in range specified by parameter LH"

like intToBitMaskFromRange

I do not like abbreviated words in names, like "rng". Those only make sense when you use them a lot and get used to the name. If this is something you use only occasionally, full name is better. Type faster that all.

LemonParty

  • Hero Member
  • *****
  • Posts: 554
Re: Best name for procedure
« Reply #4 on: July 07, 2026, 05:58:41 pm »
There is no actual need to specify Int in the name of procedure because this is done in parameters (also I want to have overloads for bytes, smallints, words and etc).
This is a good thought to swap Count and LH.
LH stands for: low bound and high bound.
I work with assembler so this is a reason why I don't want to have more than 4 parameters. In x86-64 in Windows if you have less or equal to 4 parameters all parameters are passed through registers and you need no stack.
« Last Edit: July 07, 2026, 06:00:32 pm by LemonParty »
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

LemonParty

  • Hero Member
  • *****
  • Posts: 554
Re: Best name for procedure
« Reply #5 on: July 07, 2026, 06:13:53 pm »
Yep, "Rng" is kind of not Pascal way of naming things.

I think I don't need even "Mask" in name because I planing to dedicate a whole unit for extracting masks so "Mask" will be in the name of unit.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Warfley

  • Hero Member
  • *****
  • Posts: 2070
Re: Best name for procedure
« Reply #6 on: July 07, 2026, 09:01:03 pm »
First things first, if it takes more than 1 shortint, then why does the parameter is typed as if it only uses one? With either of those names I could not guess what the function does simply because of that fact.

This is an example of a function I could understand:
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3.  
  4. procedure ElementsInRange(const Elements: array of ShortInt; const minVal, maxVal: ShortInt; var ResultMask);
  5. var
  6.   i: SizeInt;
  7. begin
  8.   for i:=low(Elements) to high(Elements) do
  9.     if (Elements[i] <= maxVal) and (Elements[i] >= minVal) then
  10.       TByteArray(ResultMask)[i div 8] += 1 shl (i mod 8);
  11. end;
  12.  
  13. var
  14.   i: Integer;
  15.   Mask: Byte;
  16.   Elems: Array[0..1000] of ShortInt;
  17. begin
  18.   Randomize;
  19.   for i:=0 to 1000 do
  20.     Elems[i]:=Random(256)-128;
  21.   ElementsInRange(Elems[5..16], -10, 100, Mask);
  22.   WriteLn(Mask);
  23.   ReadLn;
  24. end.

Note that by the use of an open array, it is clearly indicated by the typesystem that it expects more than one value. The open array internally is nothing else than a pointer and a length parameter, so it's exactly the same you are building with buf and count. And it can be easily used with slices so you do not even need to do some pointer arithmetic to use it. That would be the native pascal way.

LemonParty

  • Hero Member
  • *****
  • Posts: 554
Re: Best name for procedure
« Reply #7 on: July 08, 2026, 10:27:44 pm »
Warfley, your solution looks good and handy. But actually it uses 5 parameters under the hood. But I think I can use it as an overload to my low level procedure. Thank you.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018