idioms

Kotlin and idiomatic way to write, 'if not null, else…' based around mutable value

时光毁灭记忆、已成空白 提交于 2019-12-09 04:31:54
问题 Suppose we have this code: class QuickExample { fun function(argument: SomeOtherClass) { if (argument.mutableProperty != null ) { doSomething(argument.mutableProperty) } else { doOtherThing() } } fun doSomething(argument: Object) {} fun doOtherThing() {} } class SomeOtherClass { var mutableProperty: Object? = null } Unlike in Java, where you could be left alone to worry about null dereferencing at runtime, this doesn't compile - quite rightly. Of course, mutableProperty may no longer be null

Is the Non-Virtual Interface (NVI) idiom as useful in C# as in C++?

怎甘沉沦 提交于 2019-12-07 03:05:35
问题 In C++, I often needed NVI to get consistency in my APIs. I don't see it used as much among others in C#, though. I wonder if that is because C#, as a language, offers features that makes NVI unnecessary? (I still use NVI in C#, though, where needed.) 回答1: I think the explanation is simply that in C#, "traditional" Java-style OOP is much more ingrained, and NVI runs counter to that. C# has a real interface type, whereas NVI relies on the "interface" actually being a base class. That's how it

Sink arguments and move semantics for functions that can fail (strong exception safety)

时间秒杀一切 提交于 2019-12-06 19:48:02
问题 I have a function that operates on a big chunk of data passed in as a sink argument. My BigData type is already C++11-aware and comes with fully functional move constructor and move assignment implementations, so I can get away without having to copy the damn thing: Result processBigData(BigData); [...] BigData b = retrieveData(); Result r = processBigData(std::move(b)); This all works perfectly fine. However, my processing function may fail occasionally at runtime resulting in an exception.

Stylistic question concerning returning void

爷,独闯天下 提交于 2019-12-06 03:05:18
问题 Consider the following contrived example: void HandleThat() { ... } void HandleThis() { if (That) return HandleThat(); ... } This code works just fine, and I'm fairly sure it's spec-valid, but I (perhaps on my own) consider this unusual style, since the call appears to return the result of the function, despite the fact that both functions are prototyped to be void. Typically, I would expect to see: if (That) {HandleThat(); return;} which, I feel, leaves no ambiguity as to what's going on. SO

Python - Idiom to check if string is empty, print default

旧时模样 提交于 2019-12-06 01:54:17
问题 I'm just wondering, is there a Python idiom to check if a string is empty, and then print a default if it's is? (The context is Django, for the __unicode__(self) function for UserProfile - basically, I want to print the first name and last name, if it exists, and then the username if they don't both exist). Cheers, Victor 回答1: displayname = firstname + lastname or username will work if firstname and last name has 0 length blank string 回答2: displayname = firstname+' '+lastname if firstname and

How can I implement a generator in C++?

泪湿孤枕 提交于 2019-12-05 19:08:49
问题 I want to know how to implement a generator , like Python, in C++? Python can use keyword "yield" to do so. But how to do it in C++? 回答1: In C++ we have 'iterators'. One explicitly asks for an interator, explicitly increments it and dereferences it. If you want them to be used with the standard library functions, they should mostly be derived from std::forward_iterator , and implement a number of it's functions. An other way to mimic kindof a generator on a collection is allowing a function

Is there a better alternative to this Ruby idiom?

做~自己de王妃 提交于 2019-12-05 18:14:05
I'm finding myself writing this bit of code in my controllers a lot: params[:task][:completed_at] = Time.parse(params[:task][:completed_at]) if params[:task][:completed_at] Don't get hung up on what I'm doing here specifically, because the reasons change every time; but there are many circumstances where I need to check for a value in params and change it before handing it off to create or update_attributes . Repeating params[:task][:completed_at] three times feels very bad. Is there a better way to do this? One way to shorten this slightly is: if c = params[:task][:completed_at] params[:task]

x or y: acceptable idiom, or obfuscation?

纵饮孤独 提交于 2019-12-05 13:13:41
I have to extract values from a variable that may be None, with some defaults in mind. I first wrote this code: if self.maxTiles is None: maxX, maxY = 2, 2 else: maxX, maxY = self.maxTiles Then I realized I could shorten it to: maxX, maxY = self.maxTiles if self.maxTiles is not None else (2, 2) But then I realized this might be the most succinct and easily readable: maxX, maxY = self.maxTiles or (2, 2) Is the latter acceptable, or too hackish? About, specifically, self.maxTiles if self.maxTiles is not None else (2, 2) I've found that "double negatives" of the general form if not A: B else: C

JavaScript idiom: create a function only to invoke it

[亡魂溺海] 提交于 2019-12-05 11:23:58
问题 I am learning YUI and have occasionally seen this idiom: <script> (function x(){ do abcxyz})(); </script> Why do they create a function just to invoke it? Why not just write: <script> do abcxyz </script> For example see here. 回答1: They're taking advantage of closures. A short explanation: Since JS uses function-level scoping, you can do a bunch of actions within a function and have it remain in that scope. This is useful for invoking code that doesn't mess with the global namespace. It also

What is the “?” and “:” sequence actually called? [duplicate]

ε祈祈猫儿з 提交于 2019-12-05 08:28:12
This question already has an answer here: What does '?' do in C++? 7 answers This may be a bonehead question, but I cannot figure out what the ? exp : other_exp sequence is called. Example: int result = (true) ? 1 : 0; I've tried using the Google machine, but it's hard to Googilize for something without knowing what it's called. Thanks! It is called the the conditional operator or alternativly the ternary operator as it a ternary operator (an operator which takes 3 operands (arguments)), and as it's usually the only operator, that does this. It is also know as the inline if (iif), the ternary