class-design

Best practice: ordering of public/protected/private within the class definition?

对着背影说爱祢 提交于 2019-11-27 09:34:27
问题 I am starting a new project from the ground up and want it to be clean / have good coding standards. In what order do the seasoned developers on here like to lay things out within a class? A : 1) public methods 2) private methods 3) public vars 4) private vars B : 1) public vars 2) private vars 3) public methods 4) private methods C : 1) public vars 2) public methods 3) private methods 4)private vars I generally like to put public static vars at the top, but then would a public static method

PHP 5: const vs static

一笑奈何 提交于 2019-11-27 06:03:24
In PHP 5, what is the difference between using const and static ? When is each appropriate? And what role does public , protected and private play - if any? Matt Huggins In the context of a class, static variables are on the class scope (not the object) scope, but unlike a const, their values can be changed. class ClassName { static $my_var = 10; /* defaults to public unless otherwise specified */ const MY_CONST = 5; } echo ClassName::$my_var; // returns 10 echo ClassName::MY_CONST; // returns 5 ClassName::$my_var = 20; // now equals 20 ClassName::MY_CONST = 20; // error! won't work. Public,

OO Javascript constructor pattern: neo-classical vs prototypal

牧云@^-^@ 提交于 2019-11-27 05:59:46
I watched a talk by Douglas Crockford on the good parts in Javascript and my eyes were opened. At one point he said, something like, "Javascript is the only language where good programmers believe they can use it effectively, without learning it." Then I realized, I am that guy. In that talk, he made some statements that for me, were pretty surprising and insightful. For example, JavaScript is the most important programming language on the planet. Or it is the most popular language on the planet. And, that it is broken in many serious ways. The most surprising statement he made, for me, was

python circular imports once again (aka what's wrong with this design)

时光毁灭记忆、已成空白 提交于 2019-11-27 05:10:15
问题 Let's consider python (3.x) scripts: main.py: from test.team import team from test.user import user if __name__ == '__main__': u = user() t = team() u.setTeam(t) t.setLeader(u) test/user.py: from test.team import team class user: def setTeam(self, t): if issubclass(t, team.__class__): self.team = t test/team.py: from test.user import user class team: def setLeader(self, u): if issubclass(u, user.__class__): self.leader = u Now, of course, i've got circular import and splendid ImportError. So,

Improve this PHP bitfield class for settings/permissions?

…衆ロ難τιáo~ 提交于 2019-11-27 04:22:47
问题 I have been trying to figure out the best way to use bitmask or bitfields in PHP for a long time now for different areas of my application for different user settings and permissions. The farthest I have come so far is from a class contributed by svens in the Stack Overflow post Bitmask in PHP for settings? . I have slightly modified it below, changing it to use class constants instead of DEFINE and making sure the get method is passed an int only. I also have some sample code to test the

Create custom exception or use built-in exceptions?

ぐ巨炮叔叔 提交于 2019-11-27 03:28:22
问题 Currently I'm in the process of writing a client class that utilizes DNS, Sockets, and SSL among other classes that love to throw exceptions. Other people will be implementing this class, so I was wondering what the best practice is for throwing exceptions. Should I create my own custom exception so they know that it is my class throwing the exception or should I allow the classes and methods I call (DNS, Sockets, etc.) to throw their own exceptions? Currently, the code is in the hundreds of

Howto design for extension

不想你离开。 提交于 2019-11-27 01:49:38
问题 There is a Checkstyle rule DesignForExtension. It says: if you have a public/protected method which is not abstract nor final nor empty it is not "designed for extension". Read the description for this rule on the Checkstyle page for the rationale. Imagine this case. I have an abstract class which defines some fields and a validate method for those fields: public abstract class Plant { private String roots; private String trunk; // setters go here protected void validate() { if (roots == null

When/why to make function private in class?

别来无恙 提交于 2019-11-27 01:34:06
问题 When should i make a function private and why is it good idea? 回答1: You should make a function private when you don't need other objects or classes to access the function, when you'll be invoking it from within the class. Stick to the principle of least privilege , only allow access to variables/functions that are absolutely necessary. Anything that doesn't fit this criteria should be private . 回答2: I usually make the helper functions private . But what is helper seems to be vague. So let me

Why is 16 byte the recommended size for struct in C#?

六月ゝ 毕业季﹏ 提交于 2019-11-27 01:31:49
I read the Cwalina book (recommendations on development and design of .NET applications). He says that a good designed struct has to be less than 16 bytes in size (for performance purposes). Why exactly is this? And (more important) can I have larger struct with same efficiency if I run my .NET 3.5 (soon to be .NET 4.0) 64-bit application on Core i7 under Windows 7 x64 (is this limitation CPU / OS based)? Just to stress again - I need as efficient struct as it is possible. I try to keep it on the stack all the time. The application is heavily multi-threaded and runs on sub-millisecond

Class members that are objects - Pointers or not? C++

不羁岁月 提交于 2019-11-27 00:02:02
问题 If I create a class MyClass and it has some private member say MyOtherClass, is it better to make MyOtherClass a pointer or not? What does it mean also to have it as not a pointer in terms of where it is stored in memory? Will the object be created when the class is created? I noticed that the examples in QT usually declare class members as pointers when they are classes. Regards Mark 回答1: If I create a class MyClass and it has some private member say MyOtherClass, is it better to make