如何命名一个好/有意义的类型?

问题描述 投票:0回答:2

Device_Manager.h

typedef enum
{
    DNM = 0x2A,

}TYPE_e;

typedef struct DEVICE_s* p_DEVICE;
typedef p_DEVICE(*FUNC)(char* name, TYPE_e type, uint32_t ip, uint16_t method, uint16_t groupRule);   

p_DEVICE DeviceManager_New(void);
p_DEVICE DeviceManager_Ctor(char* name, TYPE_e type, uint32_t ip, uint16_t method, uint16_t groupRule);
p_DEVICE DeviceManager_Dtor(p_DEVICE element);

Device_Manager.c

struct DEVICE_s
{
    uint32_t IP;
    TYPE_e Type;
    uint16_t Method;
    uint16_t GroupRule;
    char Name[40];
    FUNC fp_Ctor, fp_Dtor;    //this line needs modification
}DeviceSet[32],DeviceTemp;

p_DEVICE DeviceManager_InitObject(p_DEVICE* self)
{
    (*self) = DeviceManager_New();
    (*self)->IP = 0;
    (*self)->Type = 0;
    (*self)->Method = 0;
    (*self)->GroupRule = 0;
    memset((*self)->Name, 0, NAME_SIZE);
    (*self)->fp_Ctor = DeviceManager_Ctor;
    (*self)->fp_Dtor = DeviceManager_Dtor;    // warning: assign to wrong type
    return (*self);
}
p_DEVICE DeviceManager_New(void)
{
    return &DeviceTemp;
}
p_DEVICE DeviceManager_Ctor(char* name, TYPE_e type, uint32_t ip, uint16_t method, uint16_t groupRule)
{
    memcpy(DeviceTemp.Name, name, sizeof(name));
    DeviceTemp.Type = type;
    DeviceTemp.IP = ip;
    DeviceTemp.Method = method;
    DeviceTemp.GroupRule = groupRule;
    return &DeviceTemp;
}
p_DEVICE DeviceManager_Dtor(p_DEVICE element)
{
    element->IP = 0;
    element->Type = 0;
    element->Method = 0;
    element->GroupRule = 0;
    memset(element->Name, 0, NAME_SIZE);
    return element;
}

这是我第一次实现封装概念并遇到了一些问题。

在头文件中,我使用typedef将类型“FUNC”定义为函数指针。

我认为这个名字“FUNC”不够明确,因为这种命名方式会导致:

struct DEVICE_s
{
    uint32_t IP;
    TYPE_e Type;
    uint16_t Method;
    uint16_t GroupRule;
    char Name[40];
    FUNC1 fp_Ctor;    //not clear
    FUNC2 fp_Dtor;    //not clear
}DeviceSet[32],DeviceTemp;

fp_Ctor和fp_Dtor都是相同的类型(函数指针),并且参数的编号不同。

我一直在努力命名类型。可以提供一些关于命名类型的建议吗?

c encapsulation naming
2个回答
4
投票

这有点主观,但我会首先放弃在typedef后面隐藏指针的样式,并在其上涂抹一些“匈牙利符号”。很多C程序员都会同意。

所以第一个建议就是去

typedef struct DEVICE_s DEVICE_s;

然后根据DEVICE_s*定义不透明的界面。您不仅可以更轻松地阅读,还可以过滤混淆,就像调用者试图将p_DEVICE*传递给用户定义的函数等一样,因为他们没有意识到他们已经有了指针。 (Win32 API严重受此问题的影响。)

然后你的构造函数变成了

DEVICE_s* DeviceManager_Ctor ( ...

并且所有成员函数将按值使用DEVICE_s*参数而不是p_DEVICE。调用者必须声明指针而不是对象,使他们清楚地知道他们有一个指向不完整类型的指针,他们不能/应该玩什么。

接下来,您也可以将指针隐藏在函数指针中。这不是一个问题,但保持一致是很好的:

typedef DEVICE_s* DeviceManager_Ctor_t ( ...

您的函数指针定义将变为:

DeviceManager_Ctor_t* Ctor;

您可以删除“fp”命名,因为很明显该类型是函数指针。


作为旁注,我建议避免使用obj.member表示法模仿C ++成员函数。因为在C中,缺少this指针,你最终会得到obj.member(&obj, ...),这是多余的。

而只是接受C是它的方式,并将成员函数称为DeviceManager_Ctor(obj);,其中obj被声明为DEVICE_s* obj;。可读OO代码的关键是为属于“类”的所有函数使用一致的源代码前缀,如您所做的那样:DeviceManager_


-1
投票

Linux kernel coding style建议谨慎使用typedef。 “方法”或函数指针通常分组为操作结构。例如:

// DeviceManager.h
//

struct device_operations;

struct device {
    const char *name;
    const struct device_operations *ops;
};

struct device_operations {
    void (*ctor)(struct device *d, const char *name);
    void (*dtor)(struct device *d);
};

struct device *DeviceManager_new(const char *name);


// DeviceManager.c
//

#include <stdlib.h>

static void DeviceManager_ctor(struct device *d, const char *name);
static void DeviceManager_dtor(struct device *d);

static struct device_operations DeviceManager_ops = {
    .ctor = DeviceManager_ctor,
    .dtor = DeviceManager_dtor,
};

struct device *DeviceManager_new(const char *name)
{
  struct device *d = malloc(sizeof(*d));
  if (!d)
    return NULL;

  d->ops = &DeviceManager_ops;
  d->ops->ctor(d, name);

  return d;
}

static void DeviceManager_ctor(struct device *d, const char *name) { /* ... */ }
static void DeviceManager_dtor(struct device *d) { /* ... */ }
© www.soinside.com 2019 - 2024. All rights reserved.