coding-style

Can I create a new struct on the heap without defining a constructor?

余生颓废 提交于 2019-12-06 01:31:10
问题 I understand that there are very few differences between structs and classes in c++ (two?). Be that as it may, I've been instructed to use structs to define simple little things like nodes that might not need member functions (despite the fact that I could technically include include member functions). For instance I might define a node as a private member of a linked list class as follows: class LinkedList { struct Node { MyObject *data; Node *next; }; Node *list; }; In this case, however,

What is the correct way of ensuring a single instance of a class? [duplicate]

試著忘記壹切 提交于 2019-12-05 23:40:25
This question already has answers here : What is the correct way to write a singleton pattern in Ruby? (3 answers) What is an efficient way to implement a singleton pattern in Java? [closed] (29 answers) Closed last year . In java I would create something like this: private static MyClass instance; public static MyClass getInstance() { if(instance != null) { return instance; } instance = new MyClass(); return instance; } What is the appropriate way to obtain the same functionality in ruby? Update: I've read about 'include Singleton' but when I tried to do it in irb on Ruby 1.9 I got: [vertis

What are The Valid & Readable approaches to Commenting in PHP5?

≡放荡痞女 提交于 2019-12-05 23:29:22
问题 In the past 2 months that I have been learning PHP, I have identified more than two styles people use to comment code! I haven't seen much consistency... which I think usually means artists at work . So I wondered: what are the valid ways to comment which are still readable/practical? Seeing all the valid possibilities in 1 place side by side will provide the overview that I am looking for to improve commenting /* | This is what I now use (5chars/3lines) I name it star*wars \* 回答1: Quoting

Why use = to initialise a primitive type in C++?

落花浮王杯 提交于 2019-12-05 22:05:36
问题 Where I work, people mostly think that objects are best initialised using C++-style construction (with parentheses), whereas primitive types should be initialised with the = operator: std::string strFoo( "Foo" ); int nBar = 5; Nobody seems to be able to explain why they prefer things this way, though. I can see that std::string = "Foo"; would be inefficient because it would involve an extra copy, but what's wrong with just banishing the = operator altogether and using parentheses everywhere?

Accessor Method Performance and Optimization

。_饼干妹妹 提交于 2019-12-05 21:40:49
问题 Often, I come across code where the Getter method is repeatedly used/abused to get some value or pass it as a method parameter, for ex: public class Test { public void someMethod() { if(person.getName() != null && person.getName().equalsIgnoreCase("Einstein")) { method1(person.getName()); } method2(person.getName()); method3(person.getName()); method4(person.getName()); } } I usually code it, as below: public class Test { public void someMethod() { String name = person.getName(); if(name !=

C# Action/Delegate Style Question

丶灬走出姿态 提交于 2019-12-05 21:33:49
问题 What is considered better style for an event definition: public event Action<object, double> OnNumberChanged; or public delegate void DNumberChanged(object sender, double number); public event DNumberChanged OnNumberChanged; The first takes less typing, but the delegate one gives names to the parameters. As I type this, I think number 2 is the winner, but I could be wrong. Edit: A different (third) approach is the winner. Read below. 回答1: Neither 1 or 2. A third option is the winner public

Readable convention for unpacking single value tuple

余生长醉 提交于 2019-12-05 21:13:30
问题 There are some related questions about unpacking single-value tuples, but I'd like to know if there is a preferred method in terms of readability for sharing and maintaining code. I'm finding these to be a source of confusion or misreading among colleagues when they involve a long function chain such as an ORM query. Is there some convention for this similar to the pep8 guidelines? If not, which is the clearest, most readable way to do it? Below are the ways I've tried, and my thoughts on

PHP Constants: Advantages/Disadvantages

℡╲_俬逩灬. 提交于 2019-12-05 21:01:20
问题 Lately I've been in the habit of assigning integer values to constants and simply using the constant name as a means of identifying its purpose. However, in some cases this has resulted in the need to write a function like typeToString($const) when a string representation is needed. Obviously this is inefficient and unneccesary, but is only an issue every once and a while. So my question is, are there any other tradeoffs I should consider? Which case is considered to be cleaner/more standards

May I write {x,a,b}//Do[…,#]& instead of Do[…,{x,a,b}]?

泄露秘密 提交于 2019-12-05 20:44:26
问题 I'm in love with Ruby. In this language all core functions are actually methods. That's why I prefer postfix notation – when the data, which I want to process is placed left from the body of anonymous processing function, for example: array.map{...} . I believe, that it has advantages in how easy is this code to read. But Mathetica, being functional (yeah, it can be procedural if you want) dictates a style, where Function name is placed left from the data. As we can see in its manuals, // is

Initializing disposable resources outside or inside try/finally

喜欢而已 提交于 2019-12-05 20:15:34
I have seen two ways of acquiring and disposing resources. Either: Resource resource = getResource(); try { /* do something with resource */ } finally { resource.close(); } or: Resource resource = null; try { resource = getResource(); /* do something with resource */ } finally { if (resource != null) resource.close(); } I was wondering which style is preferable. The first one avoids the if condition, while the second one (I presume) handles the case of thread abort right after the assignment but before entering the try block. What other pros and cons do these styles have over each other? Which