Tag Info

New answers tagged

12

This is basically long-term vs. short-term choice. You may implement a feature in an hour doing dirty hacks, Or you may spend the next week studying the actual needs of the users/your boss, translate them into requirements, refactor the code to accommodate the code base for the new change, write tests, implement the changes and document the new code. The ...


0

All types have their size determined in compile-time. For handle_t to change it's size in runtime, the data needs to be outside of the type system. std::vector is one way to move your data outside the type system, since it keeps separate memory area for heap-allocated array. The array does not have any type attached to it, only the items have a type. ...


4

This question is highly subjective. In my experience, I have found that parameter lists are clear when they meet the following criteria: The primary object upon which the method or function operates comes first. In English, a sentence might contain a noun, a verb, and another noun upon which the verb could act. E.g.: "Alice sends a message." In OO ...


0

I am having a bit of an issue with some of these things being called "pattern", but clearly, monads, guards and validators are different beasts one may consider patterns. Particularly interesting about monads is of course, that their available operations mean you can apply other functions transparently within the monad (read: map). Therefore, monads are ...


-1

Probably the most objective way to measure if a code is readable would be to measure it through scientific tests. For example measuring the cerebral activity in the brain of other developers which tries to read and understand it. Functional Magnetic Resonance (fMRI) and EEG are now able to answer many questions on the topic. But what is worth to discuss is ...


0

Joshua Bloch (one of Java platform creators) wrote in his book Effective Java in 2001: Avoiding object creation by maintaining your own object pool is a bad idea unless the objects in the pool are extremely heavyweight. A prototypical example of an object that does justify an object pool is a database connection. The cost of establishing the ...


0

The simulation state is managed by ~200 000 variables. It is not possible for us to make a snapshot of the simulation state at every step because it would be highly impracticable, and each time the scenario is updated, the snapshot would be out of date. This sounds like a problem not only for running the simulation at a given chapter, but also for ...


5

A lot of people answered already. Thought I'd give my own personal perspective. Once upon a time I worked on an app (and still do) that creates music. The app had an abstract Scale class with several subclasses: CMajor, DMinor, etc. Scale looked something like so: public abstract class Scale { protected Note[] notes; public Scale() { ...


-1

If you're using c#, make an extension method. that would allow you to do this: var channel1 = new channel(...); channel1.SetVoltage(15); var channel2 = new channel(); channel2.SetVoltage(56); some more info on extension methods: http://msdn.microsoft.com/en-us/library/bb383977.aspx


0

Assuming that all channels are same, setChannelOneVoltage(voltage) and setChannelTwoVoltage(voltage) are doing the same, but on different channels. If you now can identify those channels easily by a function that takes the channel number as the argument, the second approach, using setVoltage(channelNo,voltage) would be the better one, mainly because of DRY ...


3

You could use an enumeration for your channel: enum Channel { C1, C2, C3, C4 }; then you can use a single method: setVoltage(Channel.C1,voltage); setVoltage(Channel.C2,voltage); setVoltage(Channel.C3,voltage); setVoltage(Channel.C4,voltage); and you can use it in a loop: for(Channel channel = Channel.C1; channel <= channel.C4; ...


0

The question is twofold. For the current example and the given set of circumstances, use approach 2. In a more general sense the answer would be "it depends" and there are more approaches than just two. Some more variants on the topic: Approach 3 setVoltage( channel, voltage) setMaxCurrent( channel, maxCurrent) setMaxWorkingTime( channel, maxHours) ...


0

There is a use case: forming a reference to a function or method as a regular object. You can see this in Java GUI code a lot: ActionListener a = new ActionListener() { public void actionPerformed(ActionEvent arg0) { /* ... */ } }; The compiler helps you here by creating a new anonymous subclass.


4

This depends on the context. Both options have its pros and cons and tradeoffs are to be made. Both at once Freedom of choice - allowing setChannelOneVoltage(220) as well as setVoltage(1, 220). Everybody's happy! But it invites inconsistency. One programmer will be using the former, perhaps unaware of the shortcut, another will prefer the latter. ...


3

If you are using an object oriented language, could it make sense to make the channel an object? channels = new Channel[4]; channels[0] = new Channel(); ... channels[0].SetVoltage(220);


11

I would definitely recommend the latter variant. It's more flexible. What if you adopt the code for another device which has not just 4 but 8, 16, 32 or even more channels? You would have to add more and more methods and your code would become more and more convoluted. The code could not be shared between the devices, because you would end up with invalid ...


4

Under the assumption that the implementations would be very similar, I would suggest having one function so for the sake of DRY. It is however also legitimate to use wrapper functions around this function to make your code more instantly readable


0

Both. Use wrapper functions with few parameters to create intuitivity, call a single function with many parameters inside the wrappers to provide compact, coherent code.


2

It's considered a bad practice to have duplicated code. This is at least partially because lines of codes and therefore size of codebase correlates to maintenance costs and defects. Here's the relevant common sense logic to me on this particular topic : 1) If I were to need to change this, how many places would I need to visit? Less is better here. 2) ...


2

This is an exception to the 'should be instances' here - although slightly extreme. If you wanted the compiler to enforce permissions to access methods based on whether it is Karl or John (in this example) rather than ask the method implementation to check for which instance has been passed then you might use different classes. By enforcing different ...


1

You really should do only one of two things Either Return a 200 (OK) status code, and an empty array in the body. Or Return a 204 (NO CONTENT) status code and NO response body. To me, option 2 seems more technically correct and keeping in line with REST and HTTP principles. However, option 1 seems more efficient for the client - because the client does ...


8

If this is the extent of the practice, then I agree this is a bad practice. If John and Karl have different behaviors, then things change a little bit. It could be that person has a method for cleanRoom(Room room) where it turns out that John is a great roommate and cleans very effectively, but Karl is not and doesn't clean the room very much. In this ...


6

In some cases you know there will only be a set number of instances and then it would kind of OK (although ugly in my opinion) to use this approach. It is better to use an enum if the language allows it. Java example: public enum Person { KARL("Karl"), JOHN("John") private final String name; private Person(String name) { ...


12

Why would I create new classes when instances are enough? In most cases, you wouldn't. Your example is really a good case where this behaviour doesn't add real value. It also violates the Open Closed principle, as the subclasses basically aren't an extension but modify the parent's inner workings. Moreover, the public constructor of the parent is now ...


15

It's silly to use this kind of class structure just to vary details which should obviously be settable fields on an instance. But that is particular to your example. Making different Persons different classes is almost certainly a bad idea - you'd have to change your program and recompile every time a new Person enters your domain. However, that doesn't ...


72

There is no need to have specific subclasses for every person. You're right, those should be instances instead. Goal of subclasses: to extend parent classes Subclasses are used to extend the functionality provided by the parent class. For example, you may have: A parent class Battery which can Power() something and can have a Voltage property, And a ...


4

I'd say this is one of many localization scenarios where you'll have to admit a simple "key/language/value" storing pattern isn't enough. :) I'd personally create a localization engine class with a method that'd take a list of words and a language as parameters. This method would build the "list" segment of your string. Once you have this, concatenate the ...


3

Based on the fact you are asking if you should use ArrayList instead, I assume the use of an array is not a requirement. I would use the ArrayList or other collection and rely on interfaces, not implementations, to reference it: List<Integer> myInts = new ArrayList<Integer>(); myInts.add(4); Java generics are in a tough position. On one hand, ...


7

@SuppressWarnings("unchecked") is sometimes correct and unavoidable in well-written code. Creating an array of generic types is one case. If you can work around it by using a Collection<T> or List<T> of generics then I'd suggest that, but if only an array will do, then there's nothing you can do. Casting to a generic type is another similar ...


0

My guess is that you're asking this question because you're already on the verge of being overwhelmed, and the thought of adding more people makes you nervous. You have to keep everyone on the same page, keep everything moving forward, and possibly even contribute code! I've lead teams as large as 8-10 people, but that's a full-time+ job, so you'd better ...


6

Whenever you don't want the class to be extended. Designing for extension has a cost (see also Eric Lippert's Blog: Why Are So Many Of The Framework Classes Sealed?) - you need to make sure you provide the right hooks for the class, that people can implement those hooks without breaking things, that you can be reasonably sure that subclasses won't be ...



Top 50 recent answers are included