No-programming (mostly without conventions)

I had a colleague who told me that he once worked for a company that had a policy of never having conventions ("if" and "switch") in the code and that they allow all decisions in the code to be executed using polymorphism and (I guess) some other principles of OO.

I understand the reasoning that the code is more severe and easier to update, but I'm looking for a more detailed explanation of this concept. Or perhaps this is part of a more general approach to design.

If someone has any resources for this or they will be ready to explain or even have some additional terms related to this, I can use to find more answers that I would be very obliged.

I found one question on SO that was related, but I am not familiar with C ++, so I don't understand too many answers there.

(I'm not an OO guru, but I can handle it)

I am most familiar with PHP, and after that Python, so I would prefer information that uses these languages.

Update: I will ask my colleague to know more about what he had in mind.

Update 2015: after some more years of programming experience, I now see that the goal of this policy was probably to prevent programmers from adding functionality randomly by simply adding conditional expressions (if statements) in certain places. The best way to expand software is to use the Open / Closed Principle , where software is expanded through inheritance and polymorphism. I categorically doubt whether the policy was strictly strict in all conventions, since it could not have passed completely without them.

+53
polymorphism design oop conditional
Aug 31 '11 at 10:16
source share
6 answers

The Anti-IF Campaign site has several resources, such as this article .

I think this is a matter of degree. Conventions are not always bad, but they can (and often) be abused.

Additional thoughts (the next day)

Refactoring: Improving the design of existing code is a good reference to this topic (and many others). It covers the Replace conditional with polymorphism . The website also has a new, Replace conditional with visitor .

I appreciate the simplicity and sole responsibility for deleting all if . These three goals often coincide. Static analysis tools that support cyclic complexity can quickly indicate code with nested or sequential conventions. if can remain after refactoring, but can be broken down into smaller methods and / or several classes.

Update: Michael Perce wrote an article on Unconditional Programming .

This is a popular topic: Phil Haack on Death for the IF operator !

+45
Aug 31 '11 at 10:23
source share

After several years of programming, I return to my own question, the context of which I now understand a little better.

Sandy Mets speaks well there, where she reorganizes a really hairy ball of if-statements for something waaay less hairy: https://www.youtube.com/watch?v=8bZh5LMaSmE

+6
Nov 13 '15 at 8:21
source share

I had a colleague who told me that once worked for a company that as a politician never have conventions ("if" and "switch" statements) in the code and that they allow all decisions in the code to be executed using polymorphism and (I guess) some other OO principles.

I think your colleague misunderstood something or used the wrong words to explain it. And you cannot completely avoid conditional statements.

There is something to say: the proliferation of if statements inside OOP can be a symptom of poor programming. Example:

Do not use if you need to check the return value of a function like old C-style programming:

 int ret = some_func(); if (ret != null) //do something 

This was typical of C code, but with OOP you should use the exception:

 try{ do_something(); }catch(Exception e){ e.printStackTrace(); //why I was not able to do something handle(e); //there is something else I could do to handle the occurred error } 

Sometimes, if the claims extend to poor design. Consider the following example in Java:

 BaseClass base; if (base instanceof DerivedClassOneFromBase){ DerivedClassOneFromBase d = (DerivedClassOneFromBase)base; d.methodOne(); }else if (base instanceof DerivedClassOneFromBase){ DerivedClassTwoFromBase d = (DerivedClassTwoFromBase)base; d.methodTwo(); } 

This is another example of unsuccessful statements, probably related to poor design. If two derived objects have a common method defined in the BaseClass base class, you could call this method instead of checking their specific type and casting them:

 base.commonMethod(); 
+5
Aug 31 '11 at 10:32
source share

I read the post you linked, and it looks like they mostly talked about eliminating the need for conditional expressions inside the class, and not confusing all the code at all. The idea is that if you need to check the state of an object (using a conditional expression) to determine if it has certain functions, then in fact you have two objects (one of which supports functionality, and the other does not) and should define them as two interconnected.

+5
Aug 31 2018-11-21T00:
source share

Sometimes, conditional methods inside are bad, because they are a sign that you are simply executing several functions or methods of several types in one method.

If you have a class called Automobile and subclasses like Car and Bike, and a method like:

 drive(Automobile a) if (a.isCar) // do stuff else if (a.isBike) // do stuff 

you like to do something wrong most of all. Even if it is not a type-based switch, it can often be incorrect. If a method performs several functions depending on some variable, it often tries to do several and probably should be divided into several methods.

For example:

 save(Car c) if (c is new) // do some stuff else if (c is old) // do some stuff 

could potentially be broken down into save and update, as these are two different functions. Although it depends.

Completely ban if statements were stupid, as they have many valid use cases.

+4
Sep 01 '11 at 13:13
source share

Avoiding conventions does not necessarily mean that you need to do this through polymorphism or inheritance, take for example:

You have 3 different folders for storing uploaded images, uploaded videos and pdf downloads

You can write code like:

 uploadMedia(mediaType){ if(mediaType == images){ uploadTo("myProject/images"); }else if(mediaType == videos){ upoloadTo("myProject/videos); }else if(mediaType == pdf){ uploadTo("myProject/pdf"); } } 

Another alternative that people can use is a switch case:

 uploadMedia(mediaType){ switch(mediaType){ case : images uploadTo("myProject/images"); break; case : videos uploadTo("myProject/videos"); break; case : pdf uploadTo("myProject/pdf"); break; } } 

But then you can completely avoid the conditional statement using something like the / hashmap / json dictionary (depending on what you are working with):

For example:

 HashMap<String,String> mediaMap = new HashMap<>(); mediaMap.put("images","myProject/images"); mediaMap.put("videos","myProject/videos"); mediaMap.put("pdf","myProject/pdf"); //mediaType can be images/videos/pdf same as keys of mediaMap uploadMedia(mediaType){ uploadTo(mediaMap.get(mediaType)); } 

This is a kind of pseudo-code, therefore syntax errors are possible, but in general this concept is also useful for preventing conventions. You can also reduce the line of code.

0
Sep 05 '17 at 7:39 on
source share



All Articles