Linked 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? [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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

15 30 50 per page