New answers tagged

2

Stylistically, the for loop is more readable. As a general rule, if you start the loop at 1, then I would prefer the termination condition to use <=; if you start counting from 0, then < would be better. The design of this function is conceptually flawed. It will return an unused filename, but presumably you will eventually want to create a file ...


1

It appears that the string you pass into the method is a type of picture not really a filename, since the filename needs an index attached. I'm not a big fan of either approach. I think using GetFiles and using the Length property is easier to see what's happening. Something like this should work: using System; using System.IO; private static string ...


0

Correctness issues Correctness merits priority over efficiency. Important to get a correct answer, not a fast and wrong one. Poor use of mixing types long interval, and int i lead to undefined behavior (UB) when interval > INT_MAX. Use same type. Small range long may use 32-bit. Easy enough to run in less than 1 minute and exceed 231 - limiting ...


5

I'd still say that you should break apart the mail message and SMTP client into separate classes for the "Single responsibility principle". In specific: A class should only have a single responsibility, that is, only changes to one part of the software's specification should be able to affect the specification of the class. This is usually where you have ...


2

Can you process code like that in a single glance? That's my really my point. Yes, and it is in fact a way of expressing code that I miss in other languages, where the equivalent code feels needlessly verbose to me. This is the same for ternary operators when you are accustomized to if-expressions (as opposed to if-statements). And just like ternary ...


5

Throwing custom exception I find the second version is cleaner as it clearly communicates which condition caused the exception. With the first one, you need to know it's about Single which you need to read the documentation for. Implemention However, as far as the implementation logic is concerned this is doing a lot of querying and the source baseFilters ...


2

Here are some suggestions. This answers uses the Range-v3 library and assumes #include <range/v3/all.hpp> namespace view = ranges::view; Please include the #includes and supply a small test program in the future. You probably have written them anyway, so why not post them to save reviewers' time? :) int may be too small for indexes. Consider ...


6

You are asking: which version would you favour and why? Instead of answering directly to your question, I will try to show why that formulation is not, at least in my opinion, “a relic of former days”, but quite idiomatic for Common Lisp (and other “terse” languages). My attempt is done by first recalling an important concept of the language, and then by ...


6

The aspect of the code that may cause difficulty to those new to Lisp appears to be the fact that in Common Lisp, nil has two meanings. It's the value of an empty list () and it's also false when used in boolean context. So when we have something like (or '(1 2 3) ()) the first non-NIL list is returned. When we use and it returns the last item if all ...


4

If you are not going to handle each exception differently just use a multicatch: public static void main(String[] args) { try (FileReader file = new FileReader("Doc.csv")) { Scanner sc = new Scanner(file); while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } } catch (IllegalStateException | ...


1

When you ask if your first solution is faster, if you are talking about theory it isn't faster. To understand why you need to understand the the difference between big O complexity and actual run time. For the purposes of this question, it is important only that you realize that big O complexity does not guarantee that a piece of code with O(n^2) run time ...


3

Takning all considerations by VisualMelon and Pieter Witvoet into account a solution could now be: static public IEnumerable<T> SkipLast<T>(this IEnumerable<T> data, int count) { if (data == null) throw new ArgumentNullException(nameof(data)); if (count <= 0) return data; if (data is ICollection<T> collection) return ...


4

There are 2 cases for which both your and VisualMelon's implementation can be improved: If count is less than or equal to 0, then you can return source directly. This speeds up iteration by removing an unnecessary intermediate step. For this to work, the yielding part of the method has to be moved into another method, but that's easy with local functions. ...


3

if (data == null || count < 0) yield break; This behaviour is somewhat consistent with Take, but not with Skip: Skip treats a negative values as a zero. As, indeed, does the SkipLast which doesn't appear in .NET Framework. It should throw on a null argument with an ArgumentNullException. My only other real issue with the methods is that neither will ...


1

require style I (and most of the Node developers I've worked with) prefer the style where require is put on top. As stated in the guide you linked, it gives a clearer overview of what are dependencies of the module/file you're writing. No one likes to see that if some condition is successful, then a new module will be loaded. It's just unnatural. On the ...


1

main C17::6.11.6: Function declarators The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature. The new standards removed the implicit int rule (I don't know if it was C11 or C99). Use int main(void).  bug CiaPan found a bug. Here are three proposals to fix it. for ...


1

Pythonic, readable and convenient are all subjective and open to debate, even though "Pythonic" is less so than the others. That said, a simplified version may be illustrative. Comparing @foo @bar def baz(value: T) -> T: return value to def baz(value: T) -> FooT: return foo(bar(value)) there are two issues with the former: The final ...


13

Suggestions on your code: Your code currently returns the input string unmodified if it contains nothing but spaces and tabs. I'd say it should return an empty string. You always use input.at(x) instead of input[x]. The former checks at runtime for out of range errors and in which case throws an exception, while the latter does not. Your code ensures ...


1

Peilonrayz was so kind to show and explain some of the steps he has taken in the optimization process for me to better follow along in chat. I wanted to preserve them if the chat room ever goes to die. The following code pieces are supposed to be used with the Luhn class as presented in the question or his answer to be able to access the look-up tables. ...


2

As your code stands, I would opt for OPTION 2: USING ENUMS. Consider inheritance only when the derived classes have specific state/operations that are not compatible with other derived classes. Don't abuse inheritance when a simple property (in this case of an enum type) allows to distinguish a certain feature amongst instances of a given type. Example ...


0

This was difficult to review because so many pieces were missing and had to be inferred. For that reason, you may find some of these comments off the mark because they are necessarily based on incomplete information. With that said, here are some thoughts that may help you improve your program. Be clear about what's allowed While your description said ...


Top 50 recent answers are included