Hot answers tagged

39

I would include a comment at the top of the file indicating the version of the relevant software you've used. A quick comment stating "Tested with Python 3.6 (installed through Anaconda)" or something to that effect is nice to make sure everyone is on the same page. Since this is intended as teaching example, I will focus on minimizing the current ...


18

Since @spyr03's extensive (and awesome) answer did not include it, here are some small comments. You want this to be an example for your students of how code should look like. You should include a docstring with every class, method and function to detail what it does and what it's arguments and return value are. Although your code is rather self-documenting, ...


14

When I think back to my student days, the most crucial point for me to understand code was always the entry point. In my experience, it takes a lot of experience to understand a code concept as a whole. The untrained is used to step by step thinking and evaluation through step by step progression. I would not have understood that code because it describes ...


6

One thing I don't see mentioned in spyr03's excellent review: I think it's unnecessarily inconsistent (and thus confusing) for you to use mixin classes to implement 100% of the get_valid_moves routine for Rook, Bishop, and Queen, but then reuse only half of that code for King (and open-code the other half). If you're going to write class Queen( ...


4

First off, you are using std:: for everything else, might as well use it for std::uint32_t and std::size_t I'm not very familiar with variadic templates, but I feel like dropping into C's varargs is not the way to go. You have to do the messy snprintf to make the prefix (which now means you need the max prefix length) Only primitive types can be logged. ...


4

Quick review, in no particular order. I haven't gone line by line, I just scrolled around and noted what I saw. Firstly, the good stuff: Consistent style (almost, see #8 below) "use strict"; jslint-checked Then, the not-so-good stuff Naming convention may be internally consistent (not that I can really tell), but your library is not, in my opinion, quite ...


2

The main problem I have with this is: void log (const char* format, ...) Its not what you think. The problem is that all the parameters have to be evaluated before the method is called. This can potentially be expensive (or it may be cheap but you are doing it a lot). If the logging is turned off this can be a lot of work with in the end no result being ...


2

ADL This can be improved. First - you're requiring a tie() member function, it'd be better to make that more of a customization point. Second - you're putting all the operators in global scope when it'd be better to hide them. Both issues can be solved with some ADL. The advantage of making an operator a non-member friend declared inline is that it can ...


2

Is this a useful Mixin? Depends on if you need it a lot. Or is it a bad practice to create Mixins like this? Nothing is bad practice if it helps you float your boat. Is it too complicated or too hard to understand? Not really, but seems like you'll often have to lookup the generated CSS to fix positioning problems. F.e. I'm not a big fan of 10px, null, ...


2

Everything is an Entity! If you would follow the Entity Component System approach, that is. The idea is that all objects are entities, and each Entity has several components. Size, Position, Movable - all this are different aspects of what an Entity possibly could do and are therefore each it's own Component. As for Systems, that's where most of the logic ...


2

The main things is that your system is much more verbose and harder to understand than simply hard-coding all the calls to superclass methods: function A(a) { this.a = a; } A.prototype = { methodA: function () { console.log("a: "+this.a); } }; function B(a) { this.b = a+1; } B.prototype = { methodB: function () { console....


2

It has been some time since I asked the question. I used your advice to improve the code and gave it as an exercise to my students. It's been a tremendous success. I will detail some of the improvements as a follow-up answer to my question. Thank you for the excellent answers. What a great community :) I have added docstrings to all the functions & ...


1

I think the norm is to just try to access the schema attribute and let it fail with an AttributeError if there isn't one. But here are two possibilities to catch it before runtime (at compile time, or using a type checker). Using __init_subclass__() This may be a good use case for adding an __init_subclass__() method to the mixin class. It will get called (...


1

The point of a mixin, at least as I've always understood it, is to inject methods into other classes -- and not to create instances of the mixin class itself. So putting an __init__() in a mixin does seem like an odd pattern. Of course, __init__() is just a method: why not inject that method too? Fair enough, but it still feels off. And is it really ...


1

The $type can be passed in directly to the pseudo-class selector after you check its validity. @if $type == 'after' or $type == 'before' { &:#{$type} { content:$unicode; font-family:"FontAwesome"; font-size:$size; color:$color; font-weight:$weight; margin:$margin; @content; } }


1

Formatting Format your code better. Put a space after the colon after properties. Don't put multiple properties in one line. Use of SASS Put your colors and lengths into variables so you can reuse them at other places, when they are supposed to be identical. Why are the numbers not rounded? There is probably no visible difference between 1.073rem and 1....


1

I don't remember much about different hash tables, but the fact that I haven't been able to figure it out from your code is should say something to you. For example you m parameter should be named size. We are in python so insert should be __setitem__, member is __contains__ and there is also __getattr__. And they should take object not its key, right? ...


1

You usage of the mixin will work and although it is not pretty there is not much else that can be done as far as valid LESS / CSS output. I've written up an example of complex mixins like below. LESS lazy mixin for element width/height/float As well as one that might be on track with what you are trying to accomplish. https://stackoverflow.com/questions/...


1

ES6 has support for classes. It also has support for super calls and mixins. I highly recommend you ditch this approach and write future-proof code. You can always use a transpiler like Babel to convert ES6 code back to ES5-compliant code which most, if not all, browsers are supporting. log = function( s ) { There's no var keyword. Should you want to wrap ...


1

Using CanBuildFrom will help you here. What you need is an implicit CanBuildFrom[M[K, T], (K, T), M[K, T]] in scope, which will be provided by the standard library if it is possible to construct instances of the correct types. import scala.collection.generic.CanBuildFrom implicit def mapUpdatedFunctor[M[KT, TT] <: Map[KT, TT], K, T] (implicit cbf: ...


1

In short: keep it simple. I'm not sure if I'm supposed to review code that was not pasted here, but I took a look, since you linked it, and your sample is very brief. I really think that you are over-designing it (and even than your design has flaws): I see no reason for a factory here. Why do you want to use it (we do use them in Ruby, although we often ...


1

It's hard to say much about such a short code sample, but factories simply encapsulate object creation (wiring up objects), which frees code using them from that responsibility. In that sense, factories are relevant in ruby too. However, note that client code would look like this, given your current code: translator = FreeGoogle::Factory.create which isn'...


1

There's always a choice, even if you need some additional state: You can add the class directly to the class and provide methods for it in the interface or go for your solution. Adding to the class directly would look like this: public interface IsBusyMixin { boolean isBusy(); void dumbToggleBusy(); void overlayTheComponentWithASpinner(); // ...


1

Refactoring You can DRY this up a bit by taking advantage of lists and the zip function. @mixin triangle($dir, $color, $size, $leg1: $size, $leg2: $leg1) { $sides: 0, $leg1 solid transparent, $size solid $color, $leg2 solid transparent; $order: ( top: top right bottom left , right: right bottom left ...


1

Trying to use JavaScript like a class based language is what you make it. It sounds like someone was expressing an opinion, rather than imperial evidence. As for your implementation, it's very confusing how you'll expect to use the Objects object. Be very careful with mixins or, "multiple inheritance." If you need multiple inheritance, I would argue that ...


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