coding-style

IF statement formatting best-practise, what's your style?

别等时光非礼了梦想. 提交于 2020-01-15 07:01:08
问题 Looking to improve my IF statement, and I want to keep my code looking pretty This is what I am currently doing, is it readable , any room for improvement ? SomeObject o = LoadSomeObject(); if( null == o || null == o.ID || null == o.Title || 0 == o.ID.Length || 0 == o.Title.Length ) I don't have anyone to ask around. That's why I came here in first place. Please don't close my question :( 回答1: Your verbosity is leading to a less readable code, I think the following format is best: if ( null =

IF statement formatting best-practise, what's your style?

跟風遠走 提交于 2020-01-15 07:01:05
问题 Looking to improve my IF statement, and I want to keep my code looking pretty This is what I am currently doing, is it readable , any room for improvement ? SomeObject o = LoadSomeObject(); if( null == o || null == o.ID || null == o.Title || 0 == o.ID.Length || 0 == o.Title.Length ) I don't have anyone to ask around. That's why I came here in first place. Please don't close my question :( 回答1: Your verbosity is leading to a less readable code, I think the following format is best: if ( null =

“dangling” local blocks in scala

烂漫一生 提交于 2020-01-15 05:18:07
问题 In scala it is possible to define a local block in a function. The local block evaluates to the last statements, for example, val x = {val x =1;x+1} Here x==2 , the inner val x is local to that block. However those local blocks can cause sneaky bugs when writing anonymous classes. For example (from scala's reference) new Iterator[Int] {...} // new anonymous class inheriting from Iterator[Int] new Iterator[Int] {...} //new Iterator[Int] followed by a "dangling" local block Differntiating

C++: Using '.' operator on expressions and function calls

こ雲淡風輕ζ 提交于 2020-01-15 05:00:27
问题 I was wondering if it is good practice to use the member operator . like this: someVector = (segment.getFirst() - segment.getSecond()).normalize().normalCCW(); Just made that to show the two different things I was wondering, namely if using (expressions).member/function() and foo.getBar().getmoreBar() were in keeping with the spirit of readability and maintainability. In all the c++ code and books I learned from, I've never seen it used in this way, yet its intoxicatingly easy to use it as

Is it okay to use an overloaded operator to implement another operator overload?

夙愿已清 提交于 2020-01-14 17:51:50
问题 For instance if I have overloaded a + operator myClass & operator + (const myClass & rhs) and also overloaded = operator myClass & operator = (const myClass & rhs) both operators are working fine. Can I use this overloaded operator in my += operator overload? myClass & operator += (const myClass & rhs){ *this = *this + progA; return *this; } The above code is working okay. I just want to know if this is good code writing practice or I should re-use the code from the two previous

Is it okay to use an overloaded operator to implement another operator overload?

旧城冷巷雨未停 提交于 2020-01-14 17:51:26
问题 For instance if I have overloaded a + operator myClass & operator + (const myClass & rhs) and also overloaded = operator myClass & operator = (const myClass & rhs) both operators are working fine. Can I use this overloaded operator in my += operator overload? myClass & operator += (const myClass & rhs){ *this = *this + progA; return *this; } The above code is working okay. I just want to know if this is good code writing practice or I should re-use the code from the two previous

Resharper: how to force introducing new private fields at the bottom of the class?

扶醉桌前 提交于 2020-01-14 13:49:07
问题 Resharper offers a very useful introduce and initialize field xxx action when you specify a new parameter in a constructor like: Constructor (int parameter) The only (minor) nuisance is that it puts the new field at the beginning of the class - and I'm a fan of putting private parts as far away as possible from the prying eyes of strangers ;). If, however, you already have some private fields in the class, Resharper will put the new field "correctly" (note the quotes, I don't want to start a

Python default values and setting the value based on other variables. To if else or to not.

人盡茶涼 提交于 2020-01-14 10:26:11
问题 At times I have a variable that I want to default to something and if something else is set change it to something else. The question is. What is preferred? Setting the default value and then changing it if the condition is met or setting the condition only once depending on the initial check with an added else? Example in code. if x: y = '%s-other' % x else: y = 'some_default' The other option would be. y = 'some_default' if x: y = '%s-other' % x This isn't always an argument passed in to a

Multiple iterations

风格不统一 提交于 2020-01-14 08:03:06
问题 Is there a easier, cleaner way to write code like this: (1..10).each do |i| (1..10).each do |j| (1..10).each do |k| (1..10).each do |l| puts "#{i} #{j} #{k} #{l}" end end end end Ideally I'd be able to do something like... (1..10).magic(4) { |i, j, k, l| puts "#{i} #{j} #{k} #{l}" } Or even better... magic(10, 4) { |i, j, k, l| puts "#{i} #{j} #{k} #{l}" } If there's not something built in, how would I write a method like the last one? 回答1: If you're on Ruby 1.9, you can do this: range = (1.

Does anyone change the Visual Studio default bracing style? - Is there a standard?

蓝咒 提交于 2020-01-14 06:56:33
问题 I find the default bracing style a bit wasteful on line count eg... function foo() { if (...) { ... } else { ... } } would, if I was writing in JavaScript for example be written like... function foo() { if (...) { ... } else { ... } } ...which I understand may also not be to peoples' tastes. But the question(s) is/are do you turn off the VS formatting style and use your own rules? What is the opinion of this in the industry when many people are working on the same code-base? Is it better just