Questions tagged [methods]

A method is a procedure that is associated with a particular object. The purpose of the method is to guide the behavior of the object, and the tag should be used when this is the case.

198 questions
179
votes
13 answers

Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?

Lately I've been trying to split long methods into several short ones. For example: I have a process_url() function which splits URLs into components and then assigns them to some objects via their methods. Instead of implementing all this in one…
Stas Bichenko
  • 3,689
  • 4
  • 24
  • 32
132
votes
10 answers

What is the opposite of initialize (or init)?

The term will be used as a method name. The method is called when a part of the user interface is hidden (or removed), and it is used to reset values to default and dispose objects that will not be used any more. Possible names are: release, remove,…
Gabriel Diaconescu
  • 1,477
  • 2
  • 10
  • 6
103
votes
8 answers

Why have private static methods?

I just wanted to clear up a question I have. What is the point of having a private static method as opposed to a normal method with private visibility? I would have thought an advantage to having a static method is that it can be called without an…
90
votes
10 answers

How and why to decide between naming methods with "get" and "find" prefixes

I always have trouble figuring out if I should name a certain method starting with getSomething versus findSomething. The problem resides in creating helpers for poorly designed APIs. This usually occurs when getting data from an object, which…
knownasilya
  • 3,074
  • 3
  • 17
  • 16
66
votes
18 answers

Are long methods always bad?

So looking around earlier I noticed some comments about long methods being bad practice. I am not sure I always agree that long methods are bad (and would like opinions from others). For example I have some Django views that do a bit of processing…
wobbily_col
  • 1,861
  • 3
  • 16
  • 25
63
votes
8 answers

Prefer class members or passing arguments between internal methods?

Suppose within the private portion of a class there is a value which is utilized by multiple private methods. Do people prefer having this defined as a member variable for the class or passing it as an argument to each of the methods - and why? On…
geoffjentry
  • 870
  • 1
  • 6
  • 8
58
votes
6 answers

What is the difference between a function and a lambda?

I'm a little bit confused about 'function' and 'lambda'. I've seen some examples showing that the scheme keyword lambda works very similarly to the JavaScript keyword function, but I really don't know how they are related. I'm told that 'function'…
Vivian River
  • 2,397
  • 5
  • 21
  • 32
46
votes
6 answers

Refactoring into lots of methods - is this considered clean or not?

So, I watched as my colleague complained a bit about a project he has inherited from someone who is, shall we say, not very experienced as a programmer (intern left to his own devices on a project). At one point there was duplicate code, about 7-8…
Max
  • 2,039
  • 1
  • 16
  • 22
43
votes
7 answers

Is using parameter names that differ from type names only by casing considered a bad practice in C#?

I see questions similar to this with regards to parameter names that match properties on the class, but I can't find anything regarding using a parameter name that is the same as the parameter type name except for casing in C#. It doesn't seem to be…
Jason Tyler
  • 653
  • 5
  • 7
36
votes
6 answers

Why is unit testing private methods considered as bad practice?

Context: I am currently working on a small project in Python. I commonly structure my classes with some public methods that are documented but mainly deal with the high level concepts (what a user of the class should know and use), and a bunch of…
Serge Ballesta
  • 479
  • 1
  • 4
  • 10
36
votes
9 answers

How to name a method that both performs a task and returns a boolean as a status?

If there is a method bool DoStuff() { try { // doing stuff... return true; } catch (SomeSpecificException ex) { return false; } } should it rather be called IsStuffDone()? Both names could be misinterpreted…
Limbo Exile
  • 705
  • 2
  • 7
  • 11
28
votes
5 answers

How do programming languages define functions?

How do programming languages define and save functions/methods? I am creating an interpreted programming language in Ruby, and I am trying to figure out how to implement function declaration. My first idea is to save the content of the declaration…
27
votes
6 answers

Method extraction vs underlying assumptions

When I split big methods (or procedures, or functions — this question is not specific to OOP, but since I work in OOP languages 99% of the time, it's the terminology that I'm most comfortable with) into a lot of small ones, I often find myself…
Max Yankov
  • 901
  • 2
  • 9
  • 17
25
votes
6 answers

How to name a method which may or may not perform an action depending on a condition?

I stumble across this case somewhat often, and I'm surprised about finding so few similar discussions around the web. This question is very related, but my problem is that I want a method that does the more general "do X if Y" rather than "do X if…
aviator
  • 359
  • 1
  • 3
  • 6
25
votes
6 answers

Is it okay for a class to use its own public method?

Background I currently have a situation where I have an object that is both transmitted and received by a device. This message has several constructs, as follows: public void ReverseData() public void ScheduleTransmission() The ScheduleTransmission…
Snoop
  • 2,718
  • 5
  • 24
  • 52
1
2 3
13 14