Linked Questions

3 votes
2 answers

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 ...
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 ...
35 votes
6 answers

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 ...
133 votes
19 answers

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

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 ...
4 votes
7 answers

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. ...
5 votes
3 answers

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 ...
5 votes
3 answers

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 ...
1 vote
3 answers

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? ...
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. ...
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 ...
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 ...
10 votes
3 answers

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 ...