coding-style

C++: An abstract class as a member

主宰稳场 提交于 2019-12-10 04:24:29
问题 I have a question about style. I have a class (in my case an Option) that depends on the value of an exogenous object (Interest Rate). My goal is to create a abstract base class for the exogenous object (Rate) so that I can construct variations, say SimulatedRate or ConstantRate, that will work inside my depending class, Option. However, I'm finding in C++, since I obviously cannot instantiate a abstract base class, I must store either a pointer or a reference to the base class. My concern is

Bind a char to an enum type

╄→гoц情女王★ 提交于 2019-12-10 03:59:51
问题 I have a piece of code pretty similar to this: class someclass { public: enum Section{START,MID,END}; vector<Section> Full; void ex(){ for(int i=0;i<Full.size();i++) { switch (Full[i]) { case START : cout<<"S"; break; case MID : cout<<"M"; break; case END: cout<<"E"; break; } } } }; Now imagine I have much more enum types and their names are longer.... well what i get is not a very good looking code and i was wondering if it possible to bind a specific char to an enum type and maybe do

Variable declaration placement guidelines in Java [closed]

末鹿安然 提交于 2019-12-10 03:50:28
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 8 years ago . There seems to be two accepted variable declaration placements for Java variables, each with different raison d'être . From the Sun's code conventions we can see: Put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces "{" and "}".) Don't wait to declare variables until

Safest way to change variable names in a project

╄→尐↘猪︶ㄣ 提交于 2019-12-10 03:49:11
问题 So I've been working on a relatively large project by myself, and I've come to realise that some of the variable names earlier on were.. less than ideal. But how does one change variable names in a project easily? Is there such a tool that can go through a project directory, parse all the files, and then replace the variable names to the desired one? It has to be smart enough to understand the language I imagine. I was thinking of using regexp (sed/awk on linux?) tools to just replace the

Is glib usable in an unobtrusive way?

瘦欲@ 提交于 2019-12-10 03:12:34
问题 I was looking for a good general-purpose library for C on top of the standard C library, and have seen several suggestions to use glib. How 'obtrusive' is it in your code? To explain what I mean by obtrusiveness, the first thing I noticed in the reference manual is the basic types section, thinking to myself, "what, am I going to start using gint , gchar , and gprefixing geverything gin gmy gcode gnow?" More generally, can you use it only locally without other functions or files in your code

do interfaces belong in files of their own

混江龙づ霸主 提交于 2019-12-10 03:05:50
问题 As as rule of thumb I generally put classes in a file of their own. Visual studio seems to encourage this but what is appropriate with regards to interfaces? e.g. I have Class Foo that implements interface Bar public interface IBar { } public class Foo : IBar { } it seems natural to group these within the same file until another class implements the interface but to dedicate a file to 2 lines code seems excessive but correct. What is appropriate? 回答1: I would split them into 2 files. I often

Would it be bad form to put braces on the same line as the statement for single line “if” statements?

≡放荡痞女 提交于 2019-12-10 03:00:54
问题 So I know it's considered somewhat good practice to always include curly braces for if, for, etc even though they're optional if there is only one following statement, for the reason that it's easier to accidentally do something like: if(something == true) DoSomething(); DoSomethingElse(); when quickly editing code if you don't put the braces. What about something like this though: if(something == true) { DoSomething(); } That way you still take up fewer lines (which IMO increases readability

which style is preferred?

徘徊边缘 提交于 2019-12-10 02:38:11
问题 Option 1: def f1(c): d = { "USA": "N.Y.", "China": "Shanghai" } if c in d: return d[c] return "N/A" Option 2: def f2(c): d = { "USA": "N.Y.", "China": "Shanghai" } try: return d[c] except: return "N/A" So that I can then call: for c in ("China", "Japan"): for f in (f1, f2): print "%s => %s" % (c, f(c)) The options are to either determine whether the key is in directory before hand (f1), or just fallback to the exception (f2). Which one is preferred? Why? 回答1: Typically, exceptions carry some

Checkstyle rules for Google Java Style

痴心易碎 提交于 2019-12-10 02:19:12
问题 Is there a Checkstyle rule file with the Google Java Style? 回答1: The checkstyle team added it several days ago. Here it is : https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml 回答2: If you have a maven-project, you can easily integrate google_checks (you have to use at least maven-checkstyle-plugin version 2.17) <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>2.17</version> <configuration>

In OOP, what is the best practice in regards to using “this” inside a class?

♀尐吖头ヾ 提交于 2019-12-10 02:14:17
问题 Something I've always wondered; in a class where you can reference a member by either using 'this.[NAME]' or simply [NAME], which is preferred? For example in Java: public class foo { public int bars = 0; private void incrementBars(){ bars++; } } and public class foo { public int bars = 0; private void incrementBars(){ this.bars++; } } 'seem' to have the same effect. In cases where I instantiate multiple instances of class foo, I'd, so far, do something like: for (foo f : listOfFoos){ f