35

Is there a use-case for build-in currency formatting? Basically, with currencies you have two ways of working: in a currency-aware environment, where people register amount sometimes in local and sometimes in foreign currency: you will never use the default built-in feature. Instead you’ll store a currency amount and a currency code. in a currency-neutral ...


21

It depends on what you mean by "OK". In the languages that I'm familiar with that have the capability to do so, a null/nil/undefined value is falsy. That means that it would be interpreted the same as false when used in a Boolean operation like described in the examples. However, I cannot say for absolute certainty that this will apply to all languages for ...


13

You are absolutely right, formatting using a system-dependent currency symbol is dangerous. I actually knew people who lost lots of money through that. Especially with US dollar and Euro being close enough that the numbers make sense. On iOS you typically use a currency code, and the currency code is displayed in a system dependent way. For example, if the ...


11

The question seem to be "why programming languages, frameworks and operating systems support features that are not the best practices for professional developers in large multinational corporations"... and the answer is there are sometimes developers who are not in that category. If your language only supports enterprise-level features you will not ...


9

In C++ and Java and many other OOP languages, in order to send a message to an object, you have to use the object.function() syntax […] First, an important correction: they use the syntax object.message(), not object.function(). Messages and functions are fundamentally different. But why was this syntax chosen to send a message? why for example wasn't the ...


8

There are a lot of long answers to a simple question here. You ask for a use-case and there's a simple one that I don't think has been mentioned yet: games. If a game involving money is set in an ambiguous location, why not use the players local currency? It creates a more immersive experience and, as the feature is built into the language, costs very little ...


8

It's still O(n^2), with a constant coefficient of 1/2. The number of times the inner loop is executed is (i-1) + (i-2) + (i-3) ... + 3 + 2 + 1 with the total number of terms being i. Note that the first and last term add up to i, as do the second and second-to-last, etc. So there are i/2 pairs, each of which adds up to i - which makes a total of i/2 * i = 1/...


7

In Ruby, nil and false are falsey, and they are the only falsey values. Every other value is truthy, this includes true (obviously), but also values that some other languages might not consider truthy such as 0, 0.0, "", [], {}, and so on. There is also a naming convention in Ruby that methods whose primary purpose is to answer a Yes/No question are named ...


7

Thirty years ago or more it was probably still reasonable to assume that most computer systems that dealt with financial amounts, did so exclusively in the local currency. In the English-speaking world and the advanced economies more generally whom computers were built to serve, the local currencies had never changed during the computer era, and many could ...


6

The best is to write the actual code in C or C++, and then to just create interfaces for any other languages so that for example a python developer can use the library in their python code without ever knowing the actual language. Writing the implementation in multiple languages is a waste of time. It increases the number of bugs. With cryptography it is ...


6

There's no convention for this when it comes to pull requests; I suggest to stick to whatever the project is currently using. It's consistency that brings clarity in cases like this. Some languages support generating documentation from comments, and it's often possible to link to another method in that documentation. But how depends on the programming ...


5

If it is an argument to request something, but the caller is not sure to get it as requested, you can use the term desired page size. Desired makes clear that it's not guaranteed. But you can be more specific in your case with desired minimum page size. This conveys the fact that the server may choose a smaller minimum, but that the page can anyway be ...


5

TL;DR Currency formatting has been an OS-level configuration for decades now, and the pre-internet days were a very different beast in terms of the frequency of international transactions and the need for someone in region A to express money using region B's currency. I suspect the OS-centric currency settings are a relic of the past, kept in either because ...


5

Index-transformations are a simple but tedious thing. Write the code with "natural" indexing for the algorithm. Write down the transformation rule for the individual indices to get "natural" indexing for the language. Substitute in the new indices. Simplify the resulting mess. Optionally, rename the new variables to the old pre-...


5

It's still trivial. Everywhere you have a(something) you change it to a(something+1). Leave n alone. Even n before is still even n after, but it affects an odd array element, because you changed a(n) to a(n+1). You can then simplify the mathematical expressions if you wish. It's the simplification that is non-trivial.


5

Most services require the user to accept the terms of service in order to create an account. If you keep track of when the account was created and a revision history for the terms of service, you will be able to associate the account with the terms of service as they were when the account was created. If you also maintain a history of login times along with ...


4

I don't know what Jim Perry meant, my mindreading capabilities are very restricted. Actually, I disagree to his literal statement: even in a hypothetic "more realistic context" an expression like if (<very complex boolean expression>) return true; else return false; does not look simpler to me than return <very ...


4

I understand that OP is looking for a canonical answer to the following question: Why is converting 0-indexed code to 1-indexed code non-trivial? But there can be no answer to this question (canonical or otherwise) because, as has already been noted in some other answers, the conversion process is trivial. I will provide a particularly simple description ...


4

The second approach is the way to go. It has the significant advantage of separation of concerns: User is related to the identity of the user. One step more would be to separate identity (name, address) from the authentication (password), which are related but different concerns. UserProfile is related to the activity of the user. I'd, by the way rename ...


4

400, 401, 403, 404, something else? If the server refuses to apply the request, and wants to call attention specifically to the request's Authorization headers, then the appropriate status code is 401. 403 is similar, but less specific - once again, it indicates that the server understood the request, but won't apply it; the difference is that this status ...


3

Is it a good idea to implement the logic in one (Turing-complete) language, then call functions from all other languages? Or do I implement from scratch for each language? Generally yes. This is the usual way to do such thing. Implement all functions and algorithms in a language you choose for a core library only once (e.g. c or c++). The interfacing with ...


3

This was likely a design decision by the original authors of some of those older languages. As others have pointed out in comments other syntaxes exist in languages that just didn't catch on. C and C++ caught on, and their challenges inspired a new generation of language authors. The newer generations were comfortable with the syntax, so they copied into ...


3

There is a third option you haven't considered. The entities are directly linked to the user. E.g. every achievement is in fact an object with a field userID containing the ID of the user the achievement belongs to. Achievement { id kind ... many more fields ... userID } For achievements that makes much more sense than storing them as static ...


3

Regardless of any legal considerations: you might need to track acceptance for new users you will need to ask re-acceptance of changed terms and conditions for existing users. you may have to ask for re-acceptance after a given time. Moreover you may need to show evidence that the user subscribed to the terms. A simple flag is not sufficient in case of ...


2

Is it OK to have a function that returns true/nothing instead of true/false? No. Statically Typed Languages bool isFoo( int a, int b ) { if ( a == b ) { return true; } } // Evaluated isFoo(2, 2) // true isFoo(2, 4) // true isFoo(2, 4) is undefined behaviour. It will most likely return true because some value in memory is thrown, and as long as it ...


2

There are three parts that must be considered: semantic, mechanic, and dialect. Lets start with the easiest, mechanic or what works. If your language does not allow you to return nothing, you should return false. If your language does not define false you probably should return nothing, but now we are moving into dialect. Remember code has two roles to ...


2

Scan through both lists in lockstep Given two sorted lists, you can find their intersection by scanning through the lists, increasing the index of the lesser item. It looks something like this: var i = 0, j = 0; var intersection = []; while (i < a.length && j < b.length) { if (a[i] == b[j]) { intersection.push(a[i]); i += ...


2

He is referring to code where the reader looses context such as the if statement covering twenty lines, or in a language where logic operators operate by success (have an object)/failure (have an error) necessitating a conversion to a boolean type.


2

Get a book about refactoring. First step: You replace an array with indices from 0 to n-1 with an array with indices from 1 to n, and add 1 to every array index. Second step: If you have a loop where an index used in this array runs in the range k1 ≤ index < k2, you change the range to k1 + 1 ≤ index < k2 + 1, and subtract 1 from every use of index. ...


1

One more aspect: In many IDEs, it's easier to put an unconditional breakpoint on some source line than a conditional one, making the second version more debugging-friendly. If you e.g want to debug the cases where the function returns false, it's straightforward to set a breakpoint with the second approach (on the return false line), but quite tricky with ...


Only top voted, non community-wiki answers of a minimum length are eligible