Tag Info

New answers tagged

2

Well, it is possible to avoid the switch statement: You can create an abstract factory class, and have some classes inherit from it (ideally via a template argument) to produce objects of the different derived types. Then you can put all the concrete factories you need in a map and use the strings to index into that map to produce a factory for the desired ...


2

I'd take a step back and ask yourself why you are trying to create a singleton in the first place. Don't get me wrong, I know there's a lot of people hating on singleton, and I think a lot of that hate is unwarranted. That said, it doesn't seem like your singleton is providing any functionality except acting as a global constant to IRCApiImpl. If you had ...


1

Well, as I see it, IRCApiImpl has a public constructor making it difficult to call either way a "classical singleton". Still, I'd say that if you follow the guideline of not creating another instance of IRCApiImpl at any other point or even better, making the class inaccessable, you can use either implementation. From a strict Software-Engineering point of ...


0

What are those role-dependent attributes? That's strange for users to have different sets of attributes, since they can change roles in a blink. What will you do with the attributes from role A when user no longer has the role? Delete them? What if tomorrow that user will change his role back to A? So, one approach is to maintain a single overly-bloated ...


1

This may or may not be a violation of the LSP. Seriously. Hear me out. If you follow the LSP, objects of type ProjectTask must behave as objects of type Task are expected to behave. The problem with your code is that you have not documented how objects of type Task are expected to behave. You have written code, but no contracts. I'll add a contract for ...


4

Summary: The design without the abstract class will be only be acceptable if it is carefully documented to distinguish its abstract and concrete behaviours. The Liskov Substitution Principle is generally regarded as a "good thing". By the LSP, I mean that if type S is a subtype of type T, then objects of type S should behave as objects of type T are ...


1

Congratulations, you just have discovered how the violation of Liskov Substitution Principle looks like, as the very first commenter politely pointed you at. To answer exactly to your question: according to the aforementioned principle, you don't design a subclass whose method contradicts its superclass. Reasons for this are explained in the literature ...


2

This bothers me because it looks like a logical contradiction: All scissors are right-handed. Incorrect at this point. You've defined scissors as all having handedness, and that this defaults to right-handed if not overridden. You have not said that all scissors are right-handed, that would require overriding being prohibited (as can be done in ...


34

There is nothing wrong with the design shown in the question. While one could also introduce abstract Scissor with two concrete subclasses, and maybe more overall clarity, it's also common to do it like shown (especially when the hierarchy is a result of years of incremental development, with Scissor being around for much longer than the concept of ...


9

Another option is to introduce the handed-ness as a dependency with a default value of right-handed. In pseudocode here as I am not familiar with Ruby: class Scissors { Scissors(isRightHanded = true) { _isRightHanded = isRightHanded } IsRightHanded() { return _isRightHanded } } class LeftHandedScissors : Scissors { ...


23

You are thinking too logically! There is no logical contradiction because class definitions are not logical propositions. Having the Scissor base class return true does not correspond to saying that all scissors are right-handed. It just means that a scissor instance is right-handed unless the method is overridden in a subclass.


15

You don't. It's like saying that all animals are dogs, and then asking how to make cats meow instead of bark. If you were naming your classes properly, your Scissor class would rather be named RightHandedScissor; now does it make sense to inherit LeftHandedScissor from RightHandedScissor? One possible approach is to make Scissor class abstract, and ...


3

"All scissors are right-handed"? Where do you get that idea from? Your code only expresses "scissors are right-handed by default". It's a default value, not a design decision. If there were no way of having a different value, what's the point in programming a boolean accessor function?


0

I take it you have something like this? public class Main(string[] args) { string param = args[0]; //load parameter from config Classifier c = new Classifier(param) SomeClass x = new SomeClass(c); var o = x.DoSomething(); } public class SomeClass { OtherClass o; //doesnt use the classifier but needs it for OtherClass, //hence ...


0

It depends on how you want access to your objects (Use as less abstraction as needed): 1) use a Container like a Collection or a Map for simly putting the objects in. Collections are made just for "holding" things. 2) If you need more fine grained access: build another object containing the Container. I later use those classifiers in multiple objects. ...


0

Make a "classifier holder" object, hand it the classifiers when they are created, and when the classifier-using objects need them, have them ask the holder to give them one.


1

Unfortunately, exposing public List Items {get;} breaks the whole encapsulation and protection mechanisms. The using code can just do container.Items.Add and put whatever they want. (which is a problem in the other answer as well). More and more I start to prefer composition instead of inheritance, and when some form of inheritance is needed (to avoid ...


2

The direction where you are going is great. I would make a few tweaks to this design like so: public abstract class Container : Item { private float _MaxWeight; private List<Item> _Items; public List<Item> Items { get { return _Items; } } public float MaxWeight { get { return _MaxWeight; } set { _MaxWeight = value; } } ...


2

How to avoid …Helper or …Manager classes« In general: by good Design First, I have a User class. I need a method GetSuggestedFriends() for a user. Yes. A user has a relationship to other users. And the relationship could be expressed as a method on user, e.g. user.isFriend(user2). This is the responsibility of the user-object. Besides that, you ...


0

Your teacher's definition completely misses the most important point of object-oriented programming, the one thing that makes it useful and unique. "Message passing" is a bunch of nonsense dreamed up by the Smalltalk folks, and it's been a failure everywhere it's been tried. The true power of OOP is something known as Liskov substitution, and while the ...


2

I would suggest that you have a FriendshipService class that has a (non-static) GetSuggestedFriends(User) method. Avoid static methods since you cannot implement an interface which makes it more difficult to test. Avoid adding the user object to the constructor since you might want to extend your FriendshipService with methods not specifically related to a ...


5

If you need to ask the question, you don't need to know the answer. If you had a profiler, profiled your code, and found this to be a hotspot, you could trivially find out the answer yourself by simply changing the code and profiling again. Since you haven't done that, we can readily deduce that in fact, you have no such hotspot and no such profiler, and ...


1

From my point of view the method "CanBeAdded" should neither be in the item, nor in a special rulter. It should be in the list itself. Therefore: if list.CanAdd(item) ... end The List implementation then should have a set (probably implemented as list) of rules, that define whether an item can be added. function CanAdd(item) As Boolean for each ...


0

Look at using the Abstract factory, Builder and Factory method patterns. These will let you do something like the below (apologies for any syntax errors): IRequestData rd = RequestDataBuilder.Build('old').GetData(); // not strictly necessary but follow the pattern throughout IRequestProcessor rp = RequestProcessorBuilder.Build().GetProcessor(); on which ...


0

There are two ways to do it. The OO way: Make your current OldRequestData and NewRequestData classes more active and let them implement a method that is executing the desired behaviour. This means your Process methods will be put into your IRequestData classes. The advantage is that you can now add classes implement IRequestData and let them implement have ...


4

create a collection (map, dictionary, etc) of requests to objects. In ProcessRequest, you iterate through the collection to find an entry that matches the input request, and call the associated object. Then you only have to populate the collection, but this can be done by reading configuration or by having each RequestData class register itself at startup ...


1

The constructor should not do too much except initializing some variables or the state of the object. Doing too much in constructor can raise runtime exceptions. I would try avoiding client doing somenthing like this: MyClass a; try { a = new MyClass(); } catch (MyException e) { //do something } Instead of doing: MyClass a = new MyClass(); // Or a ...


0

I think Observer Pattern is a simple and clean solution to update your GUI. It is a behaviour pattern that allows you to implement an event like system. It will keep your GUI nicely decoupled from your library. I normally use it in conjunction with an Adapter pattern in other to decouple the concrete "update" implementation from my main Entity. This way I ...


4

Simplest method is to pass a callback function to the state machine that gets called when a state changes (or other event happens). Then when it gets called you can update the gui in that function (or forward the message to the gui thread if the state machine runs in a separate thread). The signature would be something like void callback(State oldstate, ...


2

GUI libraries (like for example Qt) are using some kind of subscriber-publisher design pattern, which is event based. That means, on an event, all subscribers are going to be notified. So, on a state change, publish the event. Every object that subscribed to the event, is going to execute a callback.


1

You can use any one that you are comfortable with. "Fully dressed" use cases tend to be more verbose and detailed. User stories, on the other hand, involve input that comes directly from users, and is usually a very short description which is less detailed. That said they push more of the design work into coding phase, which most developers tend to like. ...


2

You have a mentor! Wow, great first step. At least somebody cares. The thing about writing code is that any idiot can do it. And a lot of idiots do. The difference between idiot code, and good, clean, maintainable, documented code that works well and is easy to maintain... a bit like the difference between chalk and cheese. Things like coding standards, ...


1

Is my mentor's concern for code quality excessive? Possibly. Your statement that only one (his) version of solving a problem hints to it. But then he probably is way more competent than you are right now, so it might just a (wrong) impression that you get. Others have pointed out how what he does sounds perfectly reasonably. But it shouldn't really ...


2

Your mentor is taking his role seriously; you should always appreciate that. By holding you to a high standard, he is helping you develop the practices and proficiencies that will serve you well in your career. By pointing out errors made by others, he is helping you better recognize bad code so that yours will more often be better. To become expert at ...


-8

This is an opinion question so I'll give my opinion as a desktop and web programmer. Yes his concern are excessive in the context of web development in JavaScript. Such concerns apply to large codebase written in strong typed languages that have to be maintained for decades. JavaScript was never meant to follow best practice and industry standards. It was ...


6

for the first time feeling like I get close to being competent. I've been programming for over twenty years, and every year I wind up saying that to myself at least once or twice. anything takes a whole lot of time because I have to find the best way to do it or else its a waste of time, it also feels like my creativity doesnt matter because there ...


1

As some of the comments mentioned, you may be either consuming the library incorrectly or it may not be a well designed library. But ignoring this, a variation façade pattern seems appropriate. I don't have enough information about your application, but I can think of two approaches off of the top of my head: Simple class MyObject { private ...


6

"Done is better than perfect" I hate to break it to you, but code can always be made better, code is never perfect. If you think your code is perfect, you probably haven't looked at it enough yet, or you're no real engineer, as a real engineer can always make things better than they are. (Or, in the programmers lingo, a real hacker can always make things ...


21

To tell you a little about myself, I'm a newbie programmer working internships and learning a lot from experienced programmers [...] This sentence it seems to me is the key to the answer. I manage teams of programmers. When I do code review with programmers in general (of good and not so good levels of skill) I will get them to write code to meet or ...


9

Your mentor may be an excellent coder, but... Does he have the authority to review and change all the code? Is he potentially imposing stylistic changes on functional code rather than fixing bugs/preventing dangerous behavior? Are the changes being made backed up by unit tests and/or functional tests? Was there significant unit test coverage before the ...


1

When I mentor I always need to keep in mind that my pov is not the absolute truth. Design is still a creative process, but the more I learn the more I put constraints on my creativity, which I don't think kills creativity, it actually makes it stronger. Some of my ideas are still experiments and I can't enforce those to newcomers. Leaving aside "the ...


8

There are two issues here: The issue of your mentor disliking your solution is hard to qualify without concise examples. Maybe if you posted your code, you would find that everybody agrees with your mentor and that you are using the wrong approach (do not worry about it, the fact that mentors exist is the prove that most people need to get some actual ...


130

"Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live." (Took it from here) That said: there is no level of »excessive« for code reviews. I'm a newbie programmer working internships and learning a lot from experienced programmers What is more important than writing code is reading code ; read ...


27

I am very much like your mentor. I treat all code reviews for developers of any experience the same way, even if I wrote the unchanged code I'm complaining about myself. It's no reflection on you or your abilities. Often, the problems with a design are not apparent until you try to modify it. Alternately, a design might have been perfect before the ...


70

Let me quote from Page xxii (Foreword) of "Clean Code" by Robert C. Martin from Prentice Hall. Back in my days working in the Bell Labs Software Production Research organization (Production, indeed!) we had some back-of-the-envelope findings that suggested that consistent indentation style was one of the most statistically significant indicators ...


14

First things first - there is very rarely a single "correct" way to do something in code. So your creativity always matters. That said, there are a whole lot of bad ways to do something in code. Ways that will cause you (or others on your team) issues in the future. Sometimes immediately. So code quality is very important, since poor code quality tends to ...


1

Your way of thinking is perfectly reasonable. I guess, the biggest question is should I even bother doing the refactoring (from a purely practical point of view)? That depends. From the artist's standpoint, the answer is YES. But there is money involved. So, you must estimate the impact of unrefactored code to your development cycle. If it will cause ...



Top 50 recent answers are included