Search Results

Search found 106 results on 5 pages for 'idioms'.

Page 2/5 | < Previous Page | 1 2 3 4 5  | Next Page >

  • Standard Place for an Empty String Array in the JDK

    - by Simon B
    Hi is there a standard place for accessing empty array constants in the JDK 1.5. When I want to do a conversion from a String Collection (e.g. ArrayList)to a String Array I find myself using my own which is defined in my own Constants class: public static final String[] EMPTY_STRING_ARRAY = new String[0]; And then in my client code something like: String[] retVal = myStringList.toArray(Constants.EMPTY_STRING_ARRAY); return retVal; I was wondering if this is the "idiomatic" of doing it or if I'm missing something I get the impression from the brief search I did that this kind of thing is prevalent in many people's code. Any ideas, answers, comment (aside from that I shouldn't really use String Arrays) greatly appreciated, Cheers Simon

    Read the article

  • Using interface classes and non-virtual interface idiom in C++

    - by andreas buykx
    Hi all, In C++ an interface can be implemented by a class with all its methods pure virtual: class IFoo { public: virtual void method() = 0; }; Now I want to implement this interface by a hierarchy of classes: class FooBase : public IFoo // implement interface IFoo { public: void method(); // calls methodImpl; private: virtual void methodImpl(); }; For the class hierarchy I would like to use the non-virtual interface (NVI) idiom, to deny derived classes the possibility of overriding the common behavior implemented in FooBase::method(), but it seems that all derived classes have the opportunity to override the FooBase::method() because it is declared in the interface class. Is my observation correct? And if so are there other options to both use interface classes and the NVI idiom?

    Read the article

  • Idiomatic Python: 'times' loop

    - by perimosocordiae
    Say I have a function foo that I want to call n times. In Ruby, I would write: n.times { foo } In Python, I could write: for _ in xrange(n): foo() But that seems like a hacky way of doing things. My question: Is there an idiomatic way of doing this in Python?

    Read the article

  • DRY Ruby Initialization with Hash Argument

    - by ktex
    I find myself using hash arguments to constructors quite a bit, especially when writing DSLs for configuration or other bits of API that the end user will be exposed to. What I end up doing is something like the following: class Example PROPERTIES = [:name, :age] PROPERTIES.each { |p| attr_reader p } def initialize(args) PROPERTIES.each do |p| self.instance_variable_set "@#{p}", args[p] if not args[p].nil? end end end Is there no more idiomatic way to achieve this? The throw-away constant and the symbol to string conversion seem particularly egregious.

    Read the article

  • Ruby switch like idiom

    - by Eef
    Hey, I have recently started a project in Ruby on Rails. I used to do all my projects before in Python but decided to give Ruby a shot. In the projects I wrote in Python I used a nice little technique explained by the correct answer in this post: http://stackoverflow.com/questions/277965/dictionary-or-if-statements-jython I use this technique due to Python not having a native switch function and it also get rid of big if else blocks I have been trying to do recreate the above method in Ruby but can't seem to quite get it. Could anyone help me out? Thanks Eef

    Read the article

  • How to implement an interface class using the non-virtual interface idiom in C++?

    - by andreas buykx
    Hi all, In C++ an interface can be implemented by a class with all its methods pure virtual. Such a class could be part of a library to describe what methods an object should implement to be able to work with other classes in the library: class Lib::IFoo { public: virtual void method() = 0; }; : class Lib::Bar { public: void stuff( Lib::IFoo & ); }; Now I want to to use class Lib::Bar, so I have to implement the IFoo interface. For my purposes I need a whole of related classes so I would like to work with a base class that guarantees common behavior using the NVI idiom: class FooBase : public IFoo // implement interface IFoo { public: void method(); // calls methodImpl; private: virtual void methodImpl(); }; The non-virtual interface (NVI) idiom ought to deny derived classes the possibility of overriding the common behavior implemented in FooBase::method(), but since IFoo made it virtual it seems that all derived classes have the opportunity to override the FooBase::method(). If I want to use the NVI idiom, what are my options other than the pImpl idiom already suggested (thanks space-c0wb0y).

    Read the article

  • Is str.replace(..).replace(..) ad nauseam a standard idiom in Python?

    - by meeselet
    For instance, say I wanted a function to escape a string for use in HTML (as in Django's escape filter): def escape(string): """ Returns the given string with ampersands, quotes and angle brackets encoded. """ return string.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace("'", '&#39;').replace('"', '&quot;') This works, but it gets ugly quickly and appears to have poor algorithmic performance (in this example, the string is repeatedly traversed 5 times). What would be better is something like this: def escape(string): """ Returns the given string with ampersands, quotes and angle brackets encoded. """ # Note that ampersands must be escaped first; the rest can be escaped in # any order. return replace_multi(string.replace('&', '&amp;'), {'<': '&lt;', '>': '&gt;', "'": '&#39;', '"': '&quot;'}) Does such a function exist, or is the standard Python idiom to use what I wrote before?

    Read the article

  • Idiomatic PHP web page creation

    - by GreenMatt
    My PHP experience is rather limited. I've just inherited some stuff that looks odd to me, and I'd like to know if this is a standard way to do things. The page which shows up in the browser location (e.g. www.example.com/example_page) has something like: <? $title = "Page Title"; $meta = "Some metadata"; require("pageheader.inc"); ?> <!-- Content --> Then pageheader.inc has stuff like: <? @$title = ($title) ? $title : ""; @$meta = ($meta) ? $meta : ""; ?> <html> <head> <title><?=$title?></title </head> <!-- and so forth --> Maybe others find this style useful, but it confuses me. I suppose this could be a step toward a rudimentary content management system, but the way it works here I'd think it adds to the processing the server has to do without reducing the load on the web developer enough to make it worth the effort. So, is this a normal way to create pages with PHP? Or should I pull all this in favor of a better approach? Also, I know that "<?" (vs. "<?php" ) is undesirable; I'm just reproducing what is in the code.

    Read the article

  • Is there a downside to adding an anonymous empty delegate on event declaration?

    - by serg10
    I have seen a few mentions of this idiom (including on SO): // Deliberately empty subscriber public event EventHandler AskQuestion = delegate {}; The upside is clear - it avoids the need to check for null before raising the event. However, I am keen to understand if there are any downsides. For example, is it something that is in widespread use and is transparent enough that it won't cause a maintenance headache? Is there any appreciable performance hit of the empty event subscriber call?

    Read the article

  • How does DATEDIFF calculate week differences in SQL Server 2005?

    - by eksortso
    I would like to calculate the difference in weeks between two dates, where two dates are considered part of the same week if their preceding Sunday is the same. Ideally, I'd like to do this using DATEDIFF, instead of learning an elaborate idiom to calculate the value. But I can't tell how it works when weeks are involved. The following query returns 1 and 2. This might make sense if your calendar week begins with a Sunday, i.e. if you run SET DATEFIRST 7 beforehand or if @@DATEFIRST is 7 by default. SET DATEFIRST 7; -- SET DATEFIRST 1; DECLARE @d1 DATETIME, @d2a DATETIME, @d2b DATETIME ; SELECT @d1 = '2010-04-05', -- Monday @d2a = '2010-04-16', -- Following Friday @d2b = '2010-04-18' -- the Sunday following ; SELECT DATEDIFF(week, @d1, @d2a) AS weekdiff_a -- returns 1 ,DATEDIFF(week, @d1, @d2b) AS weekdiff_b -- returns 2 ; So I expected different results if SET DATEFIRST 1 is executed instead of SET DATEFIRST 7. But the return values are the same, regardless! What is going on here? What should I do to get the correct week differences?

    Read the article

  • Python indentation in "empty lines"

    - by niscy
    Which is preferred ("." indicating whitespace)? A) def foo(): x = 1 y = 2 .... if True: bar() B) def foo(): x = 1 y = 2 if True: bar() My intuition would be B (that's also what vim does for me), but I see people using A) all the time. Is it just because most of the editors out there are broken?

    Read the article

  • How to sift idioms and set phrases apart from other common phrases using NLP techniques?

    - by hippietrail
    What techniques exist that can tell the difference betwen plain common phrases such as "to the", "and the" and set phrases and idioms which have their own lexical meanings such as "pick up", "fall in love", "red herring", "dead end"? Are there techniques which are successful even without a dictionary, statistical methods HMMs train on large corpora for instance? Or are there heuristics such as ignoring or weighting down "promiscuous" words which can co-occur with just about any word versus words which occur either alone or in a specific limited set of idiomatic phrases? If there are such heuristics, how do we take into account set phrases and verbal phrases which do incorporate promiscuous words such as "up" in "beat up", "eat up", "sit up", "think up"? UPDATE I've found an interesting paper online: Unsupervised Type and Token Identi?cation of Idiomatic Expressions

    Read the article

  • Haskell function composition (.) and function application ($) idioms: correct use.

    - by Robert Massaioli
    I have been reading Real World Haskell and I am nearing the end but a matter of style has been niggling at me to do with the (.) and ($) operators. When you write a function that is a composition of other functions you write it like: f = g . h But when you apply something to the end of those functions I write it like this: k = a $ b $ c $ value But the book would write it like this: k = a . b . c $ value Now to me they look functionally equivalent, they do the exact same thing in my eyes. However, the more I look, the more I see people writing their functions in the manner that the book does: compose with (.) first and then only at the end use ($) to append a value to evaluate the lot (nobody does it with many dollar compositions). Is there a reason for using the books way that is much better than using all ($) symbols? Or is there some best practice here that I am not getting? Or is it superfluous and I shouldn't be worrying about it at all? Thanks.

    Read the article

  • Any good idioms for error handling in straight C programs?

    - by Will Hartung
    Getting back in to some C work. Many of my functions look like this: int err = do_something(arg1, arg2, arg3, &result); With the intent the result gets populated by the function, and the return value is the status of the call. The darkside is you get something naive like this: int err = func1(...); if (!err) { err = func2(...); if (!err) { err = func3(...); } } return err; I could macro it I suppose: #define ERR(x) if (!err) { err = (x) } int err = 0; ERR(func1(...)); ERR(func2(...)); ERR(func3(...)); return err; But that only works if I'm chaining function calls, vs doing other work. Obviously Java, C#, C++ have exceptions that work very well for these kinds of things. I'm just curious what other folks do and how other folks do error handling in their C programs nowadays.

    Read the article

  • Origin of discouraged perl idioms: &x(...) and sub x($$) { ... }

    - by knorv
    In my perl code I've previously used the following two styles of writing which I've later found are being discouraged in modern perl: # Style #1: Using & before calling a user-defined subroutine &name_of_subroutine($something, $something_else); # Style #2: Using ($$) to show the number of arguments in a user-defined sub sub name_of_subroutine($$) { # the body of a subroutine taking two arguments. } Since learning that those styles are not recommended I've simply stopped using them. However, out of curiosity I'd like to know the following: What is the origin of those two styles of writing? (I'm sure I've not dreamt up the styles myself.) Why are those two styles of writing discouraged in modern perl? Have the styles been considered best practice at some point in time?

    Read the article

  • How can I learn to effectively write Pythonic code?

    - by Matt Fenwick
    I'm tired of getting downvoted and/or semi-rude comments on my Python answers, saying things like "this isn't Pythonic" or "that's not the Python way of doing things". To clarify, I'm not tired of getting corrected and downvoted, and I'm not tired of being wrong: I'm tired of feeling like there's a whole field of Python that I know nothing about, and seems to be implicit knowledge of experienced Python programmers. Doing a google search for "Pythonic" reveals a wide range of interpretations. The wikipedia page says: A common neologism in the Python community is pythonic, which can have a wide range of meanings related to program style. To say that code is pythonic is to say that it uses Python idioms well, that it is natural or shows fluency in the language. Likewise, to say of an interface or language feature that it is pythonic is to say that it works well with Python idioms, that its use meshes well with the rest of the language. It also discusses the term "unpythonic": In contrast, a mark of unpythonic code is that it attempts to write C++ (or Lisp, Perl, or Java) code in Python—that is, provides a rough transcription rather than an idiomatic translation of forms from another language. The concept of pythonicity is tightly bound to Python's minimalist philosophy of readability and avoiding the "there's more than one way to do it" approach. Unreadable code or incomprehensible idioms are unpythonic. I suspect one way to learn the Pythonic way is just to program in Python a whole bunch. But I bet I could write a bunch of crap and not improve that much without some guidance, whereas a good resource might speed up the learning process significantly. PEP 8 might be exactly what I'm looking for, or maybe not. I'm not sure; on the one hand it covers a lot of ground, but on the other hand, I feel like it's more suited as a reference for knowledgeable programmers than a tutorial for fresh 'uns. How do I get my foot in the Pythonic/Python way of doing things door?

    Read the article

  • Configure autocomplete in intellij with hamcrest and mockito dsl

    - by sgargan
    I'm wondering if its possible to configure Intellij's smart insert to suggest hamcrest and mockito dsl idioms when in test classes. Really I'm looking for something like eclipse's 'static favorites', so that when I hit ctrl + space or ctrl +shift +space the idioms are present. In general is it possible to configure autocomplete/smart insert or the suggestions balloon for that matter. I've looked through the settings without much luck but could easily have overlooked something.

    Read the article

  • Evolution of mainstream programming languages: simplicity versus complexity.

    - by Giorgio
    I had posted this question on http://stackoverflow.com but I was suggested that it may be more appropriate to post it on this forum. I did a quick search on this site and it seems to me that this question has not been asked yet. Please give me a hint if the topic has been raised already by someone else. Update I have rephrased this question, removed personal opinions and made it shorter. I hope in this way it is better suited for this forum. By looking at the recent development of Java (Java 7) and C++ (C++0x) I see that new features are added to these languages. For sure this makes it easier to use certain programming idioms, adding to the productivity of developers. On the other hand, there might be the following risks A language becomes too big, complex, and difficult to understand. It lacks coherence in the design, e.g. if it mixes different paradigms like object-orientation and functional programming, which might not fit well together. Questions: what is more important to you as a developer: to have a rich language that captures a large collection of programming idioms or to have a small language that aims at coherence and simplicity (of course, with a good deal of libraries and tools accompanying it)? Or is it possible to have both? With respect to these issues: How do you judge the current evolutions of main-stream programming languages like Java or C++? Are they becoming too complex, less intuitive? Do they have enough features? Do they need more? Are they still easy enough to understand and use?

    Read the article

  • Advanced example-driven C book with a lot of code.

    - by Inso Reiges
    Hello, I am looking for a book on advanced C programming that: Teaches how to effectively express one's solution in C when one already knows the language in depth. Shows some common design idioms expressed in C, like encapsulation, modularity and that kind of thing. Is example-driven with a lot of good-quality code. I already know the language itself so books like otherwise wonderful "Expert C Programming" by Peter van der Linden is not really what i am looking for. What i need is a book on how to express my design in C, what are the common idioms, best practices, etc. I would also like to note that i am primarily interested in C, not C++, C#, Objective-C or any other languages inspired by C-like syntax. Thank you.

    Read the article

  • Google I/O 2012 - Migrating Code from GWT to Dart

    Google I/O 2012 - Migrating Code from GWT to Dart Ray Cromwell Curious to learn how to port your GWT code to Dart? In this session, we will go over Dart equivalents for various GWT libraries and idioms, techniques for interoperating with existing GWT server backends, and tricks to allow Dart code to talk to existing GWT and Javascript code. For all I/O 2012 sessions, go to developers.google.com From: GoogleDevelopers Views: 178 2 ratings Time: 57:46 More in Science & Technology

    Read the article

  • What do you code first to learn a new language, library, or framework?

    - by Griffin
    Every language, framework, and library has its own syntax, quirks, and pitfalls. What Program, Game, etc. do you code in order to learn these unique characteristics? How do you decide on what previous programming experience is applicable? I'd imagine that the task would have to be complicated enough to force you to use applicable programming techniques and idioms, but simple enough that it wouldn't take a ton of time.

    Read the article

  • Real World Java EE Patterns by Adam Bien

    - by JuergenKress
    Rethinking Best Practices, A book about rethinking patterns, best practices, idioms and Java EE Real World Java EE Patterns - Rethinking Best Practices discusses patterns and best practices in a structured way, with code from real world projects. This book covers: an introduction into the core principles and APIs of Java EE 6, principles of transactions, isolation levels, CAP and BASE, remoting, pragmatic modularization and structure of Java EE applications, discussion of superfluous patterns and outdated best practices, patterns for domain driven and service oriented components, custom scopes, asynchronous processing and parallelization, real time HTTP events, schedulers, REST optimizations, plugins and monitoring tools, and fully functional JCA 1.6 implementation. Real World Java EE Night Hacks - Dissecting the Business Tier will not only help experienced developers and architects to write concise code, but especially help you to shrink the codebase to unbelievably small sizes :-). Order here. WebLogic Partner Community For regular information become a member in the WebLogic Partner Community please visit: http://www.oracle.com/partners/goto/wls-emea ( OPN account required). If you need support with your account please contact the Oracle Partner Business Center. BlogTwitterLinkedInMixForumWiki Technorati Tags: Adam Bien,Real World Java,Java,Java EE,WebLogic Community,Oracle,OPN,Jürgen Kress

    Read the article

  • What are some good practices when trying to teach declarative programming to imperative programmers?

    - by ChaosPandion
    I offered to do a little bit training in F# at my company and they seemed to show some interest. They are generally VB6 and C# programmers who don't follow programming with too much passion. That being said I feel like it is easier to write correct code when you think in a functional matter so they should definitely get some benefit out of it. Can anyone offer up some advice on how I should approach this? Ideas Don't focus on the syntax, instead focus on how this language and the idioms it promotes can be used. Try and think of examples that are a pain to write in an imperative fashion but translates to elegant code when written in a declarative fashion.

    Read the article

  • Is there a good book to grok C++?

    - by Paperflyer
    This question got me thinking. I would say I am a pretty experienced C++ programmer. I use it a lot at work, I had some courses on it at the university, I can understand most C++ code I find out there without problems. Other languages you can pretty much learn by using them. But every time I use a new C++ library or check out some new C++ code by someone I did not know before, I discover a new set of idioms C++ has to offer. Basically, this has lead me to believe that there is a lot of stuff in C++ that might be worth knowing but that is not easily discoverable. So, is there a good book for a somewhat experienced C++ programmer to step up the game? You know, to kind of 'get' that language the way you can 'get' Ruby or Objective-C, where everything just suddenly makes sense and you start instinctively knowing 'that C++ way of thing'?

    Read the article

< Previous Page | 1 2 3 4 5  | Next Page >