lc-soft/trad

Class method naming style #1

lc-soft posted onGitHub

Description:

Class instance methods and static methods have the same naming style and are easy to conflict.

For example:

class MyDict {
  fetchValue() {}
  static createStringKeyDict() {}
  static createIntKeyDict() {}
}

Output:

typedef struct MyDictRec_* MyDict;

void *MyDict_FetchValue(MyDict);
Dict MyDict_CreateStringKeyDict();
Dict MyDict_CreateIntKeyDict();
MyDict MyDict_New();

Reference:

https://github.com/lc-soft/trad/blob/d4a022e74bd3fcb6b7cf0d6c9fea031ad87b8685/packages/trad/index.js#L691-L704

Cost:

30 miutes

benefits:

Make class instance methods and static methods in C code easier to identify

Assignee:

Who do you want to be assignee for this feature?

  • I will implement this feature
  • Other contributors
  • Maintainers
  • I don't know

Contribution:

For this feature, I will provide these contributions:

  • Contribute documentions
  • Contribute testcases
  • Develop other feuatres, or fix existing bugs
  • Write an article to promote this project
  • Fund this issue on IssueHunt
  • Donate this project on OpenCollective
  • I don't want to provide any help

Solution 1

No need to change. Class instance methods and static methods should not have the same name to avoid ambiguity.

posted by lc-soft almost 6 years ago

Solution 2

  • For class instance method, use lower camel case style for prefix, its naming style is the same as variable.
  • For class static method, use upper camel case style for prefix, its naming style is the same as class.

Example output:

void *myDict_FetchValue(MyDict);
Dict MyDict_CreateStringKeyDict();
Dict MyDict_CreateIntKeyDict();
MyDict MyDict_New();
posted by lc-soft almost 6 years ago

Solution 3

Use a double underscore as a separator for class static method. It looks like C++ style: dict.fetchValue(), MyDict::createIntKeyDict()

Example output:

void *MyDict_FetchValue(MyDict);
MyDict MyDict__CreateStringKeyDict();
MyDict MyDict__CreateIntKeyDict();
MyDict MyDict__New();
posted by lc-soft almost 6 years ago

@lc-soft has funded $2.00 to this issue.


posted by issuehunt-app[bot] almost 6 years ago

Solution 4

Define:

typedef struct MyDictRec_* MyDict_t;

// Class instance struct
struct MyDictRec_ {
  // Class instance property
  void *data;
  // Class instance method
  void* (*fetchValue)(MyDict_t);
};

// Class static methods
struct {
  MyDict_t (*new)(void);
  void (*delete)(MyDict_t);
  MyDict_t (*createStringKeyDict)(void);
  MyDict_t (*createIntKeyDict)(void);
} MyDict;

Usage:

MyDict_t dict = MyDict.new();
MyDict_t dict = MyDict.createStringKeyDict();
MyDict_t dict = MyDict.createIntKeyDict();

void *value = dict->fetchValue(dict, "keyname");

MyDict.delete(dict);

Problems:

  • Initializing an instance requires assigning a value to each function pointer
  • Methods are converted to function pointers and defined in structures that are not easy to read
posted by lc-soft almost 6 years ago

@lc-soft has rewarded $1.70 to @d4yvector. See it on IssueHunt

  • :moneybag: Total deposit: $2.00
  • :tada: Repository reward(5%): $0.10
  • :wrench: Service fee(10%): $0.20
posted by issuehunt-app[bot] over 5 years ago

Fund this Issue

$2.00
Rewarded

Rewarded pull request

Recent activities

d4yvector was rewarded by lc-soft for lc-soft/trad# 1
over 5 years ago
lc-soft submitted an output to  lc-soft/ trad# 1
over 5 years ago