Linked Questions

179 votes
13 answers
79k views

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

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 ...
Stas Bichenko's user avatar
21 votes
11 answers
4k views

Why are we supposed to use short functions to sectionalize our code? [duplicate]

I've seen an increasing trend in the programming world saying that it is good practice to separate code blocks into their own functions. Obviously, if that code block is reusable, you should do that. ...
dallin's user avatar
  • 412
17 votes
9 answers
2k views

Hiding away complexity with sub functions [duplicate]

I am having a discussion on code style, and it's starting to sound like a "matter of taste". I strongly believe otherwise, so I'm writing this to get your opinion and learn from your arguments for and ...
Miquel's user avatar
  • 281
14 votes
3 answers
16k views

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? ...
KyelJmD's user avatar
  • 981
0 votes
1 answer
774 views

Is it good practice to shorten functions? [duplicate]

Is it good practice to shorten functions? Like this code for instance: using System; namespace Hello { class Program { public static void Main(...
XiKuuKy's user avatar
  • 121
129 votes
12 answers
31k views

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 ...
deprecated's user avatar
  • 3,297
28 votes
3 answers
35k views

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 = ...
JᴀʏMᴇᴇ's user avatar
34 votes
9 answers
9k views

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 ...
Hortitude's user avatar
  • 449
6 votes
9 answers
6k views

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 ...
CS2020's user avatar
  • 193
8 votes
9 answers
9k views

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 ...
Yuri Yaryshev's user avatar
14 votes
5 answers
713 views

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 ...
mcfroob's user avatar
  • 249
9 votes
5 answers
2k views

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 ...
Vadim Ovchinnikov's user avatar
2 votes
4 answers
307 views

Is using subprocedures to logically separate my code a bad idea for structured programming? [duplicate]

Most of my programming experience is in OOP where I have fully embraced the concepts thereof including encapsulation. Now I'm back to structured programming where I have a tendency to logicaly ...
Chad Harrison's user avatar
2 votes
4 answers
673 views

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....
MatthewRock's user avatar
5 votes
7 answers
2k views

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 ...
Nurrl's user avatar
  • 179
3 votes
3 answers
2k views

How do you balance out code structuring (few big functions vs. many small ones)?

The golden rule of code structuring is always said as splitting into many sub functions is a good thing. Though I noticed it becomes a problem in complex applications when a class of e.g. 10 bigger ...
user avatar
2 votes
2 answers
3k views

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 ...
BPL's user avatar
  • 455
0 votes
3 answers
408 views

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)....
neuquen's user avatar
  • 105
-3 votes
2 answers
2k views

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 ...
JungleDiff's user avatar
1 vote
3 answers
282 views

Redundant function to clarify purpose? [duplicate]

Since what matters is not how the code does it but what it does, would you consider wrapping a function with a different name just to clarify it's behavior in certain situations a good practice? ...
Asics's user avatar
  • 239
1 vote
2 answers
171 views

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) { //... } ...
clb's user avatar
  • 521
1 vote
1 answer
110 views

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 ...
Hassaan's user avatar
  • 111
2 votes
1 answer
155 views

Validating data to nest if or not within try and catch

I am validating data, in this case I want one of three ints. I am asking this question, as it is the fundamental principle I'm interested in. This is a basic example, but I am developing best ...
user avatar