r/AskProgramming • u/RedDragonWebDesign • Nov 12 '20
Other What features of programming languages do people OVER use?
Inspired by this comment and this sister thread.
What features of programming languages do people OVER use?
I'm gonna guess that OOP is a strong contender. What else we got?
4
u/Altavious Nov 12 '20
Possibly an unpopular answer, but I'm going to go with conditionals (if statements). Every time one is used it creates a potential fork in the flow of a program that can fail so you need to have testing of one sort or another to feel confident that it works. On the OOP front, Sandy Metz does a good talk where she compares common OO vs a better OO design for a system and counts the conditionals that result if you want a good example.
1
u/RedDragonWebDesign Nov 14 '20
What's a good alternative to conditionals?
1
u/Altavious Nov 14 '20
The goal is to reduce not eliminate - static configuration over runtime configuration. Approaches like functional core/imperative shell to box the branching logic: https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell Design pattern wise, approaches like the strategy pattern. In traditional OO pulling the conditional logic into a passed in object so that paths aren't available unless they are likely to be used. Similar to this:
https://www.deconstructconf.com/2018/sandi-metz-polly-want-a-message
2
u/YMK1234 Nov 12 '20
Well then have fun writing programs without conditions. And no, just because your condition is hidden behind some abstraction magic it does not go away. It just moved somewhere where you'll have an even harder time testing for it because it just became non-obvious.
0
u/Altavious Nov 12 '20
I didn’t say no conditionals - the original statement was “overused”.
3
u/YMK1234 Nov 12 '20
people generally don't put conditions into programs for no reason, but because they are actually needed though.
59
u/Poddster Nov 12 '20
Terseness and "one-liners". This is especially true in functional languages. I prefer every line of code to have the same amount of "thought" required for it, so it can be read at a consistent speed. But then you'll hit a whopper of something nested list-composition thing full of lambdas and whatever. I prefer to split it out onto multiple lines, give the lambdas a name, etc.
26
u/nermid Nov 12 '20
This. Code golf is cancer.
4
u/xigoi Nov 13 '20
Code golf is a fun game to practice creative problem solving. Good golfers know very well that's not what their “real” code should look like.
8
0
Nov 12 '20
[deleted]
10
u/nermid Nov 12 '20
Or you could just spend the extra ten seconds to write readable, maintainable code.
0
Nov 12 '20
[deleted]
7
u/ckini123 Nov 12 '20
Not defending code golf but list comprehensions are measurably faster than appends and tend to be the fastest method of generating a list in Python.
Whether or not you value the slight trade-off in performance for readability is up to you but definitely an argument to be made.
The example you gave is perfectly understandable, Pythonic code I'm upset you had to rewrite it.
1
u/BoredInventor Nov 12 '20
I guess it becomes more verbose as custom records and the like are introduced. I rarely juggle with numbers
2
0
Nov 12 '20
[deleted]
1
u/Ran4 Nov 13 '20 edited Nov 13 '20
Python's list and dict comprehensions tends to be rather readable though, as long as you don't have multiple levels of loops.
But e.g.
young_people = [person for person in persons if person.age <= 35]or even say
[FieldOut(name=field.name, value=field.value) if isinstance(field, SuccessField) else FieldOut(name=field.name, value="") for field in get_fields()]is much more readable than the alternative.
30
u/cyrusol Nov 12 '20
OOP isn't a feature, it's a paradigm. And it isn't overused per se either, it's just wrongly applied because it was wrongly taught. People should look at how for example older Smalltalk software or the OCaml standard library were designed in order to apply OOP more usefully.
But what is overused is inheritance, This was true in the 90s and remains true today, sadly. OOP is much more than inheritance.
10
4
u/VirtualLife76 Nov 12 '20
I think what op meant, was using oop design when it's just not needed. I've seen people extract a simple if statement into strategy pattern or similar for no good reason. No need for 20 lines of code when 1 will do.
1
2
u/east_lisp_junk Nov 13 '20
The design of the OCaml standard library mostly suggests that the useful way to apply OOP is to avoid it ¬_¬
2
u/Python4fun Nov 12 '20
Applying Interfaces for each different feature that a thing can have is dumb. If you don't have multiple things needing to be referenced by a shared ancestor then don't abstract away an interface or abstract class.
0
u/cyrusol Nov 12 '20
Disagree.
If you don't have multiple things needing to be referenced by a shared ancestor
Meaning: if you don't do unit tests.
2
u/Python4fun Nov 12 '20
Why would you write unit tests that required the different classes to have a shared ancestor? Tests should not govern your class inheritance of the application code.
2
Nov 12 '20 edited Nov 12 '20
The Liskov Substitution principal has many ramifications but on is to suggest that anything that consumes an interface should work in a manner that comes up with consistent results regardless of the concrete type passed in. Meaning in regards to the consuming piece of code, you can substitute any concrete version and have it behave as expected.
A very good example of this in action is C#'s LINQ extension methods. It doesn't matter if you drop a List<T> or a HashSet<T>, the LINQ methods will do their job because they are operating on the contract defined in IEnumerable and IQueryable interfaces. Anyone that has used LINQ to any degree would probably agree about the power that comes out of this design principal.
So how does this relate to your comment? Well you can apply this same concept to unit testing and write tests that test those interfaces. Then you can easily add new derived types and have a suite of tests that ensure it works as it should in addition to whatever tests test the specifics of the concrete version. But the key point is you can have a suite of tests ready to go with little effort.
Just by breaking out an interface you loosely couple your components in a manner that enables powerful code reuse and patterns. Interfaces should really be at the heart of any design in languages that support them.
I personally like to map out large portions of the structure using interfaces before I even think about what I need in-between the curly braces.
4
u/Python4fun Nov 12 '20
I completely agree with the power of interfaces. My disagreement was originally about creating interfaces where the code has no use for them.
The ones that are most offensive from my experience are interfaces that are only used by one class.
Edit: if you are choosing a good candidate to become an interface then it should actually be useful in the application code.
-2
Nov 12 '20 edited Nov 12 '20
I'd still disagree with you slightly there. Because there is still utility for the interface even if it only has one class that implements it in your application. Let's put aside the fact that you will enable the ability to easily add a second class if needed (despite that being compelling enough for me) and let's assume you'll only have one. You still will probably have that class a dependency somewhere which means you will want that dependency to be mockable for unit tests. The easiest way to enable that is via an interface in most languages.
The only compelling reason in C# I know of where I probably won't break out an interface is when dealing with structs and that is mainly to avoid boxing as well as them basically having minimal in the means of methods and properties. But even that isnt really THAT big of a deal.
1
u/Python4fun Nov 12 '20
My argument is more about adding unnecessary complexity. Until you have a second class for that functionality, I see no reason to add the interface. I've seen too many times interfaces built for passing things around when there is only a single descendant class, and all that it does is make the code substantially harder to understand and maintain.
0
u/Ran4 Nov 13 '20
Because there is still utility for the interface even if it only has one class that implements it in your application. Let's put aside the fact that you will enable the ability to easily add a second class if needed
Re-read that sentence again :)
If there is only one class that will ever implement it, then by definition the ability to easily add a second class if needed is worthless.
You still will probably have that class a dependency somewhere which means you will want that dependency to be mockable for unit tests.
Not true: a lot of the time, simply using the one implementation is fine. For every bug you introduce by depending on a single implementation you'll introduce just as many from the code bloat by having lots of interfaces.
You should create abstractions around IO to make things more testable, but you don't need an interface for every single pure class you're using.
0
u/Ran4 Nov 13 '20
The point is that you don't need to care about these things if there is only and will only ever be one concrete implementation. Which is surprisingly often the case.
Dependency inversion is useful, but it does lead to massive amounts of bloat and a hard-to-read codebase.
1
Nov 13 '20
The point you are missing is you never know. You speak of things in hindsight as if these things are clear during development.
1
u/cyrusol Nov 12 '20 edited Nov 12 '20
Should not? Based on what?
That's only possible if tests were completely separated (like system tests, black box tests) or if you rely on reflection to allow for the possibility of making a type polymorphic at runtime. Which isn't supported by every language/platform and there may be arguments to not rely on reflection anyway.
Otherwise the dependencies of the unit to be tested must necessarily all be polymorphic, aka. interfaces for which there are multiple implementations possible in order to just making said unit testable.
1
u/Python4fun Nov 12 '20
My argument was against interfaces for single implementations.
1
u/cyrusol Nov 12 '20
I am aware. My argument was in favor of that since any unit that have those as a dependency got to be unit-tested too.
1
u/Python4fun Nov 12 '20
When you implement the second class, you could make the interface and move the tests there. I just find that most of the time future-proofing winds up going in unnecessary directions. This is especially true when you have a really complex application to start with.
0
u/Ran4 Nov 13 '20
If it has that dependency, then it doesn't sound like a unit test.
1
Nov 13 '20
Thats the point you don't get. Most code has dependencies and are unit tested by injecting mocks in place of the concrete classes they depend on.
1
u/nickelghost Nov 12 '20
Can you point me to a resource explaining OOP in those languages?
1
u/Ran4 Nov 13 '20
Easiest is by far to follow a smalltalk tutorial or similar. There's a few really good ones, and a few hours of following one will be much more valuable than simply reading about it.
0
u/cyrusol Nov 12 '20
Not specifically. Personally I did and would recommend to just explore those languages on a weekend when nothing else goes on.
12
u/wsppan Nov 12 '20
Inheritance.
1
u/RumbuncTheRadiant Nov 13 '20
I would argue that Inheritance over use isn't the problem.... writing bugs is.
An inheritance hierarchy that violates the Liskov Substitution Principle isn't "Over using a language feature", it's a common or garden bug.
If it conforms to LSP, it isn't a problem, and removing the inheritance merely cuts off an opportunities for reuse.
2
u/xigoi Nov 13 '20
The problem is that LSP is very easy to violate even with an “intuitive” hierarchy.
https://en.m.wikipedia.org/wiki/Circle%E2%80%93ellipse_problem
1
u/RumbuncTheRadiant Nov 13 '20
The problem is that people don't think of it in terms of class invariants.
For every class you write you should write a class invariant check routine. (If you can't you have a PoD, not a class)
If it is a subclass, it should check it's own invariant AND invoke its parent's invariant check.
Now, for at very least the unit test builds, invoke the invariant check at the end of the constructor and the start of the destructor and at the start and end of every public method.
Because ultimately that's really the whole point of a class, an object instant is a bundle of state for which you can and should utterly rely on the class invariant holding.
Do this and the problem goes away.
1
u/Ran4 Nov 13 '20
For every class you write you should write a class invariant check routine.
No, that's... crazy. Using composition instead makes a lot more sense than simply writing more tests.
The main argument for inheritance is that it reduces the amount of code you need to write (to copy some behaviour), if you need to write a bunch of logic-style statements to prove the correctness of every class then you might as well not bother with inheritance.
1
u/RumbuncTheRadiant Nov 14 '20
The need to understand (and conversely rely) on your class invariant is orthogonal to inheritance.
Even if you purely use composition...
https://www.artima.com/intv/goldilocks.html#part3
Design by Contract is one of the most useful and practical tools a day to day practical industrial programmer can borrow from the academics.
1
u/wsppan Nov 13 '20
I agree mostly. My comment is more inline with the composition over inheritance principle as the better way; that classes should achieve polymorphic behavior and code reuse by their composition rather than inheritance from a base class. I'm finding the lack of inheritance and the introduction of traits and algebraic data types in Rust very refreshing.
32
u/coded_artist Nov 12 '20
Comments.
Often badly written comments are lies waiting to happen. This also happens with function names, but at least you can do code coverage tests.
The worst case of this is zombie code.
12
u/YMK1234 Nov 12 '20
Comments
problem is, people get taught to comment the wrong stuff (the what) instead of the important stuff (the why).
The worst case of this is zombie code.
Uh yeah nothing like a few hundred lines of commented out code.
9
u/coded_artist Nov 12 '20
Just make sure that 10 of those lines, disbursed randomly, are not commented and are functional
2
u/nathan_lesage Nov 12 '20
Commented out stuff is easy because you actually know that it‘s unused, but the worse is code that‘s still functional but never called — I get a small heartattack every once in a while when looking at an important function only to realise it‘s never being called
9
u/YMK1234 Nov 12 '20
That's especially funny in dynamic languages, or if there is reflection involved, where you never ever actually can be sure if not some maniac actually somehow manages to call that code.
0
u/onebit Nov 12 '20
Unit tests (mostly) solve that.
3
u/YMK1234 Nov 12 '20
Assuming you actually got decent coverage, and even then no. Stuff like that can easily be a problem that only happens in integration and in very obscure cases.
1
5
u/funbike Nov 12 '20
A comment should explain "why" something was done, not "how" or "what".
The code can speak for itself on how it works. However it can't always explain "why" you wrote it. This is especially true of workarounds.
And as for commenting out code, that's what git is for. Go ahead and remove the code and record the commit-id in a ticket for adding it back in.
3
u/Pan4TheSwarm Nov 12 '20
This is the best answer IMO right here. Nothing scarier than a misleading or obsolete comment in a big project
Clean code speaks for itself!
1
u/Python4fun Nov 12 '20
Even well written comments are lies waiting to happen. Badly written comments may be lies at the time of writing them.
Zombie code with an important sounding name can be horrible. It leads you to try changes that do nothing.
13
u/YMK1234 Nov 12 '20 edited Nov 12 '20
Across all languages, the IDE's copy-and-paste functionality. I've seen copy-paste coded with a tiny modification way too often, which should have simply been a function. But copy-pasting without thought is just too easy.
Heck I recently found an 8x copy-pasted DB query where the only difference were three parameters in the query.
if (param1)
if (param2)
if (param3)
[query with all 3 params]
else
[query with params 1 and 2]
else
if (param3)
[query with params 1 and 3]
else
[query with param 1 only]
else
[same if/else hell as above except without param 1]PS: and before anyone says it might not be possible otherwise ... it actually is. Most trivially:
q = [base-query]
if(param1)
q = q.filter(....)
if(param2)
q = q.filter(....)
[etc]6
u/deelyy Nov 12 '20
Hm. Funny. Sometime I prefer first long version.
Mainly because: sometime long duplicated code more easy to read and understand, and more easy to modify. Quite often I have no idea how exactly logic of these statements will be modified or updated later and quite often in the end second version become complex and spaghetti-like with a lot of nested if/then conditions again.
1
u/but_how_do_i_go_fast Nov 12 '20
I agree. I'm always saying to myself, "Just map, reduce, filter, find" but another for-i loop and pushing to some array seems easier to modify in the future. Hell, easier to modify while just debugging and reading the spec requirements again
4
u/YMK1234 Nov 12 '20
It's mainly what you are used to ... I find for-loops increasingly hard to read compared to well structured map/reduce code.
0
u/YMK1234 Nov 12 '20
Heck no, especially considering these queries are not trivial, and about 15-20 lines of shared joining and selecting.
in the end second version become complex and spaghetti-like with a lot of nested if/then conditions again.
so you are saying, because of bad coding pactices you end up with the 1st version and not even try ... that's a shite attitude. If the code later actually becomes more ugly, you can still refactor it to be better again.
3
u/deelyy Nov 12 '20
Hehe. Looks like you do not have any experience with financial systems or complex DMS?
Complexity of functionality will remain, no matter how we implement it. You can use complex SQL queries, multilevel if/then statements, multilevel objects structure, functional approach. Heh. You can even write you own scripting language or store all this complexity in JSON or XML or ini files. But main question will be the same: is it hard to read/understand your code now or in ten years? is it easy to modify your code?
And what I`m trying to say: sometime, sometime it better to not over shrink your code.
-1
1
u/Python4fun Nov 12 '20
Query builder with each param added
if (param1)can streamline that and remove a hundred lines of code with the initial query being in a single place.2
u/YMK1234 Nov 12 '20
well, SQLAlchemy in this case, but same applies. You can just slap on more filters and such until you actually enumerate over the result or similar. The person who wrote that left horrible code everywhere though so I'm not even surprised.
1
u/Python4fun Nov 12 '20
I'm sorry for your headaches. In this time of Covid, take solace in the fact that desk bourbon, while working remote, is not searchable by your employer.
10
u/andivx Nov 12 '20
Getters and setters. And Lombok, for the same reasons.
https://www.yegor256.com/2014/09/16/getters-and-setters-are-evil.html
I don't really agree with that article, but I think an awful lot of people put them by default, even when they are not gonna use them.
4
u/__2M1 Nov 12 '20
I think kotlin has solved this problem quite nicely by making them language Features, which can be omitted if not needed
1
2
1
u/funbike Nov 12 '20 edited Nov 12 '20
I've always wanted to write an app using "clean architecture" and ideas from the above article.
The idea is to cohesively keep each feature-set in a single package. Ever wonder why Java is package-private by default? Properties are private to the package, not the class. For example with MVC, the Controller and the View would have direct access to model properties. No getters/setters involved. These internal mechanisms would be not visible outside the package. One set of MVC objects could not communicate with the internals of another MVC objects, except as precisely defined.
The result is very few public methods, such as getters/setters. Strong cohesion. Less coupling. Fewer leaky abstractions. A much simpler API.
This would work better for a desktop app than it would for a SPA/REST app.
1
6
u/but_how_do_i_go_fast Nov 12 '20
Not a feature persay, but stack overflow and specifically a stranger's advice.
I've seen lots of stupid one liners that are trying to be clever, but just make things that much more difficult when we revisit the problem a week later. I've also seen these one liners posted all over SO.
The annoying part is where the SO answer itself is simply trusted with no testing. "It's got 355 votes, must be tried and tested".
Likewise, with asking stranger's: I'm a part of the vue discord, and there are multiple "MVPs" that answer with bad feedback. I had an async/await issue with emits, and a previous guy I knew I couldn't trust had left someone down the wrong path.
I'm sure doing feature requests per git fits in these categories too.
1
u/Ran4 Nov 13 '20
Introducing bugs instead of crashing early, by setting something to null (or similar) instead of crashing.
E.g. lots of people would start off writing a function like this:
def divide(a, b):
return a / bThen they'd run the program, and see that it crashes/raises an exception for b=0.
What they should be doing is... nothing. Sending in b=0 is arguably a programmer error, and crashing is the correct approach.
But what many people end up doing is
def divide(a, b):
if b == 0:
return None
else:
return aAnd now they've introduced a bug. Instead of crashing/raising an exception when a problem occurs, they just silently ignore it by returning a value - that will end up crashing something elsewhere.
The correct behaviour when your program stumbles upon something that isn't valid should almost always be to crash: the alternative is introducing bugs. And spontaneous crashes are almost always better than bugs, as they're much more noticeable and WAY easier to fix.
0
Nov 12 '20
[deleted]
1
u/MadocComadrin Nov 12 '20
Encapsulation isn't just OO, and polymorphism is a subset of subtyping.
1
u/SandyCoder Nov 12 '20
Could you elaborate on what you mean?
What else is Encapsulation and What is subtyping?
26
u/SV-97 Nov 12 '20
Mutable state... :D