Linked Questions

179 votes
13 answers
78k 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
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
133 votes
19 answers
16k views

Can a function be too short?

Whenever I find myself writing the same logic more than once, I usually stick it in a function so there is only one place in my application I have to maintain that logic. A side effect is that I ...
146 votes
6 answers
17k views

Are private methods with a single reference bad style?

Generally I use private methods to encapsulate functionality that is reused in multiple places in the class. But sometimes I have a large public method that could be broken up into smaller steps, each ...
Jordak's user avatar
  • 921
35 votes
6 answers
9k views

Should I extract specific functionality into a function and why?

I have a large method which does 3 tasks, each of them can be extracted into a separate function. If I'll make an additional functions for each of that tasks, will it make my code better or worse and ...
dhblah's user avatar
  • 1,381
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
4 votes
7 answers
2k views

How can wrapping an expression as a function be Clean Code?

Another programmer just started working in our team and submitted a patch. What required was to have something that compares and check a couple of conditions and set a property based on the outcome. ...
imel96's user avatar
  • 3,498
10 votes
3 answers
3k views

Being prepared for a code review as a developer?

I am looking for some ideas here. I read the article How should code reviews be Carried Out and Code Reviews, what are the advantages? which were very informative but I still need more clarity on the ...
Karthik Sreenivasan's user avatar
5 votes
3 answers
2k views

What is the purpose of writing functions and methods? When should you make a snippet of code into a function or method?

I have a philosophical disagreement with one of my co-workers, and I'm trying to get back to basics here. What is the purpose of a method? In this question, consider this example. I was criticized ...
SharkCop's user avatar
  • 239
3 votes
2 answers
698 views

Avoiding variables/functions only referenced once

I've heard some developers say that if a variable is only referenced one place, to just replace the reference to it with the value assigned to it. I've also heard developers say if a function is only ...
jinglesthula'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
5 votes
3 answers
423 views

Functions that contain single statement?

Is it OK to have functions that have single statements? I usually make functions that have single statements. I believe these increases code readability and i am able to write code faster as they ...
Gaurav Grover's user avatar