What is idiomatic code?
I'd be interested in some before-and-after c# examples, some non-idiomatic vs idiomatic examples. Non-c# examples would be fine as well if they get the idea across. Thanks. ...
I'd be interested in some before-and-after c# examples, some non-idiomatic vs idiomatic examples. Non-c# examples would be fine as well if they get the idea across. Thanks. ...
I'm looking for a simple, idiomatic method of replicating Excel's visual cue that a number is too large to be displayed in a column. I've got the following xaml: <ListView.View> <GridView> <GridViewColumn ... /> </GridView> <ListView.View> and what I'd like is if the text in the column is too small to be displayed (i.e...
Alright I have an xml document that looks something like this: <xml> <list> <partner> <name>Some Name</name> <status>active</status> <id>0</id> </partner> <partner> <name>Another Name</name> <status>active</status> <id>1</id> </partner> </list> </xml> I ...
Still trying to figure out idiomatic jQuery - here's some jQuery for setting a variable base on the class of a div - is there a jQuery way of doing this more compactly, given that the div in question may have more than one class ... var currentType = 'component'; // text_component, image_component, tagged_component if ($(my_div).hasClas...
I am beginning to delve deeper into Perl, but am having trouble writing "Perl-ly" code instead of writing C in Perl. How can I change the following code to use more Perl idioms, and how should I go about learning the idioms? Just an explanation of what it is doing: This routine is part of a module that aligns DNA or amino acid sequences...
IllegalStateException is often used in Java when a method is invoked on an object in inappropriate state. What would you use instead in Python? ...
It would be helpful to pick up Ruby for my new gig so this morning I wrote the following. It takes a PGN file of chess games I've played and indexes them by first move. I'd appreciate any suggestions as to how to make it more "idiomatic". Since it doesn't take command line arguments (such as for the filename) and isn't object orient...
For example: <?php function get_current_user_id(){ static $id; if(!$id){ $id = 5; echo "Id set."; } return $id; } $id = get_current_user_id(); $id2 = get_current_user_id(); $id3 = get_current_user_id(); echo "IDs: ".$id." ".$id2." ".$id3; ?> //Output: Id set.ID...
Here is some jQuery for a search box that I expect is actually an antipattern, and am sure there is a much better solution for that I would love to be pointed towards: I will describe it in comments then just give the code, since the comments may be more clear and simple than the code: // set up a function call on keypress. // functio...
I hear people talking about dependency injection and the benefit of it all the time, but I don't really understand it. I'm wondering if it's a solution to the "I pass database connections as arguments all the time" problem. I tried reading wikipedia's entry on it, but the example is written in Java so I don't solidly understand the dif...
What is the rationale behind the advocated use of the for i in xrange(...)-style looping constructs in Python? For simple integer looping, the difference in overheads is substantial. I conducted a simple test using two pieces of code: File idiomatic.py: #!/usr/bin/env python M = 10000 N = 10000 if __name__ == "__main__": x, y = 0...
What is the "best" way to use "isa()" reliably? In other words, so it works correctly on any value, not just an object. By "best", I mean lack of un-handled corner cases as well as lack of potential performance issues, so this is not a subjective question. This question mentions two approaches that seem reliable (please note that the ...
UPDATE 2: For posterity, this is how I've settled on doing it (thanks to Jorg's input): 100.step(2, -2) do |x| # my code end (Obviously there are plenty of ways to do this; but it sounds like this is the most "Ruby" way to do it; and that's exactly what I was after.) UPDATE: OK, so what I was looking for was step: (2..100).ste...
I'm just starting to learn F#. I wrote this F#/ADO.NET code last night. In what ways would you improve the syntax - make it feel like idiomatic F#? let cn = new OleDbConnection(cnstr) let sql = "SELECT * FROM People" let da = new OleDbDataAdapter(new OleDbCommand(sql, cn)) let ds = new DataSet() cn.Open() let i =...
I've got an Erlang snippet here that I'd like to work into more idiomatic Erlang, rather than a crude Python translation. Process takes a pairs of congruent lists and combines them. Some of the elements need to be taken from one lists or the other, based on their properties, while the rest of the elements need to be summed. It works pro...
Imagine this: def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta, theta, iota, kappa): pass The line overpass the 79 characters, so, what's the pythonic way to multiline it? ...
C++ being a value oriented language doesn't seem to support OO (and thus sub-typing polymorphism) very well. As for parametric polymorphism, lack of type inference on type parameters and verbose syntax of templates makes them challenging to use. Please note that the only languages I know moderately well are Java (sub-typing polymorphism...
In Common Lisp you use the (null x) function to check for empty lists and nil values. Most logically this maps to (or (nil? x) (= '() x)) In clojure. Can someone suggest a more idiomatic way to do it in Clojure? ...
I'm passing the name of a function for use in another method. (defn mapper [m function] (cond (= '() m) '() true (cons (function (first m)) (mapper (rest m) function)))) (println (map_ '((blue red)(green red)(white red)) #'first)) Is there a more idiomatic way to do this in clojure? ...
Here is an example I wrote that uses if-else branches and guard expressions. When is one more appropriate over the other? The main reason I want to know this is because languages typically have a idiomatic way of doing things. test1 a b = if mod b 3 ≡ 0 then a + b else if mod b 5 ≡ 0 then a + b else a test2 a b | mod b...