1
\$\begingroup\$

The following are an example of methods supported by a proprietary device:

Monitor
ControlTemp
PutPeakInfo
GetPeakInfo

I have a class that builds the packets for the above corresponding methods:

GetMonitorPacket
GetControlTempPacket
GetPutPeakInfo

Example:

public byte[] GetMonitorPacket(int ddcNum)
    {
        byte[] monitorPacket = new byte[MONITOR_PACKET_BYTE_COUNT];
        Buffer.BlockCopy(START_HEADER_BYTES, 0, monitorPacket, 0, START_HEADER_BYTES.Count());
        monitorPacket[2] = (byte) ddcNum;
        monitorPacket[3] = 0x01;
        byte checksum = 0;
        foreach (var b in monitorPacket)
        {
            checksum = (byte) (((ushort) checksum + (ushort) b) & 0xff);
        }
        checksum = (byte) (checksum ^ 0x55);
        monitorPacket[4] = checksum;
        return monitorPacket;
    }

All they do is return the bytes in proper format, which then will be sent to the proprietary device via TCP to call the methods. Now I'm screwed on my naming convention for GetPeakInfo method.

  1. How do I name this? What about GetGetPeakInfoPacket (two gets for a method name)?
  2. Do I break the naming convention here as an exception case and drop one of the gets?
  3. Do I use some other naming convention?

Actually, GetPutPeakInfo sounds a little bit weird too, but at least there's no duplication.

\$\endgroup\$
  • \$\begingroup\$ For example, Microsoft doesn't seem to care. "How do we call method, which returns MethodInfo for property getter? PropertyInfo.GetGetMethod(), no problem." In your case, i like Create prefix. But, iguess, its a matter of taste. \$\endgroup\$ – Nikita B Jul 12 '13 at 10:30
6
\$\begingroup\$

How about using Generate as prefix?

GenerateMonitorPacket
GenerateControlTempPacket
GeneratePutPeakInfoPacket
GenerateGetPeakInfoPacket

Alternatively, you can also create a separate class for each type of packet generator. You've only shown the code to create the packet for Monitor, so I don't know how complex the code for the other packets is, but separating these four methods into their own class might be logical and beneficial in the long run. If you create separate classes, you put the name of the packet (Monitor, ControlTemp, PutPeakInfo and GetPeakInfo) in the class name.

public class MonitorPacketGenerator
{
    public byte[] Generate(int ddcNum) { ... }
}
\$\endgroup\$
  • \$\begingroup\$ Best I could've come up myself so far. :) \$\endgroup\$ – Abbas Jul 12 '13 at 7:56
3
\$\begingroup\$

I have a class that builds the packet

Then why don't you call your methods Build*, e.g. BuildMonitorPacket?

I think such naming is also clearer, because “build” says more clearly what does the method do, “get” is too general. (For example, it would make sense if a “get” method returned the same result every time, but not so much for a “build” method.)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.