coding-style

Is there an alternative to hyper-indented code?

*爱你&永不变心* 提交于 2019-12-18 04:12:56
问题 I often run into code that has to perform lots of checks and ends up being indented at least five or six levels before really doing anything. I am wondering what alternatives exist. Below I've posted an example of what I'm talking about (which isn't actual production code, just something I came up with off the top of my head). public String myFunc(SomeClass input) { Object output = null; if(input != null) { SomeClass2 obj2 = input.getSomeClass2(); if(obj2 != null) { SomeClass3 obj3 = obj2

Is there an alternative to hyper-indented code?

半城伤御伤魂 提交于 2019-12-18 04:12:07
问题 I often run into code that has to perform lots of checks and ends up being indented at least five or six levels before really doing anything. I am wondering what alternatives exist. Below I've posted an example of what I'm talking about (which isn't actual production code, just something I came up with off the top of my head). public String myFunc(SomeClass input) { Object output = null; if(input != null) { SomeClass2 obj2 = input.getSomeClass2(); if(obj2 != null) { SomeClass3 obj3 = obj2

Why might one also use a blank constructor?

有些话、适合烂在心里 提交于 2019-12-18 03:39:28
问题 I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constructor. For example: public class Genotype { private boolean bits[]; private int rating; private int length; private Random random; public Genotype() { // <= THIS is the bandit, this one right here random = new Random(); } /* creates a Random genetoype */ public Genotype(int length, Random r) { random = r; this.length =

What's the most efficient way to determine whether an untrimmed string is empty in C#?

人走茶凉 提交于 2019-12-18 03:16:08
问题 I have a string that may have whitespace characters around it and I want to check to see whether it is essentially empty. There are quite a few ways to do this: 1 if (myString.Trim().Length == 0) 2 if (myString.Trim() == "") 3 if (myString.Trim().Equals("")) 4 if (myString.Trim() == String.Empty) 5 if (myString.Trim().Equals(String.Empty)) I'm aware that this would usually be a clear case of premature optimization, but I'm curious and there's a chance that this will be done enough to have a

Is GOTO considered harmless when jumping to cleanup at the end of function?

南笙酒味 提交于 2019-12-18 03:07:19
问题 The goto statement has been examined at great length in several SO discussions (see this and that), and I certainly don't want to revive those heated debates. Instead, I'd like to concentrate on a single use case of goto s and discuss its value and possible alternatives. Consider the following code snippet, which is common in (at least my own) FSMs: while (state = next_state()) { switch (state) { case foo: /* handle foo, and finally: */ if (error) goto cleanup; break; case bar: /* handle bar,

Is GOTO considered harmless when jumping to cleanup at the end of function?

橙三吉。 提交于 2019-12-18 03:07:11
问题 The goto statement has been examined at great length in several SO discussions (see this and that), and I certainly don't want to revive those heated debates. Instead, I'd like to concentrate on a single use case of goto s and discuss its value and possible alternatives. Consider the following code snippet, which is common in (at least my own) FSMs: while (state = next_state()) { switch (state) { case foo: /* handle foo, and finally: */ if (error) goto cleanup; break; case bar: /* handle bar,

Naming Conventions: What to name a method that returns a boolean? [closed]

不羁岁月 提交于 2019-12-18 03:01: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 11 months ago . I have an interface in C# that helps retrieving of data from a custom archive on server. The interface looks like this: public interface IRetrieveData { bool OkToRetrieve(SomeData data); // Method in question... bool RetrieveToLocal(SomeData data); } This interface is

Add stock option in woocommerce

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 02:49:09
问题 I would like to add a new option to the dropdown list of stocks options for a product. By default, there is "Out of stock", "In stock" and I would like to add a third option. I found the method that displays the dropdown ( in class-wc-meta-box-product-data.php ) // Stock status woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array( 'instock' => __( 'In stock', 'woocommerce' ),

Policy with catching std::bad_alloc

◇◆丶佛笑我妖孽 提交于 2019-12-18 02:40:15
问题 So I use Qt a lot with my development and love it. The usual design pattern with Qt objects is to allocate them using new . Pretty much all of the examples (especially code generated by the Qt designer) do absolutely no checking for the std::bad_alloc exception. Since the objects allocated (usually widgets and such) are small this is hardly ever a problem. After all, if you fail to allocate something like 20 bytes, odds are there's not much you can do to remedy the problem. Currently, I've

Preferred standard use: range based for or std::for_each

 ̄綄美尐妖づ 提交于 2019-12-17 23:48:11
问题 In C++11, there are two loops over all elements (range based for and for_each). Is there any reason to prefer one over the other or are there situations where one is a better fit? for (auto& elem: container) { // do something with elem } std::for_each(container.begin(), container.end(), [](Elem& elem) { // do something with elem }); My idea would be that the first is simpler and is similar to range based loops in other languages while the second also works for sequences that are not complete