Tag Info

New answers tagged

0

they do not recommend that one use the using namespace ornamespace:function` - if I did not misinterpret it. You did. The disrecommendation only applies to the using namespace directive (which is commonly referred to as abusing namespace, not entirely humorously). It is strongly preferred that you use the fully qualified name of a function or object, ...


4

This is because: 1) it defeats the entire purpose of namespaces, which is to reduce name collision; 2) it makes available to the global namespace the entire namespace specified with the using directive. For example, if you include and define your own max() function, it will collide with std::max(). http://en.cppreference.com/w/cpp/algorithm/max The ...


1

This is somewhat a language specific answer, but no language is stated in the question. The book "Dive Into Python" suggests implementing comments using Verbose Regular Expressions: Python allows you to do this with something called verbose regular expressions. A verbose regular expression is different from a compact regular expression in two ways: ...


4

They are not equivalent in their execution order. foo("hi", bar("bye", function() { // do something cool })); This must first evaluate bar("bye", function() { … }), then calls foo("hi", …) with the result of that invocation. As the return value of bar is undefined, you'll get an error when you call that as a function. If you want the callback example ...


0

To me in practice this question translates to 'Why write smaller code?', because in practice smaller, more unit-able blocks of code invite the possibility of nicer and cleaner design. Manipulating theses smaller blocks becomes much easier than the large ones, and they are more inviting to be refactored as needed. Now, my answer: after mulling over some ...


6

It is usually a good thing to do this whenever possible, but I like to think of this sort of work not as "steps", but as subtasks. A subtask is a specific unit of work that can be done: it has a specific responsibility, and defined input(s) and output(s) (think of the "S" in SOLID). A subtask need not be re-usable: some people tend to think "I'll never ...


5

It's not as strange as you might think. For example, in Standard ML it's customary to limit the scope of helper functions. Granted, SML has syntax to facilitate it: local fun recursion_helper (iteration_variable, accumulator) = ... (* implementation goes here *) in fun recursive_function (arg) = recursion_helper(arg, 0); end I would ...


0

The best you're going to do here is to state what the API isn't, or what it fails to provide. Constructor vs. Setter Injection is a bit of a side journey, but the principle is sound: write your classes so that it is clear from the constructor arguments exactly what is required to create a fully-formed, functional object. If you follow best practices in ...


0

One small improvement to this specific example would be to take the destination File out of the parameters and just make the method return the File (provided this method only generates new files and doesn't add to existing ones). File destination = generate_subset(source, key, index); Other than that, it's mostly what the others are also saying: stuff the ...


1

I totally agree with the comments that throw it back and ask why your parameter list is long. Fewer parameters is usually "better" (where better does have some subjectivity applied). But putting that aside, the rest of your question is about code formatting or style. And the word "style" accurately captures the concept that there is no real right or wrong. ...


2

As the others have suggested, the first course of action is to reduce the number of parameters. Bundle related parameters into composite types. (Harder to do in Java/C#, which lack native support for simple tuples and records, but still a good idea.) There's only 2 ways to permute 2 parameters, but there's 6 ways to permute 3 and 24 ways to permute 4. It's ...


0

We use the StyleCop rule (vertical list) when the list is too large (due to number of parameters or length of names). Though if is due to number of parameters, I would think about creating a class to hold the parameters or refactor the class/method


1

The first problem is that you have a constructor which takes too many parameters. Why are you having so many parameters? If some of them are related, group them into a structure or a class. If all are unrelated, think twice: either they are not, or you are doing it wrong. Otherwise, your third example is the one I see the most in open source projects, ...


3

It should be to avoid them. Robert Martin in Clean Code states limit your parameters to three or less. Pass a class or a struct with your would be parameters in there as members or just make the parameters properties that the method could access. This would be much neater and easier to read.


4

I would break them so each line is conveying a different concern such as superclass constructor invocation or an expression. This might not be 79 characters, but with modern widescreen, high resolution monitors is that truly necessary anymore? Whenever possible, let the IDE format it for you. Some are better at this than others: for example, I have found ...


5

Slightly offtopic answer... Don't worry - this is a common "behavior" in any expert community. And be honest, if you're good in any language, and you will meet a code what is "strange", probably you will critizing it too. (because, want TEACH). I'm in the perl-world - when will see something like: $imax=$#array; $str="" for($i=0; $i<$imax; $i++) { ...


37

Each programming language has a set of idioms and best practices, which will usually lead to elegant, correct, and performant code. Here are a few worst-practices that are perfectly fine in some other language: I will yell if you write for ($i = 0; $i < 42; $i++) { … } in Perl, but not in PHP (in Perl, variables should be declared, and such loops should ...


11

I'm not a hardcore C++ developer, but... Why would it be a bad programming practice to use more error prone code in prototype situations, if refactoring makes it more robust afterward? One thing to keep in mind is that an error in C++ usually means "undefined behavior". In a safe language the worst that could happen is an exception terminating your ...


25

Without seeing the code in question, there are a few ways to write Java code in C++, some worse than others. At the one extreme, there's laying out your source like Java: everything in one file, everything within the class definition, etc.: class HelloWorldApp { public: void main() { cout << "Hello World!" << endl; } }; ...


1

Why would it be a bad programming practice to use more error prone code in prototype situations, if refactoring makes it more robust afterward? When writing quick and dirty with the mind of fixing later there is the danger of forgetting something that you need to fix. How would can program written in C++ be like it was written in Java? What makes it ...



Top 50 recent answers are included