coding-style

When should I add comments to my code? [closed]

耗尽温柔 提交于 2019-12-20 08:49:30
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . When I'm writing it? After I got a part done (Single class/function/if-elses)? After I got the whole thing working? 回答1: The short

Why write `sizeof(char)` if char is 1 by standard?

时光怂恿深爱的人放手 提交于 2019-12-20 08:46:42
问题 I was doing some C coding and after reading some C code I've noticed that there are code snippets like char *foo = (char *)malloc(sizeof(char) * someDynamicAmount); So I want to ask what's more C-ish way to allocate memory for char array? Use sizeof(char) and supposedly future-proof the code against any standard changes or omit it and use the number directly? 回答1: The more Cish way would be char* foo = malloc(someDynamicAmount * sizeof *foo); referencing the variable and not the type so that

How to name factory like methods?

风流意气都作罢 提交于 2019-12-20 08:22:02
问题 I guess that most factory-like methods start with create . But why are they called "create"? Why not "make", "produce", "build", "generate" or something else? Is it only a matter of taste? A convention? Or is there a special meaning in "create"? createURI(...) makeURI(...) produceURI(...) buildURI(...) generateURI(...) Which one would you choose in general and why? 回答1: Some random thoughts: 'Create' fits the feature better than most other words. The next best word I can think of off the top

Passing hashes instead of method parameters [closed]

梦想与她 提交于 2019-12-20 08:09:57
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . I see that in Ruby (and dynamically typed languages, in general) a very common practice is to pass a hash, instead of declaring concrete method parameters. For example, instead of declaring a method with parameters and calling it like this: def my_method(width, height, show

Is there a way to avoid spaghetti code over the years? [closed]

血红的双手。 提交于 2019-12-20 08:09:51
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I've had several programming jobs. Each one with 20-50 developers, project going on for 3-5 years. Every time it's the same. Some programmers are bright, some are average. Everyone has their CS degree, everyone read design patterns. Intentions are good, people are trying hard

Did anyone create the Java Code Formatter Profile for Eclipse IDE that conforms to the Android Code Style Rules?

不想你离开。 提交于 2019-12-20 08:07:57
问题 Android Code Style Guide defines "Android Code Style Rules". To conform to these rules one have to change quite a number of settings of the Java Code Formatter (Window->Preferences->Java->Code Style->Formatter) default profile (in Eclipse IDE). Did anyone manage to configure the formatter to follow the "Android Code Style Rules" already? If yes, please export the Formatter profile and publish to be used by community. PS: I've tried to do this myself but I've found that there are too many

C++ iterators & loop optimization

ぃ、小莉子 提交于 2019-12-20 08:06:39
问题 I see a lot of c++ code that looks like this: for( const_iterator it = list.begin(), const_iterator ite = list.end(); it != ite; ++it) As opposed to the more concise version: for( const_iterator it = list.begin(); it != list.end(); ++it) Will there be any difference in speed between these two conventions? Naively the first will be slightly faster since list.end() is only called once. But since the iterator is const, it seems like the compiler will pull this test out of the loop, generating

Understanding Dijkstra's Mozart programming style

妖精的绣舞 提交于 2019-12-20 08:03:37
问题 I came across this article about programming styles, seen by Edsger Dijsktra. To quickly paraphrase, the main difference is Mozart, when the analogy is made to programming, fully understood (debatable) the problem before writing anything, while Beethoven made his decisions as he wrote the notes out on paper, creating many revisions along the way. With Mozart programming, version 1.0 would be the only version for software that should aim to work with no errors and maximum efficiency. Also,

Is it bad style to use return at the beginning of a function to avoid doing unnecessary work? [closed]

狂风中的少年 提交于 2019-12-20 05:52:14
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Say we have a function foo(), and a bool bar. The work foo does is of no use if bar is false. What is the most proper way to write foo

Correct C pointer notation [closed]

主宰稳场 提交于 2019-12-20 05:47:14
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago . Which one is the best way to write: string* str Or: string *str Is there any drawback of side effect to one of them ? Thanks 回答1: A