control-flow

How to run code inside a loop only once without external flag?

若如初见. 提交于 2019-11-30 12:56:33
问题 I want to check a condition inside a loop and execute a block of code when it's first met. After that, the loop might repeat but the block should be ignored. Is there a pattern for that? Of course it's easy to declare a flag outside of the loop. But I I'm interested in an approach that completely lives inside the loop. This example is not what I want. Is there a way to get rid of the definition outside of the loop? bool flag = true; for (;;) { if (someCondition() && flag) { // code that runs

Is non-local return in Scala new?

前提是你 提交于 2019-11-30 11:24:42
问题 A colleague just showed me this and I was surprised that it compiled at all: def toUpper(s: Option[String]): String = { s.getOrElse(return "default").toUpperCase // ^^^^^^ // a return here in the closure?? } and this even works: println(toUpper(Some("text"))) // TEXT println(toUpper(None)) // default I thought return from inside a closure was not allowed. Since when has this worked? Are there caveats with such non-local returns? 回答1: The semantics are relatively simple: return will throw a

Control Flow via Return vs. If/Else [closed]

心已入冬 提交于 2019-11-30 11:17:00
Which one is better (implicit control flow via return or control flow via if ) -- see below. Please explain what you see as advantage/disadvantage to either one. I like option A because it's less code. Flow via Return: public ActionResult Edit(MyClass class) { if (!class.Editable) return null; class.Update(); return View(); } Flow via If/Else: public ActionResult Edit(MyClass class) { if (class.Editable) { class.Update(); return View(); } else { return null; } } There's not much difference in this specific example, but in general I like the first approach because it uses a guard clause to

How do I tell which guard statement failed?

南楼画角 提交于 2019-11-30 05:07:51
If I’ve got a bunch of chained guard let statements, how can I diagnose which condition failed, short of breaking apart my guard let into multiple statements? Given this example: guard let keypath = dictionary["field"] as? String, let rule = dictionary["rule"] as? String, let comparator = FormFieldDisplayRuleComparator(rawValue: rule), let value = dictionary["value"] else { return nil } How can I tell which of the 4 let statements was the one that failed and invoked the else block? The simplest thing I can think of is to break out the statements into 4 sequential guard else statements, but

How to run code inside a loop only once without external flag?

久未见 提交于 2019-11-30 04:09:40
I want to check a condition inside a loop and execute a block of code when it's first met. After that, the loop might repeat but the block should be ignored. Is there a pattern for that? Of course it's easy to declare a flag outside of the loop. But I I'm interested in an approach that completely lives inside the loop. This example is not what I want. Is there a way to get rid of the definition outside of the loop? bool flag = true; for (;;) { if (someCondition() && flag) { // code that runs only once flag = false; } // code that runs every time } It's fairly hacky, but as you said it's the

Programming style: should you return early if a guard condition is not satisfied?

亡梦爱人 提交于 2019-11-30 02:53:22
One thing I've sometimes wondered is which is the better style out of the two shown below (if any)? Is it better to return immediately if a guard condition hasn't been satisfied, or should you only do the other stuff if the guard condition is satisfied? For the sake of argument, please assume that the guard condition is a simple test that returns a boolean, such as checking to see if an element is in a collection, rather than something that might affect the control flow by throwing an exception. Also assume that methods/functions are short enough not to require editor scrolling. // Style 1

Are there any static Call-Graph and/or Control-Flow-Graph API for JavaScript? [closed]

喜欢而已 提交于 2019-11-29 19:59:47
Are there any Call-Graph and/or Control-Flow-Graph generators for JavaScript? Call Graph - http://en.wikipedia.org/wiki/Call_graph Control Flow Graph - http://en.wikipedia.org/wiki/Control_flow_graph EDIT: I am looking specifically for a static tool that let me access the graph using some API/code To do this, you need: parsing, name resolution (handling scoping) type analysis (while JavaScript is arguably "dynamically typed", there are all kinds of typed constants including function constants that are of specific interest here) control flow analysis (to build up the structure of the control

Control Flow via Return vs. If/Else [closed]

妖精的绣舞 提交于 2019-11-29 17:21:52
问题 Which one is better (implicit control flow via return or control flow via if ) -- see below. Please explain what you see as advantage/disadvantage to either one. I like option A because it's less code. Flow via Return: public ActionResult Edit(MyClass class) { if (!class.Editable) return null; class.Update(); return View(); } Flow via If/Else: public ActionResult Edit(MyClass class) { if (class.Editable) { class.Update(); return View(); } else { return null; } } 回答1: There's not much

How Can I Avoid Using Exceptions for Flow Control?

主宰稳场 提交于 2019-11-29 13:46:50
I have been assigned a project to develop a set of classes that act as an interface to a storage system. A requirement is that the class support a get method with the following signature: public CustomObject get(String key, Date ifModifiedSince) Basically the method is supposed to return the CustomObject associated with the key if and only if the object has been modified after ifModifiedSince . If the storage system does not contain the key then the method should return null. My problem is this: How do I handle the scenario where the key exists but the object has not been modified? This is

Programming style: should you return early if a guard condition is not satisfied?

坚强是说给别人听的谎言 提交于 2019-11-28 23:51:57
问题 One thing I've sometimes wondered is which is the better style out of the two shown below (if any)? Is it better to return immediately if a guard condition hasn't been satisfied, or should you only do the other stuff if the guard condition is satisfied? For the sake of argument, please assume that the guard condition is a simple test that returns a boolean, such as checking to see if an element is in a collection, rather than something that might affect the control flow by throwing an