New answers tagged

3

Your code looks very structured and nicely written, but to my understanding, your solution is not "clean architecture" (CA) as described by uncle bob. Your solution is an MVC solution. In clean architecture you can (for example): easily replace express with another framework easily replace mongo with another DB In your case the framework is embedded into ...


1

On the big picture question, you ask for the best OOP conventions, to which the answer is probably “don’t use OOP; use POP (protocol oriented programming)”. Use OOP where you really need hierarchies of concrete types, but that would not appear to the be case here. For example, you’re never instantiating what you called an Events object, but rather you are ...


1

From the high level I don't see anything wrong in your constructor. But I have some improvement to propose: Use interface Your NewsSpider is an abstract class with one empty method. If you want to force the implementation of the crawl method make it abstract. And if your class has no implementations, make it an interface. interface NewsSpider { void ...


1

Use interface instead of class with single method Each concrete implementation should be a singletone Each concrete implementation should contain only logic and accept your current news in crowler() method: do not recreate a strategy instance for each news.


1

template <typename... Args> void insert(const Key& key, Args&&... args) { auto resPtr = std::make_unique<Resource>(); if (!resPtr->loadFromFile(resourcesDir + std::forward<Args>(args)...)) { msgErrorLoading(std::forward<Args>(args)...); ///* todo: should I e.g. "throw ErrorLoadingResource" here? *...


2

Type hints You're most of the way to having a well-explained constructor: ticker (str): Stock-ticker Ex. 'AAPL' It's better to move that (str) to an actual type hint, i.e. def __init__(self, ticker: str): Superfluous methods As it stands, make_request doesn't need to exist. It would make sense to keep it if you add a Session instance to the class, ...


3

Please consider follow recommendations: Make habit of extracting each struct/class/enum in separate files which brings more readability and clarity. Code looks less cluttered. Please try getting used to of not using semicolons :) You have defined Events as a class with empty implementations of functions. Are you sure you want to do that, I think it could be ...


2

It is good practice to extract the UI as smaller components in smaller classes and use UIViewController only to bring them together. You should try to imagine each component as an independent unity which could be reused across your application in multiple places. Writing code by UI brings more clarity but it is a matter of person choice and takes time ...


0

You seem to have grasped some of the mechanics of a fluent interface without grasping the fundamentals around purpose or fit (maybe your gut has, but your mind has not yet reached this understanding). When Evens and Fowler first document this pattern, their intent was to make code clearer and more readable by allowing methods to be chained together. Your ...


2

Lets first respond to your initial question: is it fine to typecast from one implementation to another? Absolutely! When using interfaces to declare behaviours like you have shown, not only is it fine but it becomes important that you DO typecast, otherwise what was the point of declaring the behavior through interfaces in the first place. The use of ...


2

Since Weapon and Attack are partially related, you may need to implement each separately in order to make them flexible for future changes, and since Attack can be defined in multiple classes (not just for weapons), we can either implement an interface for it, or an abstract class, (or both if needed) and use it as definition of the attack logic. If you ...


2

I get the feeling you just want to call the setters in a chain to spare you from writing item. and ; before and after every call, instead of actually having a fluent interface. Does this actually make your code more fluent and more maintainable or are you introducing foreign concepts that confuse the people who read it the future? Will you even remember ...


2

Composition over inheritance and Strategy Pattern I would, instead of having differents weapons implementing differents interfaces defining their behaviour, create classes that holds the logic of the behaviour Let's consider a weapon can have a primary attack, a secondary one and reload. The class could be defined as such : abstract class Weapon { ...


0

I wrote code like the question once for a Fluent Builder which also accepted Consumer as parameter to allow callers to provide a set of opaque modifications determined earlier. In a menu example this might be: new Fluent<>(new Menu("File"))) .with(o -> o.setAccelerator(accelerator)) .with(contextSensitiveMenuItem) This rapidly required a ...


3

Yes, your implementation is sound and possible...however, it doesn't make any sense to have it in the first place, at least not for your example. final MenuItem item = new Fluent<>(new MenuItem("Foo")) .with(o -> o.setAccelerator(accelerator)) .with(o -> o.setOnAction(this::...


2

One of the great things about working with OOP, is instance methods and polymorphism. If you add methods in a Line class to perform this logic. Then you hold several instances of the line, and move each with a simple method call. You can also make use of inner properties to make the calculation simpler. This may not help enough. You said there are 8 ...


1

I think what you are missing is dependency injection and dependency inversion. You should have something like let keychainService = KeychainService() let apiClient = ApiClient(keychainService: keychainService) let loginService = LoginService(apiClient: apiClient, keychainService: keychainService) let loginViewController = LoginViewController(loginService: ...


2

General feedback For a beginner this looks quite thorough, though I did spot a couple places where improvements can be made. See the suggestions below. It is really tempting to suggest ecmascript-6 features but I will resist the temptation because you seem to want to stick with ES5 for now. Suggestions Strict mode Some functions contain: 'use_strict'; ...


0

As addition to @Simon Frosbergs answer, I would create an interface for the factory: interface BuildingFactory{ fun build(...) : Building } For the param you can choose for enums or Strings: enum class BuildingType{ Woodcutter } The enums are typesafe and the Strings are not. The drawback for enums is that every time you add a BuildingType, you ...


1

First of all, I would recommend using a data class. In order to do this, it's better to use List rather than Array (because Array does not support hashCode/equals) data class Building ( val name: String, val inputs: List<Resource>, val outputs: List<Resource> ) Actually, considering that the order of elements doesn't matter you ...


2

Use optional instead of null. I wrote an article about avoiding nulls if you are interested. Use early returns to avoid nesting If !reader return [status: '50', message: 'READER NOT FOUND'] If !userSettings return... Rest of the logic I prefer to avoid nulls,so I would choose to call save with failOnError=true and it will throw exception instead of ...


0

This is accomplished more elegantly and minimally by: Fetching methods from relevant modules Assigning these methods to the class, programmatically We can further ensure that methods are intended for the class by checking whether self is in the input signature, and can omit specific methods by name. Below accomplishes this: from inspect impport ...


0

Hi OverLordGoldDragon, Why don't you define your methods as functions in your submodules and attach them to your main class afterwards? # module_a.py def woof(self): """I'm a dog""" self.do_woofing() def do_dooting(self): print("DOOT") # module_b.py def doot(self): """I'm a skeleton""" self.do_dooting() def do_woofing(self): ...


3

It definitely seems like a hack. A first smell is that changing the function signature by just renaming variables will break it. If a is available in locals(), it has to match a in the function signature. Even just a capital A in either spot will break the behaviour. This requires you to change names in multiple locations if you only wanted to change it in ...


0

Avoid overloading the == operator (and other operators) as this is a tricky business, and best avoided - so I wouldn't go down that route. OK next the code. Enums and Integer representations If you can alter the Individual and IndividualDm classes. Add something that returns the integer value as the Enum... Simple version: Models.Database.Individual ...


1

It is difficult to judge without full code, but while keeping things mutable (please use a more descriptive names, a future you will thank you): public class StatusModel { public IndividualStatus Status { get; set; } public int StatusId { get => (int)Status; set => Status = (IndividualStatus)value; } public bool InProgressStatus => ...


Top 50 recent answers are included