100
votes
Accepted
Is using parameter names that differ from type names only by casing considered a bad practice in C#?
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 ...
59
votes
Accepted
Is it considered bad software engineering practice to always use "public" methods?
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. ...
50
votes
Working through the single responsibility principle (SRP) in Python when calls are expensive
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 ...
48
votes
Which object should have the method?
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 ...
47
votes
Is it a bad idea have make a class method that is passed class variables?
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 ...
42
votes
Accepted
Why is unit testing private methods considered as bad practice?
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 "...
39
votes
Is it considered bad software engineering practice to always use "public" methods?
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 ...
39
votes
Accepted
Pass object twice to same method or consolidate with combined interface?
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 ...
38
votes
Accepted
What is better IllegalStateException or silent method execution?
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 ...
35
votes
Accepted
Is it okay for a class to use its own public method?
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 ...
33
votes
Accepted
Why is cyclomatic complexity that important for a single method?
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 ...
33
votes
Accepted
Is it a bad idea have make a class method that is passed class variables?
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 ...
26
votes
Which object should have the method?
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 ...
25
votes
Why is unit testing private methods considered as bad practice?
Given that one of the main purposes of unit tests is that you can refactor the internals of your program and then be able to verify that you haven't broken its functionality, it's counterproductive if ...
23
votes
Accepted
Why does Java use :: for method references instead of .?
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:
...
22
votes
Accepted
Which object should have the method?
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 ...
21
votes
Is it okay for a class to use its own public method?
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 ...
21
votes
Accepted
What does it mean for a method or a function to do one thing?
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 ...
21
votes
Accepted
Is there a clean way to model methods that only make sense depending on the current state of the object?
What you've done here has a name. This is an internal Domain Specific Language (iDSL). It can be used like this:
void execute(PlannedMission plannedMission) {
plannedMission
.start()
...
20
votes
Accepted
Is it a good practice to end a function's name with a preposition?
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 ...
19
votes
What is the purpose of enclosing all return values and arguments of a method in separate classes?
Whether it's a widespread convention (I hope it's not), I don't know, but I agree with @Telastyn. Those are the same reason why many people keep naming classes with DTO.
I will ignore the DTO thing on ...
18
votes
What is better IllegalStateException or silent method execution?
There are two distinct kinds of actions one may wish to perform:
Simultaneously test that something is in one state, and change it to another.
Set something to a particular state, without regard for ...
17
votes
Accepted
Can method names give any implementation details and break encapsulation?
The actual inventory is still hidden from outside classes (i.e., the List<GameItem> inventory is private), so encapsulation is not broken. Whether it makes more sense to name the method has(...
17
votes
Accepted
Working through the single responsibility principle (SRP) in Python when calls are expensive
is Python simply poor at handling code breakdown altogether?
Unfortunately yes, Python is slow and there are many anecdotes about people drastically increasing performance by inlining functions and ...
16
votes
Is using parameter names that differ from type names only by casing considered a bad practice in C#?
I do this all the time, it is giving me great piece of mind. If it is an argument passed to a constructor that needs to be assigned to a member, my member would be named range as well and the ...
16
votes
Why is unit testing private methods considered as bad practice?
Writing unit tests for private methods ties your unit tests to implementation details.
Unit tests should test the behavior of a class at the class's outer surface (it's public API). Unit tests ...
16
votes
What is the purpose of enclosing all return values and arguments of a method in separate classes?
What is the purpose of doing so?
Potentially, it allows you to violate the open closed principle and add things to these DTOs without impacting the rest of the code as much. It also potentially ...
14
votes
How to name a method which may or may not perform an action depending on a condition?
You're trapped in a structural way of thinking. A name should abstract away implementation details. It shouldn't become a short hand for them.
IfBirthdayBuyCake();
is a terrible name. It exposes ...
14
votes
Accepted
What's the general option on passing a boolean parameter in a method or constructor in OOP languages?
Enh. It's generally distasteful to pass in a boolean that governs behavior - "If X do this other stuff". The function then tends to not have a single focused responsibility making it harder to test, ...
13
votes
Accepted
Long method refactoring: leaving as is vs separating into methods vs using local functions
Self-contained tasks should be moved to self-contained methods. There is no drawback large enough to override this goal.
You say that they pollute the namespace of the class, but that's not the trade-...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
methods × 202object-oriented × 38
java × 33
functions × 22
c# × 20
naming × 20
design × 18
parameters × 18
design-patterns × 13
python × 13
class-design × 13
object-oriented-design × 12
programming-practices × 12
c++ × 11
coding-style × 11
return-type × 9
class × 8
variables × 8
php × 7
refactoring × 7
static-methods × 7
programming-languages × 6
code-quality × 6
single-responsibility × 6
naming-standards × 6