Hot answers tagged

100

Don't overthink this, Range range is fine. I use such kind of naming for more than 15 years in C#, and probably much longer in C++, and have never experienced any real drawbacks from it, quite the opposite. Of course, when you have different local variables in the same scope, all of the same type, it will probably help to invest some mental effort to ...


82

The characteristic of being static is independent of the visibility. The reasons that you will want to have a static method (some code that does not depend on non-static members) will still be useful. But maybe you don't want anyone/anything else to use it, just your class.


69

In .NET, you often have pairs of methods where one of them might throw an exception (DoStuff), and the other returns a Boolean status and, on successful execution, the actual result via an out parameter (TryDoStuff). (Microsoft calls this the "Try-Parse Pattern", since perhaps the most prominent example for it are the TryParse methods of various primitive ...


59

The methods, properties and constructors (i.e. members of a class) that you define using a public accessor determine the surface area that the users of your class will be allowed to interact with. Any method that you don't want to be part of this surface area should not be made public. Reasons why some members might not be public: They are implementation ...


52

The only reason I can think of is that dates are immutable objects, so by calling plusDays you're not adding days to the original object but creating a new one with new properties, but that's very vary subtle. This is exactly the reason. Imagine you had some kind of api for manipulating ranges of dates for scheduling purposes. It might expose methods ...


50

Many potential performance concerns are not really a problem in practice. The issue you raise may be one of them. In the vernacular, we call worrying about those problems without proof that they are actual problems premature optimization. If you are writing a front-end for a web service, your performance is not going to be significantly affected by ...


47

Calling a class method with some class variables is not necessarily bad. But doing so from outside the class is a very bad idea and suggests a fundamental flaw in your OO design, namely the absence of proper encapsulation: Any code using your class would need to know that len is the length of the array, and use it consistently. This goes against the ...


47

A user is someone who is registered and able to use the system. A chat room is a place people can chat. What happens when a user joins a chat room? What is that thing that represents a user who has joined a chat room? That is the abstraction you are missing. Other answers are hinting at this. You could say a user participates in a chat. You need a class that ...


40

No, this is perfectly fine. It merely means that the API is over-engineered with regards to your current application. But that doesn't prove that there will never a use case in which the data source and the measurer are different. The point of an API is to offer the application programmer possibilities, not all of which will be used. You should not ...


38

There is no rule. It's entirely up to how you want to make your API "feel." Personally, in a music player, I think a transition from the state Stopped to Stopped by means of the method Stop() is a perfectly valid state transition. It's not very meaningful, but it is valid. With this in mind, throwing an exception would seem pedantic and unfair. It would ...


38

Short answer: Declaring a method or field "public" translates to a promise: Dear colleages, I invite you to use this method or field wherever you find it appropriate. I have documented everything you need to know about its usage, and as long as you don't violate this documentation, I take responsibility for every bug in this context. ...


33

I would say it's not only acceptable but encouraged especially if you plan to allow extensions. In order to support extensions to the class in C#, you would need to flag the method as virtual per the comments below. You might want to document this, however, so that someone isn't surprised when overriding ReverseData() changes the way ScheduleTransmission() ...


33

The core thing here: "brain capacity". You see, one of the main functions of code is ... to be read. And code can be easy to read and understand; or hard. And having a high CC simply implies a lot of "levels" within one method. And that implies: you, as a human reader will have a hard time understanding that method. When you read source code, your brain ...


33

There are may things with the class that I would do differently, but to answer the direct question, my answer would be yes, it is a bad idea My main reason for this is that you have no control over what is passed to the add function. Sure you hope it is one of the member arrays, but what happens if someone passes in a different array that has a smaller ...


32

Would I be correct in assuming that your "parse" function not only parses the code but also executes it at the same time? If you wanted to do it that way, instead of storing the contents of a function in your map, store the location of the function. But there's a better way. It takes a bit more effort up-front, but it yields much better results as ...


31

Speaking strictly, a procedure is a subroutine that is executed purely for its side effects (like printing something to the screen) and returns no values. A function is a subroutine that always returns the same value given the same inputs and has no side effects. A method is a procedure or function that is associated with a class or object. The confusing ...


31

A fairly common reason (in Java) would be for initializing immutable field variables in a constructor by using a simple private static method to reduce constructor clutter. It is private: external classes should not see it. It is static: it can perform some operation, independent1 of the state of the host class. A somewhat contrived example follows... eg: ...


29

For example, imagine an initialisation method split into a series of small ones: in the context of method itself, you clearly know that object's state is still invalid, but in an ordinary private method you probably go from assumption that object is already initialised and is in a valid state. The only solution I've seen for this is... Your concern is well-...


29

A couple of reasons: Typically when you're tempted to test a class's private method, it's a design smell (iceberg class, not enough reusable public components, etc). There's almost always some "larger" issue at play. You can test them through the public interface (which is how you want to test them, because that's how the client will call/use them). You can ...


28

Your code appears to have one feature you can use to your advantage here: all those grouping comments. You can use those comments as the basis for breaking up the method into chunks by extracting smaller methods. You pull out each chunk into its own method, then call that method from the code's original location. As you start to pull these pieces out, you ...


25

Rule of thumb: Your methods are probably in the right place if they don't need to pull data out of other objects. This is often called "envy", as in "feature envy". If your method wants data that does not belong to it, it is basically envious of the features of some other object. Don't envy, cooperate! Thinking about who do you want to ...


22

This is to avoid ambiguity in case if class has (static) member with the same name as method (Java allows that). It is easy to see from code snippet in Java tutorial about method references: Because this lambda expression invokes an existing method, you can use a method reference instead of a lambda expression: Arrays.sort(rosterAsArray, Person::...


22

In your case, you have users vs. Chatrooms. If you have methods that concern both, you could put them into either class, not much difference. However: You will have not only users vs. Chatrooms, you will have Users vs. bills, users vs. Support requests, users vs. 100 other things. If you do things consistently by putting methods into the user class, it will ...


21

UPDATE: This question was the subject of my blog in May 2014. Thanks for the great question! To add to Robert Harvey's answer: a property should be: logically a property of the class, the way that say its color or year or model are the properties of a car. not more than, let's say, ten times slower to compute than fetching from a field. something you don't ...


21

A common use-case for a private static method is a utility method which is only used by that one class is independent of the internal state of that class


20

Absolutely. The visibility of a method has a sole purpose to allow or deny access to a method outside the class or within a child class; public, protected and private methods can always be called inside the class itself. There is nothing wrong in calling public methods. The illustration in your question is the perfect example of a situation where having ...


19

You see that you have a few lines of code that is repeated in a lot of your methods, so you decide to extract them to a single method, as duplicated code is not good. You make the method private as it is not designed for widespread usage and you don’t want unrelated code calling it. (Debate this point in the comments….) As the method does not access any ...


19

Console applications predate GUI applications, and these take command-line parameters for very long time (at least from CP/M time, which preceded MS-DOS, which preceded Windows, which preceded Windows NT — all preserving more or less the same logic). You can pass command-line parameters to Windows GUI apps, too. These are the args.


19

If it adds meaningful clarification or fits the "ethos" ... Yes. .NET's OrderBy() and ElementAt() might be good examples. Personally, I like code that reads pretty much like English. It takes a ton of guesswork, hovering and digging, and arcane knowledge out of reading someone's code when it tells me unambiguously what it does. And as far as I can tell, I'...


19

If I tell you to "take out the trash" I've asked you to do one thing. It's one cohesive idea. Does that mean it can't be decomposed into more things? No. Everything can be decomposed into more things. If I tell you this function should do one thing I'm not telling you it should manipulate only one trash bag or only one quantum quark. I'm telling you if a ...


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