# Tag Info

## Hot answers tagged library

23

Your mathematical criterion for equality has some issues. One important point to consider is that your "equality" is not transitive. In your code, you use a threshold of $\epsilon$ ($\epsilon = 5\times10^{-16}$ in your code, when the values have type double); you declare $x$ and $y$ to be equal to each other if $|x-y|\leq \epsilon$. Now suppose ...

22

IOutput class module (Interface) Looking at how the interface is being used: this.Parent.OutStream.PrintLine output Where output is clearly a String, which makes sense. But the interface's signature doesn't reflect that, and is confusing: Public Sub PrintLine(Optional ByVal object As Variant) Why is the parameter optional? and why is it a Variant? ......

21

... as I went through others, there is always this voice in head, which says: "Oh, this is a lot of code and functionality. Is that really necessary or would it slow down the performance?" This is a reasonable consideration, but premature optimisation is evil for a reason. It might be a good idea to try to roll out your own solution, but it ...

20

Compared to Python's NumPy, my matrix multiplication is quite a bit slower. Can it be made faster without using sophisticated matrix multiplication algorithms like Strassen's? One issue is that you use a vector of vectors - your memory accesses aren't going to be as contiguous. Typically one represents a matrix as a single really long vector, and then ...

17

template <typename Less, typename T, typename... Ts> constexpr const T& min(Less less, const T& a, const T& b, const Ts&... rems) { This function requires a minimum of 2 elements. A minimum element will exist if the user provides a single argument variadic list. Consider handling that. auto& min1 = min(std::less<>{}, 4, 5); ...

16

Use use of a "state" to distinguish between different output formats is very wrong. If you need different formats, use multiple output methods (have toString just for debug output), or even better, write a separate class for converting Points to a string representation. I think you have overdone it with the JavaDoc. IMHO it's neither the place for ...

16

Let me collect a couple of thoughts here. Aggregate initialization currently works like this: array2d<int, 2, 2> a{{1, 2, 3, 4}}; but wouldn't it be favorable to allow for array2d<int, 2, 2> a{{1, 2}, {3, 4}}; std::array::at performs bound checking and throws upon an out of bounds index. When your intention is to stick with the std::array ...

15

Unless you wish to optimize the code, with specialized swappers for various hosts orders, you are doing it wrong. I invite you to check The Byte Order Fallacy by Rob Pike. The punch line: the byte order of the computer you are executing the code on doesn't matter, because the language abstracts it for you. Thus, only the byte order of the network matters, ...

14

I'll take a swing at this: 2) Unified function name based on overloads: ... Also it would be nice to end up with a universal set of functions that select the most efficient method based on input parameter without having a suffix to select between them: ... I am pretty sure that's not completely achievable due to ambiguity. The question is what ...

13

What you did well As far as I can tell, the code produces correct output when given correct input. The functions feel like a coherent library, with consistent function names and logical order of parameters. You use a caller-owns-everything memory management policy, which works well for C. What you could improve on The name json_initJsonParser() is a bit ...

13

1. Why static ? I have a minor issue with the following: private static void initPolar(Point point) { Why do you define a static function when the argument is an instance of a Point and thus could be a method? private void initPolar() { double angle = getAngleRadians(); x = getRadius() * cos(angle); y = getRadius() * sin(angle); } 2. Do ...

13

I don't like the idea of using an all-in-one struct. You will have to do more maintenance: every time you create a function that returns different values, you have to create a new struct. Further, I think the struct return value will confuse the user. Everyone is familiar with the basic types, so they will immediately know how to use a function just from it'...

13

Is it appropriate to require the user to specify argument names with *? Yes! I think this is excellent practice, and in my opinion a far underused feature of Python 3. It greatly enhances the readability of your function when called. Reading just the invocation of play_game() in your simple example, I can immediately work out what all of the arguments mean....

12

Look what I found Dim procedureName As String, lastFound As String Dim procedureBody As String Personally I don't like declaring variables like this, and almost every language allows you to do this in some way or another. I think this is one of those holy war issues though, some programmers like doing this and some programmers say this is bad practice. ...

12

This looks nice! Two issues I see here, When compiling your template with clang, it refuses the if constexpr dispatch for every recursive instantiation with sizeof...(rems) > 1, e.g. error: constexpr if condition evaluates to 2, which cannot be narrowed to type bool [-Wc++11-narrowing] gcc seems to accept this, but the fix is quite simple, just be ...

11

I think the code looks pretty tidy. Not sure if you can get a huge speed up - you are effectively tokenizing a string by storing the meta data of the tokens (start/end positions and type). This alleviates the pain of having to copy the data and dealing with the associated memory allocation issue. A few remarks: You could consider changing the token to ...

11

Design I think you need to break that class in two. Having instance members on a static class is pretty confusing and bug-prone. I'd suggest Enumerable to be the static class, with these members (notice source being preferred over collectionObject, and explicit ByRef modifiers and Variant types): Function Contains(ByRef source As Variant, ByVal value As ...

11

Headers and namespaces The function is missing a definition of map. This is probably what you want: #include <map> using std::map; Similarly, the main() needs: #include <iostream> using std::cout; using std::endl; That said, there's no good reason for std::endl in this code: an ordinary newline is sufficient, and we don't need the extra ...

11

Use Requests urllib is kind of a pain to use. Use Requests instead. Among other things, all of this code: for key in self.keys: request += "&" + key + "='" + self.keys[key] + "'" will go away. Requests has query string formatting from a dictionary. Julian conversion It's doubtful that you need to be doing this ...

10

The Math.atan2 function is defined rather strangely (not only in Java, but in every programming language I know) and the correct use is atan2(y, x).

10

I'm glad to see I'm not the only one who finds "interfaces" in JavaScript useful. Yes, the language doesn't support interfaces, but there is value in defining them anyway if you expect third party code to integrate with it. Interfaces in JavaScript There is no official vehicle for defining interfaces in JavaScript. That being said, when someone sees a ...

10

public S isIn(Range range) This method looks strange to me, because of 2 points It is violating SRP because it is checking if parameter is in range and composing the exception message The composing of the exception message. So let us take a look at message composition, which we can extract to a separate method to solve point one also. private ...

10

I like that the library provides syntactic sugar to make code easy to read in English. Chaining is a nice design. IllegalArgumentException is a non-final class, and by subclassing it, you could probably do better than encoding the validation errors into the message string. I would hope that StringPreconditions.isNotEmpty() also checks that the string is ...

10

Standard doesn't specify the signedness of char. The line swap_table[(int) table[i]] = deletechars[i]; fails on a default-signed-char architecture (table[i] is promoted to negative value). Make it unsigned char * table. Functions which should not be called directly should not be exposed to the client. Make them static. translate_no_table Counting found ...

10

Do you really need a new implementation for floating-point numbers comparison? java.math.BigDecimal may be useful for FP numbers comparison with its compareTo(arg) method. areEqual(args...) overloading seems too repetitive. You may consider redefining them using generics in a single method. An example with minor changes in the original code: public static &...

10

It would be nice to be able to pass an IComparer<TKey> instead of having to make sure TKey implements IComparable<>. It could default to Comparer<TKey>.Default. This provides you with a lot more flexibility in terms of which types can be used as keys. Get can return null . So if you try to access a non-existing key you will get a ...

10

for loops in C++ In C++ you can declare the loop variable in the statement instead, like this: for (long a = 0; a < cols; ++a) { res.M[0][0] += M[0][a] * right.M[a][0]; } This is a very nice feature, because the variable a in this example only exists in the scope of the loop, so it cannot be accidentally misused outside of the loop. So if you only ...

9

My main comment is that you need to concentrate on avoiding duplication of code and on writing smaller functions. 50 lines or so is the sort of max length I use. You also use long names that are often too long giving the code a very dense appearance and making it difficult ot read. Smaller functions, reduced variable scope and hence shorter name sizes ...

9

You should just include <cstddef> in order to use std::size_t. Including <cstring> seems unnecessary if you're not actually going to utilize anything else. Since this is C++11, you can now use strongly-typed enums over plain ones. One difference is that plain enums are implicitly cast to int, whereas strongly-typed ones must be explicitly cast ...

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