Linked Questions
23 questions linked to/from Should I extract specific functionality into a function and why?
180
votes
13
answers
81k
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 ...
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. ...
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 ...
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?
...
0
votes
1
answer
815
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(...
130
votes
12
answers
33k
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 ...
28
votes
3
answers
37k
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 = ...
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 ...
8
votes
12
answers
7k
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 ...
8
votes
9
answers
10k
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 ...
14
votes
5
answers
746
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
Have ...
9
votes
5
answers
3k
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 ...
2
votes
4
answers
342
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 ...
2
votes
4
answers
779
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....
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 ...