Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

After learning, coding, and coding even more than ever now, I have noticed a trend in a lot of programming which I do. The trend is that the majority of my functions are if{} or if{} else{} statements when it comes to performing the functions in my code.

I narrowed out the option of it just being the project which I am working on. The project is building a PHP Framework or Application (whichever you want to call it). The vast majority of my code for the autoloader.php, application.php, and all of the heavy-lifting files are performing their functions of making sure everything loads correctly and builds the environment.

After working with very complex functions within my code to do this and that, they are all wrapped within an if{} or if{} else{} statement.

Is this a common trend for the majority of programming? Or am I over reacting to something which I am seeing a lot of? Does the most complex functions in coding always go back to the basics of the legendary if{} or if{} else{} statement?

share|improve this question

closed as unclear what you're asking by user61852, MichaelT, Rob Y, Doc Brown, Bart van Ingen Schenau Jun 22 at 7:08

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

4  
This question seems too vague to really answer. It's hard to get far in software without an if statement and there is certainly nothing wrong with them. You could be over-using them. Or maybe you just need them. You're asking for a generalization in order to prove a specific example. –  Brandon Jun 22 at 2:10
    
Sure, the vast majority of programming will involve conditionals of some sort. After all, if you didn't need to branch, you could just pre-calculate your answer... –  Telastyn Jun 22 at 2:13
    
The ability to do a 'test and branch' is one of the fundamental parts of computation. –  MichaelT Jun 22 at 2:23
1  
Maybe adding 10 - 20 lines of pseudo code would help. –  Dan Pichelman Jun 22 at 3:11
1  
In many cases there is indeed a pattern of endless ifs, often quite noticeable in C and Go programs. If you're not afraid to read long articles using unfamiliar languages, but with nice pictures, try reading "Railway-based programming" which explores this pattern and shows a way to tackle the problem, in a way common to functional programming. –  9000 Jun 22 at 3:47
show 1 more comment

1 Answer 1

up vote 0 down vote accepted

There are multiple ways to look at this question.

How the conditionals are used

This is probably more the question you asked. And the answer is, it depends on a number of factors. For folks that heavily use SQL, the end up having queries often built in a way (e.g. using WHERE or HAVING clauses) that the code does not need if conditions. However, those conditions have just moved into somewhere else and look different. Therefore, it becomes an organges to apples comparison.

Some developers will tend to write more conditional code while others may use declarative expressions (such as LINQ in C#), in which the Select, for example, already embodies conditionals.

And lastly, sometimes it's just a personal style. For code clarity, I prefer not to write nested conditionals, and thus end up distributing them elsewhere. In many cases, I reduce the conditionals by using patterns, such as Adapter or DataMapper which pushes conditional in one place and the rest of the logic flows depending on which class was instantiated.

Code complexity

There are some metrics, such as cyclomatic complexity (also known as conditional complexity) that will attempt to estimate whether the code is too complex to be maintainable (and thus considered bad). For the reasons I mentioned in the previous section, and by spending a fair amount of my career in test discipline testing others' code, I can tell you that it's hard to generalize this one too, and thus opinions vary on these metrics, even within the test community.

Howeve, you can read more on these and some tools, such as Visual Studio, will give cyclomatic complexity and sometimes you can try to improve it and see if it helps with code organization. Sometimes, such 'prototyping' does help.

Principle of conditions

In principle, if/else conditions (and all of its sisters) are really making one or more decision. That decision could be because the program actually needs to make a decision on user's behalf (such as deciding whether to include a transaction for a balance sheet, etc.), or a decision for internal program logic (e.g. based on screen resolution, deciding whether the flow is multi-column or single column).

Thus, in general, quite a bit of code can look conditional, and no one can state whether there is anything wrong with it (or not).

Conclusion

In my opinion, code organization is some art and some science. I learnt the art part just by coding for a living for a major part of my career, and the science part by learning through books, code reviews, etc. I do not think anyone can achieve one without the other. Your best bet, if you are concerned about your code, is to convince someone to do a peer review of your architecture, design, data model and code. An outsider, especially someone who hasn't worked with you, can sometimes even bring a perspective that you cannot easily payback. But you can return the favor to them (or someone else).

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.