Linked Questions

129 votes
12 answers

One-line functions that are called only once

Consider a parameterless (edit: not necessarily) function that performs a single line of code, and is called only once in the program (though it is not impossible that it'll be needed again in the ...
0 votes
3 answers

Separate Function or Same Function for similar functionality (SRP vs DRY)

I'm trying to decide the best course of action based on two design principles that seem to be conflicting with each other: "a function should only do one thing" (SRP) and "don't repeat yourself" (DRY)....
9 votes
5 answers

Long method refactoring: leaving as is vs separating into methods vs using local functions

Suppose I have long method like this: public void SomeLongMethod() { // Some task #1 ... // Some task #2 ... } This method doesn't have any repetitive parts that should be moved to ...
6 votes
9 answers

How small should functions be?

I'm new at writing professional code (the bulk of my experience is with personal projects) so excuse me if this is trivial. When I write code I find myself being a little inconsistent with how much ...
14 votes
3 answers

Should a method do one thing and be good at it? [duplicate]

"Extract Till You Drop" is someting I've read in Uncle Bob's blog, meaning that a method should do one thing alone be good at it. What is that one thing? When should you stop extracting methods? ...
2 votes
4 answers

Is creating really small utility functions a bad idea? [duplicate]

Recently, I've been talking with a friend about the code I've written. The code looked something like: import json def save_data(destination, data): with open(destination, 'w') as out: json....
8 votes
9 answers

When should I create separate function (or class)

I do programs for several years. And now I know from my colleagues my pros and cons: Pros: I can solve very complex problem cons: I make overcomplicated solutions for simple tasks. Now I'm trying ...
1 vote
2 answers

Is it unnecessary to have a method that merely loops through an array and calls another method with each item?

For example, if there's multiple lines to be drawn, I could do function drawLines(lines) { lines.forEach(function (line) { drawLine(line); }); } function drawLine(line) { //... } ...
5 votes
7 answers

Is it a good idea to split a constructor in multiple functions?

Here is the work flow of a class of my program: Server class instanciation -> Creating socket -> Binding socket to addr:port -> Listening -> Handling clients Should I put all that stuff in the ...
28 votes
3 answers

In C#, why are variables declared inside a try block limited in scope?

I want to add error handling to: var firstVariable = 1; var secondVariable = firstVariable; The below won't compile: try { var firstVariable = 1; } catch {} try { var secondVariable = ...
1 vote
1 answer

Should I extract method to reduce duplication if the resulting method's functionality will vary based on type of collection given to it?

I am designing a treeNode class. While implementing the getPathToParentNode() and getPathFromParentNode() methods, I noticed that their functionality is essentially the same - they just operate on ...
-3 votes
2 answers

In Python, when is it justified to make long functions into smaller ones?

Say I have this function def function_A(): blah blah blah return A When is it justified to make that long function_A into smaller pieces, say function_B and function_C and then call ...
34 votes
9 answers

When to refactor

I've read through most of Fowler's Refactoring book and have refactored many applications in my past big and small. One of the harder things I find to teach is "when" to refactor. I tend to do this ...
2 votes
2 answers

How to refactor functions?

Let's say I want to have a function to load a project's data from both a json and a file path, the first idea which comes to my mind would be having a couple of functions such as: def ...
14 votes
5 answers

Small functions vs. keeping dependent functionality in same function

I have a class that sets up an array of nodes and connects them to each other in a graph-like structure. Is it best to: Keep the functionality to initialize and connect the nodes in one function ...

15 30 50 per page