Dealing with the node.js callback pyramid

I've just started using node, and one thing I've quickly noticed is how quickly callbacks can build up to a silly level of indentation:               doStuff(arg1, arg2, function(err, result) {         doMoreStuff(arg3, arg4, function(err, result) {             doEvenMoreStuff(arg5, arg6, function(err, result) {                 omgHowDidIGetHere();             });         });     });      The official style guide says to put each callback in a separate function, but that seems overly restrictive on the use of closures, and making a single object declared in the top level available several layers down, as the object has to be passed through all the intermediate callbacks. Is it ok to use function scope to help here? Put all the callback functions that need access to a global-ish object inside a function that declares that object, so it goes into a closure?               function topLevelFunction(globalishObject, callback) {              function doMoreStuffImpl(err, result) {             doMoreStuff(arg5, arg6, function(err, result) {                 callback(null, globalishObject);             });         }              doStuff(arg1, arg2, doMoreStuffImpl);     }      and so on for several more layers... Or are there frameworks etc to help reduce the levels of indentation without declaring a named function for every single callback? How do you deal with the callback pyramid?


one programmer, many languages -- the name dilemma

When you work across multiple programming languages, there is a problem you encounter... _**A valid name (identifier) in one language is invalid in another._** For example... `var` `new` `function` `this` are a keywords in JavaScript, but you can use them freely in Python. Similarly `list` `dict` `def` can be used in JavaScript without problems. This is a very common and something programmers generally quickly become acquainted with when they program in multiple languages. However, when you're working in collaboration, you have to lay out some rules/guidelines for your team members to ensure consistency and uniformity in the code. With teams, this issue becomes more important than simply remembering what's valid and what's not while you program. So, my question is, what strategies you adopt...   * simply take a union of all the reserved words present in all the languages you use, hand out a list to everybody and abstain their use?   * accept the diversity and take extra pains when "context switching"   * adopt an intermediate ground where one language can use the other's, but not vice-versa (Note: i am only talking about Python and JavaScript in this question ... but please answer the question more broadly) **\-- UPDATE --** Thanks for all the answers. So the general consensus i see emerging is to let programmers use any name regardless of what they do other languages -- as long as names are descriptive, it doesn't hurt.


What would you do if a senior level programmer stole your code?

This was my experience at a previous company. As it was a small startup company, some of the normal software development procedures were not followed strictly. One of my colleagues was a senior programmer with the company for 2 years. His skills were quite lacking. He would allocate his tasks to me and then take credit for the work himself. He did this regularly, while I would also have to finish my own tasks. I felt that I couldn't express this situation to senior management as that colleague had their trust. Later, I tried to delay his tasks assigned to me so that reflect poorly on him (as he couldn't finish tasks) Also, client complaints piled up unresolved issues. Now the company knows about him. Actually, now I'm heading to a new company as a Lead. Now I'm completely free with these situation. Have any of you experienced situations like this? What would/did you do?


What happened to the this type of naming convention?

I have read so many docs about naming conventions, most recommending both Pascal and Camel naming conventions. Well, I agree to this, it's ok. This might not be pleasing to some, but I am just trying to get your opinion on why you name your objects and classes in a certain way. What happened to this type of naming conventions, and/or why are they bad? I want to name a structure, and I prefix it with "struct". My reason is that, with IntelliSense, I see all structures in one place, and anywhere I see the struct prefix, I know it's a "struct":               structPerson     structPosition      another example is the enum, although I may not prefix it with "enum", but maybe with "enm":               enmFruits     enmSex      again my reason is that in IntelliSense, I see all my enumerations in one place. Because .NET has so many built-in data structures, I think this helps me do less searching. Note that I used .NET in this example, but I welcome language agnostic answers.


What did Alan Kay mean by "assignment" in The Early History of Smalltalk?

I have been reading The Early History of Smalltalk and there are a few mentions of "assignment" which make me question my understanding of its meaning: > Though OOP came from many motivations, two were central. The large scale one > was to find a better module scheme for complex systems involving hiding of > details, and the small scale one was to find a more flexible version of > assignment, and then to try to eliminate it altogether. (from _1960-66--Early OOP and other formative ideas of the sixties_ , Section I) > What I got from Simula was that you could now replace bindings and > assignment with _goals_. The last thing you wanted any programmer to do is > mess with internal state even if presented figuratively. Instead, the > objects should be presented as _site of higher level behaviors more > appropriate for use as dynamic components_. (...) It is unfortunate that > much of what is called "object-oriented programming" today is simply old > style programming with fancier constructs. Many programs are loaded with > "assignment-style" operations now done by more expensive attached > procedures. (from _"Object-oriented" Style_ , Section IV) Am I correct in interpreting the intent as being that objects are meant to be façades and any method (or "message") whose purpose is to set an instance variable on an object (i.e. an "assignment") is defeating the purpose? This interpretation appears to be supported by two later statements in Section IV: > Four techniques used together--persistent state, polymorphism, > instantiation, and methods-as-goals for the object--account for much of the > power. None of these require an "object-oriented language" to be employed-- > ALGOL 68 can almost be turned to this style--and OOPL merely focuses the > designer's mind in a particular fruitful direction. However, doing > encapsulation right is a commitment not just to abstraction of state, but to > eliminate state oriented metaphors from programming. ...and: > Assignment statements--even abstract ones--express very low-level goals, and > more of them will be needed to get anything done. Generally, we don't want > the programmer to be messing around with state, whether simulated or not. Would it be fair to say that opaque, immutable instances are being encouraged here? Or is it simply _direct_ state changes that are discouraged? For example, if I have a `BankAccount` class, it's OK to have `GetBalance`, `Deposit` and `Withdraw` instance methods/messages; just make sure there isn't a `SetBalance` instance method/message?


What are the types of dynamically typed languages arrays?

For example, in JavaScript, I can do such things:               var arr = [1, "two", /three/, [4]];      There is no way to do such a thing in C! Except by using a `void*`, which is not an efficient/safe way. Is this how implementations do it? Using `void*` everywhere?


Recommended Payment Schedule for Freelance Development

I've been a freelance developer for many years and am setting up a system designed to attract clients through one of my developer websites. If successful, these would be clients I do not know, and some jobs may be very small. When working with clients I know on larger projects, I'm accustomed to doing the work before I get paid. But, here, that seems risky. I'm looking for suggestions on the payment schedule I should require for small projects. I suspect potential clients will not be willing to pay the full amount up front, and so the solution will probably be some sort of compromise.


Event notification for ::SCardListReaders()

In the PC/SC (Personal Computer Smart Card) Appln, I have (MSCAPI USB CCID based) 1) Calling ::SCardListReaders() returns SCARD_E_NO_READERS_AVAILABLE (0x8010002E). This call is made after OS starts fresh after reboot, from a thread which is part of my custom windows service. 2) Adding delay before ::SCardListReaders() call solves the problem. 3) How can I solve this problem elegantly ? Not using delay & waiting for some event to notify me. since a) Different machines may require different delay values b) Cannot loop since the error code is genuine c) Could not find this event as part of System Event Notification Service or similar COM interface d) platform is Windows 7 Any Help Appreciated.


How to know whether to create a general system or to hack a solution

I'm new to coding , learning it since last year actually. One of my worst habits is the following: Often I'm trying to create a solution that is too big , too complex and doesn't achieve what needs to be achieved, when a hacky kludge can make the fit. One last example was the following (see paste bin link below) http://pastebin.com/WzR3zsLn After explaining my issue, one nice person at stackoverflow came with this solution instead http://stackoverflow.com/questions/25304170/update-a-field- by-removing-quarter-or-removing-month When should I keep my code simple and when should I create a 'big', general solution? I feel stupid sometimes for building something so big, so awkward, just to solve a simple problem. It did not occur to me that there would be an easier solution. Any tips are welcomed. Best


How can I speed up the process of typing up specification during a meeting with developers?

I'm studying source code with other developers, and my job is to type up the specifications as the lead programmer describes what each function does. Are there any tips/tricks to doing this faster? a lot of times I have to ask him to repeat himself, as I didn't hear it well. How can I be a better specification writer?


What options are there for splitting UI layout from code logic using a markup language?

What tools similar to GWT's UIBinder exist in other languages? By this I mean a system where you can define your UI layout in a markup language (preferably html+css) and attach the functionality to the layout using the code. I'm most interested in anything for python, but answers in other languages would interest me as well. I'm interested because the benefits of having a non-programmer work directly on the layout without needing to touch the code and adjust a bunch of UI toolkit method calls is very productive. I'm aware of Flex for flash, but is there anything else out there? What search terms might I use to find such frameworks? I've looked around but I haven't found anything concrete. Edit: I'm not only interested in web frameworks, but desktop applications as well. Edit2: My question appears to be close to a duplicate of http://stackoverflow.com/questions/2962874/what-are-the-open-source- alternatives-to-wpf-xaml


How can I satisfy this project requirement?

I'm part of a two-person team that is building a web-based virtual tour application with specific extra functionality that doesn't exist in current applications. This is a summer school project. The team lead wants this application to be as platform and dependency-agnostic as possible. To do this, we're using Google's Fusion Tables and StreetView APIs to display the images and serve as a database. This way, a client installing this application on their server doesn't have to worry about configuring and maintaining a local database and all that is necessary on their end is a Google Apps account and the images themselves. My issue is how does the client make requests for images and data without a server-side language? Most, if not all of the data will be textual, and thus be able to render as JSON and parsed as such. However, that leaves the issue of the images themselves. They will be hosted on a to-be-determined location, and I need some way to access them. Using Ajax, I can execute a PHP call to load an image easily, and I already know how to do this. Its been suggested that I use the Java client library to interface with GData services, but I think this is overcomplicating things. My arguments for including PHP as an application requirement are   1. PHP is synonymous with hosting. I've yet to run across a commercial host that doesn't offer PHP   2. I already know how to implement the Ajax calls to the PHP function.    3. As this application has a 6 week deadline, I'd like to stick with what I know rather than learning a whole new methodology His arguments for looking somewhere else are   1. No guarantee that PHP will be loaded on the server to be used. Our school's CMS is based in Java, and PHP is not installed   2. Java is a standard inclusion in server and client configruations    3. The application should be server and dependency agnostic in the sense that it can be uploaded to a folder and accessed without any configuration Honestly, if I had the time, it would be an interesting side project to include this type of functionality, especially since I've never done it before and it sounds interesting. But I keep thinking about that 6 week deadline, and as I'm the one that will be doing the majority of the coding, I just don't think I can do it in time without PHP.


Implementation of a serial communication protocol

I need to implement a serial protocol to communicate with a device using .NET (C#). This implementation should be a library (.dll) to be used in different projects. I have the datasheet that describe the protocol (baudrate, stop bits, message protocol, commands, etc.) and I know that I need to use `SerialPort` class from `System.IO.Ports` namespace. But I have some doubts on how to struture/organize the code. Here are some questions:   1. Should I be concern about thread management or this aspect should be managed by how is using the library?   2. Can I manage received/sent data using strings or should I use bytes?   3. Commands and fixed contents should be stored using `enums`, `constants` or something else? Maybe those questions could be subjectives, but I would like to have some feedback from someone with more experience than me. If someone has some examples, tips, best pratices, etc. on this subject, I will be very grateful.


which directory to initialize git repo

I'm just getting started with Git. I'll be doing PHP development and was wondering if I should initialize my repo within my server root folder, or any directory on my hard drive and publish my changes to the server root. Does it really matter, any pros/cons? For now, this is just to keep track of personal projects I'm doing for fun, but any feedback on best practice, personal preference/experience, do's and don'ts will be greatly appreciated.


What is the benefit of hypermedia (HATEOAS)?

I don't understand the benefit to HATEOAS for APIs intended for use by programs (as opposed to humans directly browsing your API). Sure, the customer isn't bound to a URL schema but they are bound to a data schema which is the same thing in my mind. For example, assume I want to view an item on an order, let's assume I've discovered or know the order URL already. HATEOAS:               order = get(orderURL);     item = get(order.itemURL[5]);      non-HATEOAS:               order = get(orderURL);     item = get(getItemURL(order,5));      In the first model I have to know the fact that the order object has an itemURL field. In the second model I have to know how to construct an item URL. In both cases I have to "know" something ahead of time so what is HATEOAS actually doing for me?


Why REST Api do not follow the Facade design pattern

In comparing REST [api] structure with a OO model, I see these similarities: Both:   * Are data oriented      * REST = Resources      * OO = Objects   * Surround operation around data      * REST = surround VERBS (Get, Post, ...) around resources     * OO = promote operation around objects by encapsulation However, good OO practices do not always stand on REST apis when trying to apply the facade pattern for instance: in REST, you do not have 1 controller to handle all requests AND you do not hide internal object complexity. ![Simple object relation between 2 concepts](http://i.stack.imgur.com/SyVav.png) ![Analogy between OO and REST](http://i.stack.imgur.com/1CAFF.png) On the contrary, REST promotes resources publishing of all relations with a resource and other on at least two forms:   1. via resource hierarchy relations (A contact of id 43 is composed of an address 453) : `/api/contacts/43/addresses/453`   2. via links in a REST json response: > >     >> GET /api/contacts/43 >     << HTTP Response { >        id: 43, ... >        addresses: [{ >           id: 453, ... >        }], >        links: [{ >           favoriteAddress: { >               id: 453 >           } >        }] >     } >   ![Basic complexity hidden by objectA](http://i.stack.imgur.com/xwaSC.png) Coming back to OO, the facade design pattern respect a `Low Coupling` between an _objectA_ and its ' _objectB client_ ' and `High Cohesion` for this _objectA_ and its internal object composition ( _objectC_ , _objectD_ ). With the _objectA_ interface, this allow a developer to limit impact on _objectB_ of the _objectA_ internal changes (in _objectC_ and _objectD_ ), as long as the _objectA_ api (operations) are still respected. In REST, the data (resource), the relations (links), and the behavior (verbs) are exploded in different elements and available to the web. Playing with REST, I always have an impact on code changes between my client and server: Because I have `High Coupling` between my `Backbone.js` requests and `Low Cohesion` between resources. I never figured out how to let my `Backbone.js javascript application` deal with " _REST resources and features_ " **discovery** promoted by REST links. I understand that the WWW is meant to be served by multi servers, and that the OO elements had to be exploded to be serviced by many hosts in there, but for a simple scenario like "saving" a page showing a contact with its addresses, I end up with: > >     GET /api/contacts/43?embed=(addresses) >     [save button pressed] >     PUT /api/contacts/43 >     PUT /api/contacts/43/addresses/453 >   which lead me to move the saving action atomic transactional responsibility on the browsers applications (since two resources can be addressed separately). _With this in mind, if I cannot simplify my development (Facade design patterns not applicable), and if I bring more complexity to my client (handling transactional atomic save), where is the benefit of being RESTful ?_


What is the simplest archive file format to aim for when writing collections of files?

I've created a PHP-based document management system and hosted it on my Raspberry Pi. I created a "backup" function that zips together all the documents, but it takes too long due to the hardware constraints of the Pi, as well as the fact that compression cannot be disabled when creating zip files in PHP. This has led me to think that perhaps I should just devise some sort of binary file format that allows multiple files to be stored in it, and dump all my uncompressed documents into it when the user asks for backups. Oddly, I'm not necessarily concerned about the lack of possible "unarchivers". Which file format is the easiest to implement, whether a standard format, or a proposed new format? Is there any other alternative for solving my problem that does not require implementing a custom archiver? Note that I wish to avoid the following:   * Shell commands (not portable).   * Installing third party dependencies (may not work on a third party hosted system).   * Rewriting my system in a different language.


Should my colleagues review each others code from source control system?

So that's my story: one of my colleagues uses to review all the code, hosted to revision system. I'm not speaking about adequate review of changes in parts that he belongs to. He watches the code file to file, line to line. Every new file and every modified. I feel just like being spied on! My guess is that if code was already hosted to control system, you should trust it as workable at least. My question is, maybe I'm just too paranoiac and practice of reviewing each others code is good? P.S: We're team of only three developers, and I fear that if there will be more of us, colleague just won't have time to review all the the code we'll write.


Are there efforts to build a collaboratively edited HTML/JS/DOM reference?

W3Schools has a reputation of being incomplete, sometimes incorrect, and ridden with advertising; still, when looking to look up some things or link to documentation when answering a SO question, it still is the only handy cross- browser resource. There are other resources like the Mozilla Developer Network that is doing an increasingly great job documenting JavaScript, and the legendary and great Quirksmode. But they, as brilliant as they are, cover only parts of the areas I am talking about, and provide no community editing and quality control options. Is anybody aware of efforts to create a collaboratively edited, cross-browser HTML/CSS/JavaScript/DOM encyclopedia? If you will, I'm thinking of a challenger to W3Schools like SO was to Experts Exchange.


Scripts to run Java programs - e.g. Ant

My team currently use an Ant script to _execute_ various Java programs. Note that I am not asking about managing the build/deployment cycle for which we are already using Maven (quite happily). For example, we use something like the following:               <path id="lib">       <fileset dir="path/to/lib" />     </path>          <path id="run" >       <path refid="lib" />     </path>          <target name="aJavaProgram" >       <java classname="path.to.AJavaProgam"              classpathref="run" fork="yes" failonerror="false" append="true" />     </target>          <target name="anotherJavaProgram" depends="aJavaProgram" >       <java classname="path.to.AnotherJavaProgam"              classpathref="run" fork="yes" failonerror="false" append="true" />     </target>      We originally chose Ant for the following reasons (or rather I didn't choose Ant, it was that way when I joined, but I think these are good reasons for the choice):   * it was an easy way of managing dependencies (although we now pick up these from Maven)   * it allows us to easily build/maintain dependencies between particular jobs   * we can deploy the Ant script, and the Java libraries, and easily run particular jobs - i.e.      * we don't have to worry about classpaths (configured in the Ant script)     * we don't have to worry about building jars with main classes specified in the manifest     * we can use Ant to create / store / manage arguments passed to main() methods   * we can plug these ant scripts into our IDEs and run the jobs from there. Generally speaking, we have been very happy with this. However, an increasing issue is the ant-java-shutdown hook issue \- this means that if one of these Java programs fails but doesn't finish, terminating the Ant process from which it was started doesn't terminate the Java process - which then has to be done manually, and is a real bind. Also, I'm conscious of two other (possible) factors:   * this is using Ant in a slightly unusual way, which is perhaps not how Ant was intended to be used (or at least, so it seems to me...)?   * I'm not really sure, but it seems that fewer and fewer developers/teams are using Ant these days, with preference for other technologies. So, to clarify:   * I want a flexible approach to execute various Java programs   * the approach that we have been using was to do this via Ant scripts, but this had the following problems:      * terminating the Ant task does not kill the Java program      * this "feels" like Ant is not the right tool for the job   * how might we meet the needs specified above?      * if not Ant, the solution should not face the termination / shutdown hook issue


Is this a good practice or not?

I have a colleague who has come up with a way of 'genericizing' information from a database so that all his web application's drop-down lists can share the same object in his MVC.NET C# code and Views, which can contain different data depending on what tables it is being used against. We work for a government agency, and we have facilities divided up into areas called "Regions", which contain further subdivided areas called VISNs, which each contain "Sites" or "Facilities". My colleague has developed a very complex security scheme whereby people can be granted access to data based on their permission level which can be assigned by Region, VISN, Site, or any mixture of the three. It's a nice scheme and very clever. However, he has stored procedures that return lists of Regions, VISNs, and Sites based on a person's User Id, and he is returning "generic" values like `TextFieldID`, `TextField`, and `TextParentID`. My first problem with this, is that looking at this data coming out of the database, I would not know what the data is. I feel that fields coming from a query or stored procedure should be descriptive of the data they are delivering. What does everyone else think? The deeper issue for me however is that he is taking some of the data, concatenating it in his stored procedure like this               SELECT DISTINCT         t.VisnID,         NULL,         t.StationID,         'V' + CAST(t.VisnID as varchar) + ': ' + t.Station3N + ': ' + t.StationName,         t.Inactive     FROM Stations t       and sending it back in a "TextField" property, instead of sending back the discrete data separately (`Station3N`, `StationName`) and concatenating it in the View, which would allow for different concatenation depending on what device what accessing the application (perhaps mobile and desktop). His justification is that he can send all his various drop down data and capture them all, regardless of content, in the same C# object named "LookupValue."               public partial class LookupValue : IEquatable<LookupValue>     {         public LookupValue(string textFieldId, string textField, bool inactive)         {             TextFieldID = textFieldId;             TextField = textField;             Inactive = inactive;         }              public LookupValue(string textParentId, string textFieldId, string textField, bool inactive)         {             TextParentID = textParentId;             TextFieldID = textFieldId;             TextField = textField;             Inactive = inactive;         }              public LookupValue(string textParentId, string textSelfParentId, string textFieldId, string textField, bool inactive)         {             TextParentID = textParentId;             TextSelfParentID = textSelfParentId;             TextFieldID = textFieldId;             TextField = textField;             Inactive = inactive;         }              /// <summary>         /// Returns a custom string identifier if the Inactive property is true.         /// </summary>         public string GetInactiveString()         {             string value = "";             if (Inactive)             {                 value = "[I]";             }                  return value;         }              /// <summary>         /// Returns a custom text label that concatenates the TextField property with the custom string identifier of the Inactive property.         /// </summary>         public string GetDisplayNameWithInactiveString         {             get { return TextField + " " + GetInactiveString(); }         }              public bool Equals(LookupValue other)         {             //Check whether the compared object is null.               if (Object.ReferenceEquals(other, null)) return false;                  //Check whether the compared object references the same data.               if (Object.ReferenceEquals(this, other)) return true;                  //Check whether the products' properties are equal.               return TextFieldID.Equals(other.TextFieldID) && TextField.Equals(other.TextField);         }              // If Equals() returns true for a pair of objects         // then GetHashCode() must return the same value for these objects.         public override int GetHashCode()         {             //Get hash code for the TextFieldID field if it is not null.               int hashTextFieldId = TextFieldID == null ? 0 : TextFieldID.GetHashCode();                  //Get hash code for the Code field.               int hashTextField = TextField.GetHashCode();                  //Calculate the hash code for the product.               return hashTextFieldId ^ hashTextField;         }     }      He believes the re-usability of this object is worth the violation of Separation of Concerns and possible future difficulties in handling more than one display variation for a drop-down. I should point out that this object is contained in his Data project namespace (our projects are separated into Data, Web, Services, etc.) instead of the Web project namespace and he also returns this object to the Web layer via his Repository queries which call the stored procedures that I described earlier, which is gross violation of Separation of Concerns in my opinion. I am just looking for some confirmation from other programmers that this is in fact a bad practice, and also looking for ideas I can present him on better ways to do what he is attempting. I have my own ideas, but again I'm just looking for other's ideas so I have more options to present to him. \--Edit -- I forgot to mention that we are using Entity Framework as an ORM, and his Repository classes in his DAL are dumping the data from the stored procs into this LookupValue object.


Should I prefer instance methods over class methods in Ruby?

I'm working on a rails application, and I've been pulling functionality out of my rails code and into pure ruby classes in lib/. I've found myself often writing classes like this:                class MailchimpIntegration          def subscribe_email(email, fname, lname)         Gibbon.list_subscribe(:id => NEWSLETTER_LIST_ID, :email_address => email,           :merge_vars => {'fname' => fname, 'lname' => lname },           :email_type => "html",  :double_optin => false, :send_welcome => false)       end            def unsubscribe_email(email)         Gibbon.list_unsubscribe(:id => NEWSLETTER_LIST_ID, :email_address => email)       end            def change_details(old_email, email, fname, lname)         Gibbon.list_update_member(:id => NEWSLETTER_LIST_ID, :email_address => old_email,           :merge_vars => {'email' => email, 'fname' => fname, 'lname' => lname })       end            def get_email_info(email)         Gibbon.list_member_info(:id => NEWSLETTER_LIST_ID, :email_address => [email])["data"].first       end     end      My question is: Should I change these methods to be class methods? It seems reasonable to do, as I'll probably end up calling these by just newing up a MailchimpIntegration class each time. However, I generally prefer to have instance methods as they can be more easily stubbed etc, although this seems to be less of an issue in ruby. I have several classes like this in my system, so I'd be keen to see what people think about this.


The role of determination of the variable type (floating point or fixed point) in program performance

I know with changing the variable type, the speed of the program change. I want to understand the other effects of this change in the software performance. I want to know if we use a floating point variable instead of fixed point variable,the amount of memory consumption and speed of program, what will change?


How to design highly scalable web services in Java?

I am creating some Web Services that would have 2000 concurrent users. The services are offered for free and are hence expected to get a large user base. In the future it may be required to scale up to 50,000 users. There are already a few other questions that address the issue like - http://stackoverflow.com/questions/2567254/building-highly-scalable-web- services However my requirements differ from the question above. For example - My application does not have a user interface, so images, CSS, javascript are not an issue. It is in Java so suggestions like using HipHop to translate PHP to native code are useless. Hence I decided to ask my question separately. This is my project setup -   1. Rest based Web services using Apache CXF   2. Hibernate 3.0 (With relevant optimizations like lazy loading and custom HQL for tune up)   3. Tomcat 6.0   4. MySql 5.5 What are the best practices to abide by in order to make a Java based application scalable?


Learning about a new technology

I have approx 9 years of experience in the IT field. I've mainly worked on web applications and currently am looking forward to become a technical architect for J2EE applications. I usually face this challenge; in order to learn a new technology in depth I usually refer to a book. I was recently going through 'Java Persistence with Hibernate' to get in depth knowledge on Hibernate. It takes usually a month for me to complete reading the books/doing some R & D to know about how the APIs work and writing some sample program from books. I should mention that my reading is mostly done in spare time out of my regular project work. At the end of this exercise I get a decent idea about the technology and how it works (its uses in applications etc...). However after couple of months, I also start forgetting about what I read in my previous book (or about previous technology). Mainly because the project on which I work on do not use those technology, so if you do not practice, you will forget those things. For ex: I recently read about FTL, but in my project at the same time we were using plain JSPs, so I did not get chance to apply my FTL knowledge there and I forgot most of what I read about FTL. On one hand, I love to know more about the technologies around us and at the same time I want to keep that knowledge with me so that I could use it in future when needed. What am I doing wrong here? I sometime think that:   1. I should spend more time on one technology before switching on next, so that I could remember most of it.   2. My approach towards learning about new things this way is wrong.   3. Learning through Books might not be a good approach.   4. I need to do something extra to keep my learning up to date (but frankly what is that extra; I don't know). I need some help here.


When should I use StringBuilder or StringBuffer?

In a production web application, my fellow programmers used StringBuffer everywhere. Now I am taking care of application development and corrections. After reading StringBuilder and StringBuffer I have decided to replace all the StringBuffer code with StringBuilder because we don't need thread safety in our data beans. For example: (In each data bean I can see the use of StringBuffer)               @Override     public String toString() {         StringBuffer sb = new StringBuffer();// replace it from StringBuilder         sb.append(" ABCD : ").append(abcd);         sb.append(", EFGH : ").append(efgh);         sb.append(", IJKL : ").append(ijkl);     }      We create a separate data beans for each session/request. A session is used by a single user no other user can access it. Should I consider other points before migrating? If there is a single thread (no waiting threads/no new thread will be looking for object lock), it performs equally with either StringBuffer or StringBuilder. I know in the case of StringBuffer, it takes time to take the object lock but I want to know if there is any performance difference between them except the hold/release of the object lock.


What is the name of the method to find index from array, in Objective-c?

I have a following method to find index of a book object which have a book name:               +(NSInteger)indexXXX:(NSString *)bookName XXX:(NSArray *)books     {       for (NSInteger i = 0; i < books.count; i++) {         NSDictionary *book = [books objectAtIndex:i];         if ([[book objectForKey:@"bookName"] isEqualToString:bookName]) {           return i;         }       }       return -1;     }      But I don't know what name should I use for this method.   Could you tell me some method names commonly used for this kind of method.


application logic, business logic, models, controllers - where to put the application's brains?

I'm trying to wrap my head around models, views, and controllers, but I feel as though the more I read, the more conflicting information I seem to encounter. I guess the general goal is--as far as I've seen--to shoot for fat models and thin controllers, but I'm still a bit baffled by where to put what. Can I give an example from something I'm working on? This is just a personal project that I'm messing about with in PHP for fun--a mini-game of sorts. Adding timed tasks to an itinerary and collecting rewards when the task completes. So a class kind of like this exists:               class ItineraryEntry {         public $user_id;         public $task_id;         public $completion_time;     }      And another class like this:               class Task {         public $task_id;         public $description;         public $duration; //in seconds     }      So in the user interface, the user sees a task description and clicks something like "Add to Itinerary". Now if I'm understanding correctly, the controller needs to pick up that request and do something with it. But...how much? So for example, I need to figure out when the task will complete. Keeping it simple, that's merely time() + the duration of the task. So does the controller do that calculation and then give it to the model to validate, or does the model just handle all of it? And the controller is really just a glorified router of data? Then the second part is what happens when an itinerary task completes. So from the user interface, a task will show as completed if the completion time is in the past, and the user will have to click something like, "Claim Rewards". Let's just say it's...I don't know...XP (i.e., experience points)--something that gets updated on the user's account. And let's say that the amount of XP is calculated in some way based upon the difficulty of the task, time required, etc. So where would I put that calculation? Is that inside the model class for my user object, or do I have a controller that figures all of that out and then sends it to the user object? In the model for my itinerary entry model? And the reason I ask this specifically is because any kind of Claim Rewards code would have to do a couple of things at least:   1. Actually claim the reward and update the user's account   2. Clean up the expired task from the database (or marking it completed or something) So it would be interacting both with the user and the itinerary entry. A little specific guidance would be really helpful. My brain is starting to hurt from reading all of these explanations that are kind of like what I need to know, but not totally. So thanks in advance!


What should I call the process of converting an object to a string?

We are having a game of 'semantic football' in the office over this matter: I am writing a method for an object which will represent the object as a string. That string should be such that when typed (more likely, cut and pasted) into the interpreter window (I will keep the language name out of this for now), will produce an object which is, for our purposes, identical to the one upon which the method was called. There is a spirited discussion over the 'best' name for this method. The terms **pickle** , **serialize** , **deflate** , _etc_ have been proposed. However, it seems that those terms assume some process for the de-pickling (unserialization, _etc_ ) that is not necessarily the language interpreter itself. That is, they do not specifically refer to the case where strings of valid code are produced. This is closer to a **quine** , but we are re- producing the object not the code, so this is not quite right. any suggestions?


How best do you represent a bi-directional sync in a REST api?

Assuming a system where there's a Web Application with a resource, and a reference to a remote application with another similar resource, how do you represent a bi-directional sync action which synchronizes the 'local' resource with the 'remote' resource? Example: I have an API that represents a todo list. GET/POST/PUT/DELETE /todos/, etc. That API can reference remote TODO services. GET/POST/PUT/DELETE /todo_services/, etc. I can manipulate todos from the remote service through my API as a proxy via GET/POST/PUT/DELETE /todo_services/abc123/, etc. I want the ability to do a bi-directional sync between a local set of todos and the remote set of TODOS. In a rpc sort of way, one could do POST /todo_services/abc123/sync/ But, in the "verbs are bad" idea, is there a better way to represent this action?


Should a view and a model communicate or not?

According to the wikipedia page for the MVC architecture, the view is free to be notified by the model, and is also free to query the model about its current state. However, according to Paul Hegarty's course on iOS 5 at Stanford, lecture 1, page 18 all interaction must go through the controller, with Model and View that are never supposed to know each other. It is not clear to me if Hegarty's statement must be intended as a simplification for the course, but I am tempted to say that he intends the design as such. How do you explain these two opposite points of view ?


Is Lambda function same as Actor? How can it be explained?

Sometime back I read (from a book) that conceptually Lambda function is similar to Actor. However I am clear on the concept, and will appreciate any insight or explanations for this. If they are indeed similar how? If not is there any relationship between the concepts?


What's the best way to learn the MS Business Intelligence stack?

What's the best/easiest way to get into the MS BI stack on my own (preferably with a [kindle] book)? Specifically, I'd like to learn more about SSIS, SSRS, and SSAS in that order. I've tried looking at books on amazon, but I really have no idea. Some background: I just graduated with my bachelor's in computer science, and am going back for my masters in comp sci. I work right now as a C# developer, with some web and database background. I don't necessarily want to become a BI master, but I'd like to gain an intermediate-level understanding of the platform.


What does path finding in internet routing do and how is it different from A*?

**Note:** If you don't understand this question then feel free to ask clarification in the comments instead of voting down, it might be that this question needs some more work at the moment. I've been directed here from the Stack Excange chat room Root Access because my question didn't fit on Super User. * * * In many aspects path finding algorithms like A star are very similar to internet routing. For example: > A node in an A* path finding system can search for a path though edges > between other nodes. > > A router that's part of the internet can search for a route though cables > between other routers. In the case of A*, open and closed lists are kept by the system as a whole, sepratly from any individual node as well as each node being able to temporarily store a state involving several numbers. Routers on the internet seem to have remarkable properties, as I understand it: > They are very performant. > > New nodes can be added at any time that use a free address from a finite > (not tree like) address space. > > It's real routing, like A*, there's never any doubling back for example. > > Similar IP addresses don't have to be geographically nearby. > > The network reacts quickly to changes to the networks shape, for example if > a line is down. > > Routers share information and it takes time for new IP's to be registered > everywhere, but presumably every router doesn't have to store a list of all > the addresses each of it's directions leads most directly to. I'm looking for a basic, general, high level description of the algorithms workings from the point of view of an individual router. **Does anyone have one?** I presume public internet routers don't use A* as the overheads would be to large, and scale to poorly. I also presume there is a single method worldwide because it seems as if must involve a lot of transferring data to update and communicate a reasonable amount of state between neighboring routers. For example, perhaps the amount of data that needs to be stored in each router scales logarithmically with the number of routers that exist worldwide, the detail and reliability of the routing is reduced over increasing distances, there is increasing backtracking involved in parts of the network that are less geographically uniform or maybe each router really does perform an A* style search, temporarily maintaining open and closed lists when a packet arrives.


Is there a massive other side to software development which I've somehow missed, revolving entirely around Microsoft?

I'm still a beginning programmer; I've been at it for 2 years. I've learned to work with a few languages, a bit of web development technologies, a handful of libraries, frameworks, and IDEs. But over the past two years (and long before I even started, really), I keep hearing references to these...things. A million of them. Things such as C#, ADO, SOAP, ASP, ASP.NET, the .NET framework, CLR, F#, etc etc. And I've read their Wikipedia articles, in-depth, multiple times, and they all mention a million other things on that list, but I just can't seem to grasp what it all _is_. The only thing I've taken away with any certainty is that Microsoft is behind all of it. It sounds almost like a conspiracy. Are all these technologies just for developing on the Windows platform? What _is_ .NET? Do some software developers dedicate their entire career just to that side of things? Why would I want to get into it, and what advantage does...whatever it is...have over all the other technologies there are? I hope this makes sense. It's a broad question, but inside it there's a very specific question asking about something I don't know the name of. Hopefully you can grasp my confusion.


How to legally protect yourself from malicious and/or dumb users?

When building a public facing website that allows visitors to post comments, link to media and/or upload media (e.g. audio, video, images) ... what should I do to protect myself legally in the case such visitors link to or upload content that they shouldn't (e.g. adult oriented media, copyrighted images and/or media owned by someone else, etc...)? Some questions that come to mind in particular:   1. Should I allow folks to post anonymously?   2. If I make visitors agree to some kind of statement whereby they take full responsibility for what they upload, what should the copy of such a statement be? Please provide as specific as possible steps one should take if possible.


Qt's future in the light of Nokia-Microsoft partnership

In case you missed it, a lot has happened in the last two day that could potentially impact the Qt framework, for the worse. :-( It will impact the mobile sector in several and probably not currently acknowledged ways, for sure. It started yesterday with Nokia's CEO Stephen Elop internal letter depicting Nokia sitting on a burning platform and the need for a big and aggressive shift in business. A day later, at the Nokia World conference, Nokia announced the partnership with Microsoft, which at the moment resumes to Nokia adopting the Windows Phone 7 platform and development environment, dumping Symbian along the road and tagging Meego as **R &D**(a pretty dangerous keyword if you ask me), as for Maemo/N900 series I guess it's bye bye for good. I know what you're thinking but no, Qt is not going to be ported to the Window Phone platform. And I'm also scared about this. You can watch the Elop & Ballmer joint press release here. Now after reading this huge thread on the Qt-interest mailing list I can't help but wonder, what is the future of Qt at Nokia, now that they aren't focused(at all?) on Qt anymore(remember the **full** focus switch on Qt as main development framework for all Nokia products(including Symbian, yes) back in October?). I love Qt, in my opinion it is the only true cross-platform application development framework and one of the few to make C++ development a joy(to the extent possible) and good things has happened to the framework and considerable momentum while under Nokia, thus i am wondering, what are the chances that Qt might suffer a slow death at Nokia after this? Yes i know about KDE.org and the fact that Qt is easily spawnable, but I still feel uneasy. It also must be horrible for all of the efforts either by Nokia employees or third parties that have gone into Symbian and all of the Ovi Store Symbian/Qt content and business and why not, Maemo/Meego. There are also massive layoffs planned, I suspect Symbian techs and Qt? I'd love to hear your input on this? Is Qt future safe&proof? LE: The question as been gradually revised, improved and better referenced, thus you might want to throw a quick re-read to see what you might have missed.


Should a package manager modify your .bashrc file?

I am writing a package for something that requires an environment variable to be set in order to execute properly. Should the install step of a package manager modify a user's environment, or simply prompt the user to do so themselves? My intuition would be the latter, but I can see arguments for the former.


Provide a URL to a CouchDB document attachment without giving the username/password?

I posted this question on DBA, but it got closed and was never reopened even after I rewrote the whole thing to be more specific. I think its more appropriate for programmers anyway :) ## Background Information I have a CouchDB server (Server B) that is accessed by another server running PHP (Server A). My web app (Client-side Javascript) makes a request for a file to Server A. Server A sends requests to Server B with the access credentials (username:password). Server B returns JSON objects that correspond to matched files, then Server A returns that data to the client. The response includes some meta data about the file and a URL to the file on Server B. The actual file data itself is not included in the response. ## The Problem The URL to the document attachment included in the JSON response from Server B includes access credentials (username:password). Server A responds to the client-side request by returning those JSON objects. The client now has this information:               https://username:password@host:port/dbname/path/to/file.whatever      Currently all data contained within Server B resides in one database instance. If the client has access to the username:password to the CouchDB then all data could be queried with those access credentials. Like so,               https://username:password@host:port/dbname/_all_docs      ## Server-side considerations CouchDB on Cloudant ## Client-side considerations The Javascript running on the client requires a URL string to the file. The files that are being referenced are KML layers for Google Maps. A new KML layer is created using this constructor. ## What I am looking for Essentially my solution would come if I can provide a link to a CouchDB document attachment without giving the username/password.               https://host:port/path/to/file.whatever      ## What I did I had to enable cURL for PHP on Server A.               sudo apt-get install curl libcurl3 libcurl3-dev php5-curl     sudo /etc/init.d/apache2 restart      Got a bit of code from David Walsh's blog. My Javascript client code gets a list of JSON file meta data from Server A. This data includes unique DB IDs and file names (No passwords or API keys). I then pass the document ID and filename to another service on Server A which echoes the value of,               get_data('https://username:password@Server B:port/dbname/path/to/file.whatever');      I can now serve out the files to the client without revealing username or password for CouchDB on Server B.


Eager Loading Constraints

The thing is, eager loading some object with all of it's associations reduces the queries made to the database, but the query time is greatly increased.   The question is: This increased query time in the end pays off the database not being hitted so often? It seems to me that you are avoiding one problem and falling into another one.   Obs.: Would put eager-loading tag if i had reputation enough. Someone could create it and edit this observation.


Why is (position < size) such a prevalent pattern in conditionals?

In a condition statement (IF) everyone use `(position < size)`, but why? Only convention or there is a good reason for that? Example:               if (pos < array.lengh) {        // do some with array[pos];     }      why not               if (array.lengh > pos) {        // do some with array[pos];     }      ?


Teaching Programming Concepts Without a Specific Language

I'm teaming up with a guy who has no programming experience. We're using a tool to make our game (RPG Maker) that has an event-based system that allows you to do pretty much everything you want. They have a GUI and a simple text editor for events. My friend has no programming experience. None. I need him to understand basic stuff, like control flow (if/else, do/while), variables/constants, etc. What can I use to teach him this, bearing in mind that I don't care about specific language syntax? Ideally, I'm looking for a "programming" book that talks about these ideas (perhaps visually) and doesn't care much about code. Does something like this exist? My "google-fu" failed me.


How have languages influenced CPU design?

We are often told that the hardware doesn't care what language a program is written in as it only sees the compiled binary code, however this is not the whole truth. For example, consider the humble Z80; its extensions to the 8080 instruction set include instructions like CPIR which is useful for scanning C-style (NULL-terminated) strings, e.g. to perform `strlen()`. The designers must have identified that running C programs (as opposed to Pascal, where the length of a string is in the header) was something that their design was likely to be used for. Another classic example is the Lisp Machine. What other examples are there? E.g. instructions, number and type of registers, addressing modes, that make a particular processor favour the conventions of a particular language? I am particularly interested in revisions of the same family.


What software models are appropriate for daily builds and continuous integration?

On reading the joel test and about daily builds, a discussion with a few tech- lead friends of mine in various companies revealed that they never did daily builds or continuous integration because according to them:   1. Daily builds were meant for projects following Agile practices. It does not work for the waterfall model.    2. Continuous integration required automated testing which their OpenGL/OGRE/OSG type-of graphics project couldn't use, because apparently automated testing is impractical for projects where the graphics part is under (re)development.    3. One of them believes that it's not necessary to have configuration management. It's just enough to follow the philosophy of it, by defining small 2-3 day tasks, perform all activities of the development cycle for each task (code, code-review, unit test, integration), deliver to integration stream and build and generate the setup.    4. Even if they created scripts for the entire build and burn-to-cd process, creating automated tests were a problem because in any program you create, you can't always know what tests you'd have to run and scripting a test case for it takes up too much time and automated testing tools may not always support the specific kind of tests you might want to make. Is daily builds and continuous integration really practical for non-agile projects? Is it meant only for test driven development? And is it really sufficient to follow the point 3 (above) kind of philosophy? I was talking to them about the build-in-one-step kind of scripted automation that Joel talks about, but they weren't willing to buy the idea. p.s: I've been through the questions on this site about daily builds etc, but I believe this question is different. **EDIT: Point 3 should've started with "One of them believes it isn't necessary to integrate configuration mangaement with development tools"**


Why are cryptic short identifiers still so common in low-level programming?

There used to be _very_ good reasons for keeping instruction / register names short. Those reasons no longer apply, but short cryptic names are still very common in low-level programming. Why is this? Is it just because old habits are hard to break, or are there better reasons? For example:   * Atmel ATMEGA32U2 (2010?): `TIFR1` (instead of `TimerCounter1InterruptFlag`), `ICR1H` (instead of `InputCapture1High`), `DDRB` (instead of `DataDirectionPortB`), etc.   * .NET CLR instruction set (2002): `bge.s` (instead of `branch-if-greater-or-equal.short`), etc. Aren't the longer, non-cryptic names easier to work with? * * * When answering and voting, please consider the following. Many of the possible explanations suggested here apply **equally** to high-level programming, and yet the consensus, by and large, is to use non-cryptic names consisting of a word or two (commonly understood acronyms excluded). Also, if your main argument is about **physical space on a paper diagram** , please consider that this absolutely does not apply to assembly language or CIL, plus I would appreciate if you show me a diagram where terse names fit but readable ones make the diagram worse. From personal experience at a fabless semiconductor company, readable names fit just fine, and result in more readable diagrams. What is the _core thing_ that is different about low-level programming as opposed to high-level languages **that makes the terse cryptic names desirable in low-level but not high-level programming?**


Boundary conditions for testing

Ok so in a programming test I was given the following question. > Question 1 (1 mark) > > Spot the potential bug in this section of code:               void Class::Update( float dt )     {         totalTime += dt;         if( totalTime == 3.0f )         {             // Do state change             m_State++;         }     }      The multiple choice answers for this question were. > a) It has a constant floating point number where it should have a named > constant variable > > b) It may not change state with only an equality test > > c) You don't know what state you are changing to > > d) The class is named poorly I wrongly answered this with answer C. I eventually received feedback on the answers and the feedback for this question was > Correct answer is a. This is about understanding correct boundary conditions > for tests. The other answers are arguably valid points, but do not indicate > a potential bug in the code. My question here is, what does this have to do with boundary conditions? My understanding of boundary conditions is checking that a value is within a certain range, which isn't the case here. Upon looking over the question, in my opinion, B should be the correct answer when considering the accuracy issues of using floating point values.


Should we put the specification documents in source control system such as svn?

Today, One of my colleague and I have a debate about "Should we put the specification documents in source control system such as SVN?". In my opinion, It should be. Everything relate to developing project should be controled carefully with source control system. Is it a wrong concept in software development process?


Why don't interviewers  ask the applicant to read some code?

I had a dozen interviews in my life (I'm about to graduate) and I wonder why I was only once asked to read and explain some code. Roughly, 90% jobs are mostly about maintaining existing systems. IMO ability to read someone else's code is an important skill. Why don't interviewers check it?* *Among my friends I am the only one that was asked to review some code.


How to manage 2 DAO methods in a single transaction?

In an interview someone asked me : How do we manage 2 transactional/dao methods in a single transaction. Desired capabilities:   1. If anyone of them fails we need to rollback both methods.   2. Both of the methods can be called separately attached with a single transaction.   3. The management should be on DAO layer, not on service layer. I think : the question relates to spring transaction management.


Can I legally and ethically take an open-source project with community contributions to closed-source?

Let's say I start and develop some project under open-source license, and accept some community contributions. How shaky is the ground I stand on if I decide to take the project commercial and closed-source (or split license)? This question doesn't directly address the issue of a project with community contributions, which feels like different territory, at least as far as ethics are concerned. Legally, this might be iffy as well, because I'm not sure whether contributions fall under my copyright, or whether the contributor hold the copyright to the part of the project he added. Am I safe (ethically and legally) as long as I'm up front with the possibility that I may take the project commercial in the future?


Starting with Ruby on Rails? I see a lot of criticism everywhere. Is it okay to start with Rails now in 2012?

I have worked on C before and have never tried my hands on any web application creating framework. After some convincing from one of my friends I thought of giving Rails a go. Before starting to work on Rails I have just started learning Ruby a little and hence some curiosity to know what is being talked about the framework. In the last few days, I have seen a lot of criticism of Rails as a framework. Should I try my hands on Rails or should I try something else? Is python with Django the way to go? Is Nodejs the way to go? Is there some other ruby based framework which can work for me? Please suggest with an open mind. I want to be absolutely clear in my mind before I decide. [Note: The question was closed on Stackoverflow and I was suggested that the this is a better place]


Most appropriate OSS license for infrastructure code

I'm looking into potentially releasing some infrastructure code (related to automated builds and deployments) as OSS and I'm curious about how the various OSS licenses effect it. Specifically, LGPL prevents the code _itself_ (part/whole) being modified into a commercial product (which is what I'm after), but allows it to be "linked to" in the creation of commercial products (also ok). How does the "linked to" clause relate to infrastructure code, which is not deployed with the product itself? Would the application still be required to provide "appropriate legal notices" (which I'm not fussed over)? Would I be better off looking at the Eclipse Public License?


Is it possible to half-way synchronize javascript functions that include async calls?

I am wondering if there exists a way to half way synchronize javascript functions, where data is requested on the fly. I don't want to make it purely blocking (which I guess is impossible), I just want a nice way to write the overall algorithm in one piece. Let's take the following example:               function(){         command1;         var x1 = something;         async_function(x1, callback1);     }     function callback1(data){         var x2 = command2(data);         async_function2(x2, callback2);     }     function callback2(data1){         command3(data1);     }      I would like to write something like:               function(){         command1;         var x1 = something;         call(async_function, [x1], callback1);         var x2 = command2(data);         call(async_function2, [x2], callback2);         command3(data1);     }        * I tried playing around with the `caller` property of functions, but I'm not familiar enough with the execution environment.   * I also tried writing a function "call" like above, using the `apply` function to pass in a custom callback which returns the result of the async function to it's caller. What bugs me is that while programming\debugging, I have to follow the code from one function into another into another (just like the movie inception). I want one place to write the high-level algorithm without decomposing it into many functions.   For example, If I want to write A* algorithm, but the "getNeighbors" function is asynchronous.


What's special in July 26th and why is it used in examples for Expires header so often?

I've noticed that July 26th (my birthday) is used really often in various PHP examples related to preventing http caching using `Expires` header, like: http://stackoverflow.com/questions/12398714/cache-issue-with-private- networking-stream http://stackoverflow.com/questions/2833305/how-to-expire-page-in-php-when- user-logout http://expressionengine.com/archived_forums/viewthread/81945/ What's special in that date?


Why is mixing plural with singular and camel case with underscores in cake php naming convention better than a simpler convention?

I have been using cake php for over a year and generally I like it but I struggle to understand the advantages of the complex naming conventions over something simpler. Cake uses plural here and singular there (controllers vs models and reported controller names) for it's names and uses camel case and underscores too (method names and class variables vs database tables and view variables). As far as I can see picking either plural or singular and either underscores or camel case and using just one of each choice everywhere would still yield all of the advertised benefits but would be simpler to use for the programmer. What are the advantages of cake php naming convention which could not be achieved with the above system? Please demonstrate why a simpler system wouldn't work for any given assertion.


Caching factory design

I have a factory `class XFactory` that creates objects of `class X`. Instances of `X` are very large, so the main purpose of the factory is to cache them, as transparently to the client code as possible. Objects of `class X` are immutable, so the following code seems reasonable:               # module xfactory.py     import x     class XFactory:       _registry = {}            def get_x(self, arg1, arg2, use_cache = True):         if use_cache:           hash_id = hash((arg1, arg2))           if hash_id in _registry:             return _registry[hash_id]         obj = x.X(arg1, arg2)         _registry[hash_id] = obj         return obj          # module x.py     class X:       # ...      Is it a good pattern? (I know it's not the actual Factory Pattern.) Is there anything I should change? Now, I find that sometimes I want to cache `X` objects to disk. I'll use `pickle` for that purpose, and store as values in the `_registry` the filenames of the pickled objects instead of references to the objects. Of course, `_registry` itself would have to be stored persistently (perhaps in a pickle file of its own, in a text file, in a database, or simply by giving pickle files the filenames that contain `hash_id`). Except now the validity of the cached object depends not only on the parameters passed to `get_x()`, but also on the version of the code that created these objects. Strictly speaking, even a memory-cached object could become invalid if someone modifies `x.py` or any of its dependencies, and reloads it while the program is running. So far I ignored this danger since it seems unlikely for my application. But I certainly cannot ignore it when my objects are cached to persistent storage. What can I do? I suppose I could make the `hash_id` more robust by calculating hash of a tuple that contains arguments `arg1` and `arg2`, as well as the filename and last modified date for `x.py` and every module and data file that it (recursively) depends on. To help delete cache files that won't ever be useful again, I'd add to the `_registry` the unhashed representation of the modified dates for each record. But even this solution isn't 100% safe since theoretically someone might load a module dynamically, and I wouldn't know about it from statically analyzing the source code. If I go all out and assume every file in the project is a dependency, the mechanism will still break if some module grabs data from an external website, etc.). In addition, the frequency of changes in `x.py` and its dependencies is quite high, leading to heavy cache invalidation. Thus, I figured I might as well give up some safety, and only invalidate the cache only when there is an obvious mismatch. This means that `class X` would have a class-level cache validation identifier that should be changed whenever the developer believes a change happened that should invalidate the cache. (With multiple developers, a separate invalidation identifier is required for each.) This identifier is hashed along with `arg1` and `arg2` and becomes part of the hash keys stored in `_registry`. Since developers may forget to update the validation identifier or not realize that they invalidated existing cache, it would seem better to add another validation mechanism: `class X` can have a method that returns all the known "traits" of `X`. For instance, if `X` is a table, I might add the names of all the columns. The hash calculation will include the traits as well. I can write this code, but I am afraid that I'm missing something important; and I'm also wondering if perhaps there's a framework or package that can do all of this stuff already. Ideally, I'd like to combine in-memory and disk- based caching. EDIT: It may seem that my needs can be served well by a pool pattern. On further investigation, however, it’s not the case. I thought I'd list the differences:   1. Can an object be used by multiple clients?     * Pool: No, each object needs to be checked out and then checked in when no longer needed. The precise mechanism may be complicated.     * XFactory: Yes. Objects are immutable, and can be used by infinitely many clients at once. There’s never a need to create a second copy of the same object.   2. Does pool size need to be controlled?     * Pool: Often, yes. If so, the strategy to do so may be quite complicated.     * XFactory: No. An object must be delivered on demand to the client, and if an existing object is unsuitable, a new one needs to be created.   3. Are all objects freely substitutable?     * Pool: Yes, the objects are typically freely substitutable (or if not, it’s trivial to check which object the client needs).     * XFactory: Absolutely not, and it’s very hard to find out if a given object can service a given client request. It depends on whether an existing object is available that was created with (a) the same arguments and (b) the same version of the source code. Part (b) cannot be verified by XFactory, so it asks the client to help. The client fulfills this responsibility in two ways. First, the client may increment any of its several designated internal version counters (one per developer). This cannot happen in runtime, only a developer may change these counters when he believes that the source code change makes existing objects unusable. Second, a client will return some invariants about the objects that it needs, and XFactory will verify that these invariants are not violated before serving the object to the client. If any of these checks fail, XFactory will create and deliver a new object.    4. Does performance impact need careful analysis?     * Pool: Yes, in some cases a pool actually hurts performance if the overhead of object management is greater than the overhead of object creation/destruction.     * XFactory: No. The computation costs of the objects in question is known to be very high, and loading them from memory or from disk is without doubt superior than recalculating them from scratch.   5. When are objects destroyed?     * Pool: When the pool is shut down. Perhaps it might also destroy objects if told to (partially) release resources or if certain objects have not been used for a while.     * XFactory: Whenever an object was created with the version of the source code that is no longer current, as evidenced by either invariant violation or counter mismatch. The process of locating and destroying such objects at the right time is quite complicated. In addition, time-based invalidation of all objects may be implemented to reduce the accumulated risks of using invalid objects. Since XFactory is never certain that it is the sole owner of an object, such invalidation is best achieved by an additional “version counter” in the client objects, which is incremented programmatically on a periodic basis, rather than by a developer.   6. What special considerations exist for multithreaded environment?     * Pool: Has to avoid collisions in object checking out / checking in (don’t want to check out an object to two clients)     * XFactory: Has to avoid collision in object creation (don’t want to create two objects based on two identical requests)   7. What needs to be done if client does not release an object?     * Pool: It may want to make the object available to others after waiting for some time.     * XFactory: Not applicable. Clients do not notify XFactory about when they are done with the object.   8. Do objects need to be modified?     * Pool: May have to be reset to the default state before being reused.     * XFactory: No, the objects are immutable.   9. Are there any special considerations related to persistence of objects?     * Pool: Typically not. A pool is about saving the cost of object creation, so all the objects are kept in memory (reading from disk would defeat the purpose).     * XFactory: Yes, XFactory is about saving the cost of performing complex calculations, so storing pre-calculated objects on disk makes sense. As a result XFactory needs to deal with the typical problems of persistent storage; e.g. at initialization, it needs to connect to persistent storage, obtain from it the metadata about which objects are currently available there, and be ready to load them into memory if requested. And object may be in one of three states: “doesn’t exist”, “exists on disk”, “exists in memory”. While XFactory is running, the state may change only in one direction (to the right in this sequence). In summary, the pool's complexity is in items 1, 2, 4, 6, and possibly 5, 7, 8. The XFactory complexity is in items 3, 6, 9. The only overlap is item 6, and it’s really not the core function of either pool or XFactory, but rather a constraint on the design that is common to any pattern that needs to work in a multithreaded environment.


Why is studying an lisp interpreter in lisp so important?

I have seen many CS curriculums and learning suggestions for new programmers that call for the aspiring programmer to study a lisp interpreter that is specifically written in lisp. All these sites say things similar to, "its an intellectual revelation", "it is an enlightenment experience every serious programmer should have," or "it shows you hardware/software relationships," and other vague statements, particularly from this article taken from this reputable how-to. The general sentiment of my question is, how does lisp achieve the above goals and why lisp? Why not some other language? I am asking this because I just finished writing a scheme interpreter in scheme (taken from SICP http://mitpress.mit.edu/sicp/ ) and now I am writing a python interpreter in scheme and I am struggling to have this legendary epiphany that is supposed to come specifically from the former. I am looking for specific technical details between the two languages that I can exploit in their scheme interpreters to gain understanding about how programs work. More specifically: > Why is the study of an interpreter that is written in the language it > interprets so emphasized - is it merely a great mental exercise to keep the > original language and built language straight or are there specific problems > whose solutions can only be found in the nature of the original language? > > How do lisp interpreters demonstrate good architecture concepts for one's > future software design? > > What would I miss if I did this exercise in a different language like C++ or > Java? > > What is the _most used_ takeaway or "mental tool" from this exercise? ** ** I selected the answer I did because I _have_ noticed that I have gained from this exercise more skill in designing parsing tools in my head than any other single tool and I would like to find different methods of parsing that may work better for the scheme interpreter than the python interpreter.


Persist AJAX values

I have a simulator that pulls data from a DB - calculates and return JSON result to an ajax call that renders a table for the results. The calculation procedure are as follow:   1. grab X number of data that are group together and have weights to them.   2. use historical data and run a weight distribution algorithm on the group.   3. return as a JSON the metrics of each data, the old value, and the calculated new value 2 Questions: 1) Is Javascript calculation better or worse (or equal) to PHP calculation in order to yield the results. 2) IF Javascript calculation is faster or on par to PHP then I would assume the trip to call ajax is a small bottleneck. So one possibility would be to pre-load data and have it calculated on the fly. Now how would you persist that pre-loading efficiently? Would that just simply load everything in a simple var via ajax? Thus far it takes approx 55 seconds for the result to return 2000 entries on 14 days worth of data.


Unit test: How best to provide an XML input?

I need to write a unit test which validates the serialization of two attributes of an XML(size ~ 30 KB) file. What is the best way to provide an input for this test? Here are the options I have considered:   1. Add the file to the project and use a file reader   2. Pass the contents of the XML as a string   3. Create the XML through a program and pass it Which is my best option and why? If there is another way which you think is better, I would love to hear it.


Behavior-Driven Development / Use case diagram

Regarding growing of Behavior-Driven Development imposing acceptance testing, are use cases diagram useful or do they lead to an "over-documentation"? Indeed, acceptance tests representing specifications by example, as use cases promote despite of a more generic manner (since cases, not scenarios), aren't they too similar to treat them both at the time of a newly created project? From this link, one opinion is: > Another realization I had is that if you do UseCases and automated > AcceptanceTests you are essentially doubling your work. There is duplication > between the UseCases and the AcceptanceTests. I think there is a good case > to be made that UserStories + AcceptanceTests are more efficient way to work > when compared to UseCases + AcceptanceTests. What to think about?


Drawing a custom rendered control in Windows - resizing

Some background: in order to learn GUI programming and drawing in Windows I'm starting to create my own GUI toolkit in Windows (so this is a didactic exercise, please don't suggest "use Qt" or "use MFC"). I would like to draw the entire window area from memory DCs in double buffering (thus also handling mouse and keyboard events myself) but I'm now wondering how should I handle resize events from the window: suppose I have a text control and a sidebar and my window gets a resize command ![enter image description here](http://i.stack.imgur.com/YTwfs.png) the sidebar should grow larger and the text control should show more text (it is also larger). The first thing that comes to my mind is to redraw all the widgets on the memory DC and then BitBlt it to the screen. Anyway some widgets might not have changed at all or might be changed just a bit (the portion _"Text here, text here, te"_ hasn't changed at all during the resize). What can be done to exploit this fact and avoid redrawing (even in memory DC, not device DC) the parts which haven't changed?


c++ GUI application in linux without toolkit

We all know , there are number of toolkits available for GUI application in c++ for Linux. But for some reasons I want to create a GUI application without any toolkit.I know this question is ask earlier but there is no proper answer provided. So please tell the ways through which I can create GUI application on Ubuntu 12.04. Please do not suggest me toolkit, That's not an option for me.


Rewriting C# Formula Calculations in T-SQL

We have a 3-tier application with a C# client that connects to a C# web service via WCF and requests data from a SQL Server database. One feature in our application is a user-created form app in which our customers setup forms that have fields. The fields refer to values that can be imported, entered by the user, or calculated. The calculations can be thought of as Operation / Value / Timeframe. Each value referenced in a calculation might also be a calculated value. (Care is taken to ensure circular calculations do not exist.) For example, a user might setup a field that shows Cash Over / Short that subtracts the value Total to Account For and Total Accounted For. These fields might in turn add up sales, credit card payments, cash totals, etc. Changing Grocery Sales in the form UI would trigger a change that recalculates the Total to Account For, and that field's change triggers the Cash Over / Short field to recalculate itself. Complicating the calculations are historical values. Some calculations might be phrased in English like "Subtract Today's Totalizer Value from Yesterday's Totalizer Value". Or, someone might simply set a Last Year Sales field to the value of Total Sales exactly 365 days ago. All this calculation is done in the C# user interface. The C# code builds dependency trees and sets up event handlers when values change. Changing a value triggers an event, and calculations that depend on the changed value are recalculated. We also have a recalculation engine that verifies the totals are correct before making permanent changes (i.e. before drafting someone's account, so the amounts better be correct). This recalculation engine uses the same C# calculation engine across multiple threads. This incurs penalties of reading from the database, creating the C# objects, performing calculations, and writing to the database. The timeframe dependencies mean we really need to calculate in some kind of day order. I could use some kind of cursor to move day-by-day, gather up all the data on each day, figure out the dependencies, join to historical values, but... **My question: how would you approach performing complex user-defined calculations in a set-based manner in SQL?** I don't see how to do this without RBAR operations on each calculated value.


Multithreaded Pre/Post Functions

I'm programming an application for an embedded device. We are using an RTOS that supports multi threading. The device is supposed to mimic an older project that was programmed in plain C (without threads). The original project uses a stack and START / END functions at the beginning and end of each function that can be called throughout runtime. Each time START is called a constant number (identifying the function) will be put on the stack - END will remove it from the stack and check if the top is equal to the called function. If it is different something really bad has happened and the device will recover/reset. This feature is great for debugging purposes as it fully displays how functions are called. It would be very lovely to have such a feature in the multi-threaded version. I implemented START and END as a class in C++ using constructor and destructor which makes the handling fairly easy. Running this single-threaded works like a charm. Anything multithreaded will destroy the stack as the scheduler switches context and writing to one stack would not make sense. The most basic solution in my mind would be having one START/END stack for each thread -> Problem each function has to know which threads it runs on. Is there a better solution to this problem?


How to make script with faster queries?

# I have project to share useful urls with some information about the url. ## the project using:   1. add new post ( opening bootstrap with textarea )    2. user add content (url , hashtags, text)    3. the js fetch urls   4. the js fetch hashtags   5. the js send 1st url to php curl   6. php return title, description, images, site-name by tags title and meta-tag and    7. user select Image and share the post I did all that above, but there is some errors and new steps wanna ask about ## Errors ### On fetching url content   * some sites didn't have good data to fetch it ( title, description, images )   * PHP Curl fetching take long time ## The help:   * how can get the right data from any url   * how can make the Curl faster   * is there are any php class make this faster and secure  # new steps I wanna make ### 1 - I wanna display posts like as Twitter time line   * ( unread new posts button with #no of posts ) real time on click append new posts   * ( load older posts on scroll down )   * ( make it fast ) # Suggests load queries result to json file and update it by time will be faster ? if users posting the same url get it from db with out fetching ? will need to save the fetching on db ? i've insert all post as a json array on db table just the site name not url on other column? is that's good ![enter image description here](http://i.stack.imgur.com/nxlA3.png)


Do I need to use Node.js, Java or both for a multi-platform application?

I'm new on Java and I have experience building Node.js apps, so I'm thinking to create a program that it's kind of "multi-platform" where there's going to be a tablet kindle that's going to run the Java app, a PC that will show queues made from this java app in many tablets and another PC that's going to manage the data on the application (those could be web application from Node.js). So, Node.js could be the server managing queries and database while Java could be on Kindle Fire app making requests to the server, I really don't know Java too much but by the time I don't know if this project structure is fine, maybe it could work better using both sides on Java or Node.js web-app. Let's say this project is for a restaurant, waiters are going to have kindle fire (java app) instead of books, on a side there's going to be a big screen showing queues (this could be shown from Node.js) made from waiters on kindles, and the entrance there's going to be a PC that's going to manage the money and other stuff (maybe Node.js as well).


Passing the HR Screening Process

Just a quick question here. I already have a Bachelor's degree in Statistics, and I am thinking of getting the Graduate Certificate in Software Engineering offered by Harvard Extension. Assuming that I hold both BS in Statistics and Graduate Certificate in Software Engineering, Would it be possible me to pass the HR screening process if I apply for a programming job later on? (I am asking this because many programming analyst jobs seem to require a degree or diploma in Computer Science or Computer Engineering) Thanks,


User roles in GWT applications

I'm wondering if you could suggest me any way to implement "user roles" in GWT applications. I would like to implement a GWT application where users log in and are assigned "roles". Based on their role, they would be able to see and use different application areas. Here are two possible solution I thought: 1) A possible solution could be to make an RPC call to the server during onModuleLoad. This RPC call would generate the necessary Widgets and/or place them on a panel and then return this panel to the client end. 2) Another possible solution could be to make an RPC call on login retrieving from server users roles and inspecting them to see what the user can do.   * I'm also considering java security frameworks like Apache Shiro and Spring Security... What do you think about them? What do you think about? Thank you very much in advance for your help!


FXML Boiler Plate Code

Is there a way to address a typical boiler plate code to instantiate and load FXML? A typical version:               public void start(Stage primaryStage)      {            URL fxmlUrl = this.getClass().getResource( /* your string path*/ );              FXMLLoader loader = new FXMLLoader(fxmlUrl);              try          {                primaryStage = loader.load();             this.controller = loader.getController();         }          catch(IOException ioe)         {             ioe.printStackTrace();         }              primaryStage.show();     }      Note for the code above, the backing FXML file has `<Stage></Stage>` as root. This is just my preference. The loading and matching up to the FXML root can adjust accordingly. In any case, the loading of the FXML file and obtaining reference to a controller is the crucial part (for most of the time). I've attempted to create a utility method such the one below to help prevent this boiler plate code:               <R, C> void loadFXML(R root, C controller, String path)     {         URL fxmlUrl = this.getClass().getResource( path );              FXMLLoader loader = new FXMLLoader(fxmlUrl);              try          {                root = loader.load();             controller = loader.getController();         }          catch(IOException ioe)         {             ioe.printStackTrace();         }     }      However, at some stage there was null pointer errors, or the loading was incorrect. I believe it has something to do with the Generics and/or casting. How can the this code be imporoved? Or perhap can another type code be suggested to solve this problem?


Is there a process-oriented IDE?

My problem is simple : when I'm programming in an OO paradigm, I'm often having part of a main business process divided in many classes. Which means, if I want to examine the whole functional chain that leads to the output, for debugging or for optimization research, it can be a bit painful. So I was wondering : is there an IDE that let you put a "process tag" on functions coming from different objects, and give you a view of all those functions having the same tag ? **edit** : To give an example (that I'm making up completely, sorry if it doesn't sound very realistic). Let's say we have the following business process for a HR application : receive a holiday-request by an employee, check the validity of the request, then give an alert to his boss ("one of those lazy programmer wants another day off"); at the same time, let's say the boss will want to have a table of all employee's timetable during the time the employee wants his vacations; then handle the answer of the boss, send a nice little mail to the employee ("No way, lazy bones"). Even if we get rid of everything not purely business-related (mail sending process, db handling to get the useful info, GUI functionalities, and so on), we still have something that doesn't really fit in "one class". I'd like to have an IDE that would give me the opportunity to embrace quickly, at the very least :   * The function handling the validation of the request by the employee;   * The function preparing the "timetable" for the boss;   * The function handling the validation of the request by the boss; I wouldn't put all those functions in the same class (but perhaps that's my mistake ?). This is where my dreamed IDE could be helpful.


Mono is frequently used to say "Yes, .NET is cross-platform". How valid is that claim?

In What would you choose for your project between .NET and Java at this point in time? I say that I would consider the "Will you always deploy to Windows?" the single most important (EDIT: technical) decision to make up front in a new web project, and if the answer is "no", I would recommend Java instead of .NET. A very common counter-argument is that "If we ever want to run on Linux/OS X/Whatever, we'll just run Mono", which is a very compelling argument on the surface, but I don't agree for several reasons.   * OpenJDK and all the vendor supplied JVM's have passed the official Sun TCK ensuring things work correctly. I am not aware of Mono passing a Microsoft TCK.   * Mono trails the .NET releases. What .NET-level is currently fully supported?   * Does all GUI elements (WinForms?) work correctly in Mono?   * Businesses may not want to depend on Open Source frameworks as the official plan B. I am aware that with the new governance of Java by Oracle, the future is unsafe, but e.g. IBM provides JDK's for many platforms, including Linux. They are just not open sourced. So, under which circumstances is Mono a valid business strategy for .NET- applications? * * * Edit: Mark H summarized it as: "If the claim is that _"I have a windows application written in .NET, it should run on mono"_ , then not, it's not a valid claim - but Mono has made efforts to make porting such applications simpler.".


What is idiomatic?

I understand an "idiom" to be a common operation or pattern that in a particular language is not simplified by core language syntax, such as integer increment:               i = i + 1;      In C++, this idiom is simplified by an operator:               ++i;      However, when someone uses the term "idiomatic", I am not sure how to understand it. What makes a piece of code "idiomatic"?


SQL injection attacks, how do I test and secure coldfusion queiries?

I'm running Coldfusion 8 and SQL server 2008. I've been building serveral forms that insert data into the database from external users, we have a custom built security module built by the guy who I've taken his job. 1) How can we test our HTML forms to ensure that we're protected from SQL injection attacks? 2) How do I secure CFqueries in CFC's? 3) What are some best practices in terms of SQL & Coldfusion for security? \-- A lot I know!


Efficient algorithm to sort html list of players

We have list of players, which is represented in form like this:

Player1

1

Player2

2

Player3

3

Player4

4

       Something happens, and Player 4 recieves 600 points. So it goes on 1st place. With him - player3 recieves 300 points and goes on 2nd place. It happens after same event - let's assume, that they get these points from server, but that's all load that is allowed on server and server does not calculate positions. What is most efficent way to get view like this:

Player4

1

Player3

2

Player1

3

Player2

4

       It's not question about code, it's question about:   * What kind of task it is? Just sort?    * Is it ok to use JavaScript to solve this task if lists are less than 500 rows?    * Is it ok to re-render whole players list, or there are some ways to just change div element position(index in DOM or something like this)?    * Are there any tools and libraries around, that can help solve this task efficiently?  P.S.: If we want to animate all this story - we should create a queue of animation tasks each time scores for all players recalculates? **Update:** I have followed @Jalayn's answer, and stuck on following problem: Let's assume that list `[1,2,3,4]` transforms to `[2,3,4,1]`. If we map it, we will get following position diffs `[+1, +1, +1, -3]`.. Now, if we move one by one elements to it's position change, all will be ok, till we get to negotiate position diff - 1st position diff can't move down any more.. So, my update to question - how we can animate rows more than twice? **And final problem, on which I am stucked:** Not sure, if it most be solved in this question. BTW, if 2 players have same points - animation plan brakes and element_ids duplicates. My question now is how to make following transformation of structures list: `[(#id1, 1, 100),(#id2, 2, 50), (#id3, 2, 50), (#id4, 4, 25)] => [(#id3, 1, 150), (#id1, 2, 100), (#id4, 3, 75), (#id2, 4, 50)]` For now my solution is to refresh whole list, but not completly regenerate it - just move all elements to the first position in order they sorted in backed JSON. Animation is too complex to be done cause of final problem.


Delphi vs C# for GUI programming

I'm coming from PHP and Python background with little knowledge of C, I have done many web based application now I'm thinking of Desktop application for windows platform. A friend told me to go for Delphi and others are saying C# is the best, well, what I'm looking for is   1. Simplicity   2. Productivity   3. Good API documentation   4. Speed   5. Drag and Drop   6. Multi threading & Good Network API Thanks


What kind of positions could a beginning developer expect to qualify for in five years?

I am a Junior developer, just landing in a job after completing my Masters. I'm not clear on what positions or roles I could possibly be in after five years? So My Question is, What kind of positions can an Entry level programmer like me seek in 5 years?


java application architecture

We have to write an administration panel for many customers. But we want to have just one administration panel, and use it in various projects. This admin panel will have basic components such as access control logic, maker-checker system in changes, user logging and etc. It will also have reporting for the customer, logs of transaction of the customer (which can vary according to the industry such as mobile banking, banking, ticket sales). These components may have to be modified according to the business. So we are thinking about the architecture here, Is it OK to use jars of every basic components, and bring them together on a glue application? Or should we build each component as WARs and make interfaces between them? If there are any more ideas, it will be appreciated.


Where does the word "Programming" come from? I'm referring to the word "program" as referring to "to instruct a computer". Programming, programmer etc...


What is a zombie process or thread?

What is a zombie process or thread, and what creates them? Do I just kill them, or can I do something to get diagnostics about how they died?