coding-style

next versus if in a .each loop?

混江龙づ霸主 提交于 2019-12-05 12:02:03
问题 I have a text processing thing I'm doing in Ruby. Basically, I have to implement a simple state machine (with one character look-behind). My code at the moment looks like this: text.each{ |c| ... ... ... ... if @state!=:some_state next end #processing stuff for if in :some_state mode ... ... ... ... ... } Is this proper? Or should it rather be implemented like: text.each{ |c| ... ... ... ... if @state==:some_state #processing stuff for if in :some_state mode ... ... ... ... ... end } Is there

What is a good rule for when to prepend members with 'this' (C#)?

≡放荡痞女 提交于 2019-12-05 11:45:55
问题 If I am accessing a member field, property, or method, I'm never sure when I should prepend it with 'this'. I am not asking about cases where it is required, like in the case where a local variable has the same name. I am talking about cases where the meaning is exactly the same. Which is more readable? Are there any standards, best practices, or rules of thumb I should be following? Should it just be consistent throughout a class, or an entire code base? 回答1: I recommend using Microsoft's

Is there a convention for memoization in a method call?

你说的曾经没有我的故事 提交于 2019-12-05 11:39:41
问题 I want to avoid reevaluation of a value in method call. Untill now, I was doing this: def some_method @some_method ||= begin # lot's of code end end But it ends up quite ugly. In some code, I saw something like the following: def some_method @some_method ||= some_method! end private def some_method! # lot's of code end I don't like the bang ( ! ) at the end, so I came up with this: def some_method @some_method ||= _some_method end private def _some_method # lot's of code end Is prepending

Why should I use <ARGV> or <> instead of <STDIN> in Perl?

时光毁灭记忆、已成空白 提交于 2019-12-05 10:32:04
Quoting from Perl::Critic::Policy::InputOutput::ProhibitExplicitStdin Perl has a useful magic filehandle called *ARGV that checks the command line and if there are any arguments, opens and reads those as files. If there are no arguments, *ARGV behaves like *STDIN instead. This behavior is almost always what you want if you want to create a program that reads from STDIN. This is often written in one of the following two equivalent forms: while (<ARGV>) { # ... do something with each input line ... } # or, equivalently: while (<>) { # ... do something with each input line ... } Is it "just a

Do you think a software company should impose developers a coding-style? [closed]

牧云@^-^@ 提交于 2019-12-05 10:31: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 8 years ago . If you think it shouldn't, explain why. If yes, how deep should the guidelines be in your opinion? For example, indentation of code

PSR-1 2.3 Side Effects Rule

梦想与她 提交于 2019-12-05 09:53:41
问题 I have a question Regarding PHP Basic Coding Standards PSR1. PSR 1 Rule 2.3 states: Rule 2.3 Side Effects A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both. The phrase "side effects" means execution of logic not directly related to declaring classes, functions, constants, etc., merely from including the file . "Side effects" include but are not limited to: generating

C++ Style Convention: Parameter Names within Class Declaration

非 Y 不嫁゛ 提交于 2019-12-05 09:43:32
I'm a fairly new C++ programmer and I would like to hear the arguments for and against naming parameters within the class declaration. Here's an example: Student.h #ifndef STUDENT_H_ #define STUDENT_H_ #include <string> using namespace std; class Student { private: string name; unsigned int age; float height, GPA; public: Student(string, unsigned int, float, float); void setAge(unsigned int); }; #endif /*STUDENT_H_*/ vs. #ifndef STUDENT_H_ #define STUDENT_H_ #include <string> class Student { private: string name; unsigned int age; float height, GPA; public: Student(string name, unsigned int

Show 80-character margin line in TextMate

半腔热情 提交于 2019-12-05 08:59:52
问题 Is it possible to display a right-margin line at 80 characters in TextMate? (i.e. the right-margin line in eclipse, gedit, etc.) If so, how can I configure that? 回答1: Figured it out: In the View menu, uncheck 'Soft Wrap'. Then, go to View -> Wrap Column -> Other... and select the column at 80 chars in your editor window. This will show the line at 80 characters, but won't force wrapping at 80 chars. (Turning 'Soft Wrap' back on will force wrapping.) 回答2: What about Visual Ornaments/Show right

Whats the reasoning behind the different brace forms? [closed]

大城市里の小女人 提交于 2019-12-05 08:39:36
I'm reading through the Zend Framework coding standards , where they state that curly brace after a Class definitions should be on the next line, the "one true brace form". class MyClass { function.... } I usually have the braces on the same line: class OtherClass { function ... } What's the reason for putting the brace on the next line? Or using any other style, for that matter? Personal preference is really the only real "reason". Having the braces on lines by themselves helps to visually separate the inner part from the outer part. That makes it easier to quickly scan through source code

Assignment with “or” in python

♀尐吖头ヾ 提交于 2019-12-05 08:39:26
问题 Is it considered bad style to assign values to variables like this? x = "foobar" or None y = some_variable or None In the above example, x gets the value 'foobar'. 回答1: No, it's a common practice. It's only considered bad style for expressions that are considerably longer than yours. 回答2: The primary danger of doing something like this is the possibility that (in the second case) some_variable is False but not None (the integer 0 , for instance) and you don't want to end up with y equal to