12 ms·
Stop writing classes...
- johnkchow 15y agoOne concern I have with Jack Diederich's video is that he never addressed one benefit of classes: better testability. In his Muffin example, he took out the instance variable that stored the API URL and created a constant out of it. If I had an URL for my staging environment, and another for my production environment, I'd like to programatically set that instead of going back into the code, modifying the constant value, run the test, and then change the value back to the original. That's just a bug waiting to happen. This isn't to say his underlying message is wrong (I 100% agree with Jack's message, too much classes create unnecessary complexity in your library/app), but I don't want people destroying their classes without fully understanding the consequences.
- quotemstr 15y agoYou can create a variable, set that, and pass that variable as the first parameter to greeting. I don't see why having a class makes testing any easier.
- radicalbyte 15y agoDecoupling. For example if your code requires access to an outside resource (database, message queue, email service) you can mock said resource in order to test the code in isolation.
- skrebbel 15y agoHey! Ho! Stop that. Nobody uses the word "Decoupling", I just learned that in the video. You must be pulling a fast one on me.
- tmorton 15y agoLet me clarify what I think you're saying. It's not about testing class X, it's about testing code that uses class X. As long as X is a class, then we can create a mock version and pass around instances of it. If our "X functionality" is stored somewhere else (ie a bare function), then that gets harder.
- quotemstr 15y agoDown that road lies the hell of dependency injection frameworks. A simpler approach is to use something like detours[1] to mock bare functions directly. [1] https://research.microsoft.com/en-us/projects/detours/ https://research.microsoft.com/en-us/projects/detours/
- Drbble 15y agoGoogle Guice is not hell.
- densh 15y agoIn python you can mock pretty much anything you want. You can mock package-defined variables by simply modifying them to the value you want (sys.modules['mymodule'].MY_GLOBAL_SWITCH = True). In such a way you can even mock standard library functions and built-ins (which have their own __builtin__ package). With such capabilities you can test pretty much any code. (There are some limitations indeed, like you can't change types which are defined in C code.)
- johnkchow 15y agoI agree that in Jack's specific examples you don't need the constructor: the constructor trivially saves the parameters into instance variables. However, one thing that hasn't been mention is when the setup of the class is non-trivial, which most likely requires breaking it down its helper methods that's only specific to that class. Exposing those helper methods outside of the class can potentially add as much unnecessary complexity to the code base (i.e. is this a helper method for another method or if this is standalone method, how does this fit into what I'm trying to achieve?). Yes, this could be mitigated with good documentation, but classes exist for this specific reason.
- quotemstr 15y agoHuh? Modularity has nothing to do with classes. In plain C, your main "method" can be a simple external function, and the "helpers" can be static functions. Python offers similar functionality. Making a class is a step backward because the internal helpers have to exposed to users, even if they're intended not to be used.
- Drbble 15y agoExposed how? Methods can be private.
- j2labs 15y agoJust use a settings file. I use settings overrides by putting local_settings.py in my .gitignore and then adding this to the bottom of my settings.py: https://github.com/j2labs/microarmy/blob/master/settings.py#L52 https://github.com/j2labs/microarmy/blob/master/settings.py#...
- astrofinch 15y agoWhy are you masking errors from that import?
- tawhaki 15y agoI assume it is so that the default values are used if no local_settings.py file exists, instead of aborting the program with an uncaught ImportError.
- pjscott 15y agoIf so, then for the benefit of any Python newbies here: "except ImportError" is the idiomatic way to handle this situation, it it clearly communicates intent, and prevents masking legitimate errors. One fairly common pattern is to try to import one module, and if it's not installed, to go for a backup. For example, if I need to parse JSON, the simplejson library is significantly faster than the (completely API-compatible) standard library "json" module: try: import simplejson as json except ImportError: import json I'm pretty sure I've seen those exact four lines of code in the internals of Tornado and Flask, among others.
- mdonahoe 15y agoexplicit is better than implicit He should explicitly catch the ImportError
- tkahn6 15y agoWhy not just pass the url into each function where it needs to be used?
- winter_blue 15y agoUhh.. that's ugly redundant code. Classes make code neater and easier to read..
- tkahn6 15y agoNot really redundant when you can curry the function when needed. This is how libraries in functional languages are written. Classes make code harder to reason about given that you have hidden state everywhere.
- kingkilr 15y agoDo you think a closure isn't state?
- jrockway 15y agoHe does, but he feels smarter if he emulates something everyone already does in a more obscure manner. Yesterday, I derived an n-algebra for feeding my cat. See how smart I am!? (The reality is simple. Classes are Python's state-capture abstraction. Using a bunch of lambdas to do the same thing means you are writing Haskell in Python, which is a dumb thing to do, because while the computer knows what you want, other programmers don't. If you want to use Haskell, use Haskell.)
- deleted 15y ago[deleted]
- tkahn6 15y agoOuch. No need to be condescending, Jon. When I write Python, I use classes and magic methods and all that jazz. Programming in a functional style in Python is often verbose and brittle due to the duck-typing. I was under the impression we were talking about programming in general.
- scott_s 15y agoI almost never write classes in Python. That may be a result of the fact that I never use Python for anything other than system automation and data post processing; I don't use Python to build complicated systems. What I have made use of are named tuples: 3DPoint = collections.namedtuple('3DPoint', ['x', 'y', 'z']) p1 = 3DPoint(3, 2, 4) p2 = 3DPoint(10, 1, 10) dist = math.sqrt((p2.x - p1.x)**2 + (p2.y - p1.y)**2 + (p2.z - p1.z)**2) I don't mean to say that classes aren't useful in Python, just that I've gotten a lot done in the language just depending on hashes, lists, tuples, named tuples, functions, comprehensions and generators. I've never felt the need to define a a class - the closest I came, I realized I wanted a named tuple. It certainly would make sense in the above example to define "dist" as a member function of a 3DPoint class, but it's just not how I write code in Python. Contrast this with C++, where I define classes all the time. I think the reason for this discrepancy is that if you want to do anything interesting in C++, you almost have to define a struct or a class; that's where most of the abstractions happen. In fact, if I want functional style code in C++, I have to define my functions as classes. I think that Python's native tuple types, first-class functions and comprehensions make the difference.
- quotemstr 15y ago> if I want functional style code in C++, I have to define my functions as classes Much of the benefit of C++11 and Boost is in not having to write functor classes.
- scott_s 15y agoLambdas buy us this in C++11 (which is awesome), but not so much with Boost alone. Boost can help if you are, say, applying the member function of a class to a bunch of objects; you don't have to write a speciality function object to express that. But if you're writing logic from scratch, and you want it to be a closure, it needs to be a function object.
- quotemstr 15y agoYes, but many function objects can be constructed easily enough with Boost.Lambda and related libraries.
- fuzzythinker 15y agoDisagree with "Decoupling" never used or "if used, they are pulling a fast one on you". It not only will help make his point of decreasing line-count, but more importantly, it is one of the main principles in achieving DRY-ness (both code-level and more importantly, architectural level DRY-ness). I'll even go further and say that if you're not thinking about it all the time, you're most likely doing it wrong.
- Maro 15y agoI think he didn't mean that it's never used, just that it's never actually said in the real world. My experience confirms this.
- fuzzythinker 15y agoYup, my bad, he actually said "people never say". But the way he presented it, seemed like he's opposed of it. But I still think it should be mentioned more in the industry so more developers are aware of it and think about it more often than not. EDIT: Can't reply to Tommabeeng's comment (probably too deep). Yup, and I agree with the presenter's main point - that classes are not necessary in many situations, even for decoupling.
- Tommabeeng 15y agoHe does say about coupling & cohesion that "if used, they are pulling a fast one on you". That's wrong. I like the key to his talk but the plague of large complicated systems is exactly coupling and incoherence. Classes are NOT necessarily the way to achieve these.
- drucken 15y agoAs others have implied already, he could just as easily have been referring to the words. I think his intention was to explain that these are the same words (mostly from academia and taken up with reckless abandon via language architectures like Java) that may be thrown around by some to create the code mess (anti-pythonic) in the first place.
- sukuriant 15y agoI think he missed one of my favorite uses of classes: a collection of implementations of defined methods, especially if you can't pass around function pointers. For example, you're working with a Reader and the Reader is something that needs to be able to read() and flush() and open() and close(). You use that Reader everywhere with those abilities. You don't care how they work, you just need for it to be able to do those four things. If you're expecting different Readers, it's useful to have a Reader interface and then classes that implement it. Unfortunately, this is the only way to perform this activity in Java. Also, I adore Go's style of pseudo-OO programming. .. or, from reading the HN threads, is that decoupling? If so, I'm using decoupling right now!
- ibejoeb 15y agoI think he addressed that (or a very similar circumstance) while talking about the stdlib heap, and again in response to the question on "bag of related functions." I don't think he'd take issue with your position. Alternatively, if you are working with things that you expect to follow a certain interface, Python does not really require a formal definition. Duck typing should suffice.
- lrobb 15y ago"I find OOP technically unsound. It attempts to decompose the world in terms of interfaces that vary on a single type. To deal with the real problems you need multisorted algebras - families of interfaces that span multiple types. I find OOP philosophically unsound. It claims that everything is an object. Even if it is true it is not very interesting - saying that everything is an object is saying nothing at all. I find OOP methodologically wrong. It starts with classes. It is as if mathematicians would start with axioms. You do not start with axioms - you start with proofs. Only when you have found a bunch of related proofs, can you come up with axioms. You end with axioms. The same thing is true in programming: you have to start with interesting algorithms. Only when you understand them well, can you come up with an interface that will let them work." - Alexander Stepanov.
- pjscott 15y agoIn case anybody was wondering: other things that Alexander Stepanov wrote, aside from that paragraph, include the C++ STL.
- JohnQPasserby 15y agoI agree with the statement about OOP, but fail to understand what one can proove when one has no axioms.
- Drbble 15y agoThe part about proofs and axioms is completely non-logical. for the past several centuries, proofs have relied on axioms. Euclid introduced axioms (postulates) in order to provide proofs (and precise formulations) for informal statements. So the math part is really more an argument in favor of classes as a sort of axiomitization of computing.
- pavpanchekha 15y agoThe majority of the proofs Euclid produced existed, both as theorem and proof, before his time. Similarly, few recent mathematical advances have started with formalizations and axioms. Calculus wasn't put on a rigorous basis until Lebesgue's time, hundreds of years after Newton and Leibniz. Category theory needed quite some work before it was formalizable. Differential geometry wasn't that rigorous until the twenties or so. Complex analysis was developed using many, many unnecessary assumptions because the formalities weren't there. I'm not picking-and-choosing math fields here; these are just the ones I'm familiar ones. It turns out, axiomatization and theory-building are two separate jobs rarely performed by the same mathematician. Usually, the theory-building makes the mathematician famous, and the axiomatization does not, but this is in many ways a tragedy. In any case, to the outsider of mathematics, it seems that axioms come first. This is false -- axioms come much later, because the work of axiomatizing isn't worth it before the theory proves itself useful. Axioms come first in a formal way, in the same way classes come first in a formal way. But the interesting thing isn't the class -- it's the instance. And in the same way, the axioms aren't interesting -- it's the resulting theories. I found the analogy very insightful.
- deleted 15y ago[deleted]
- donald_draper 15y agoI wish they told me that when I took inline lessons on class-class.org
- phzbOx 15y agoLook, there is nothing wrong with classes and OO in general. What is wrong is using OO when it is unnecessary and only adds layers of complexity.
- gruseom 15y agoWhat is wrong is using OO when it is unnecessary That's a platitude which everybody can and does say about everything. But I think one can make a case that there is something wrong with OO in general. The original arguments for OO were: it's better for managing complexity, and it creates programs that are easier to change. Both of these turned out not to be true in general. So we need to ask, when is OO a win? But for simpler problems it doesn't matter, because anything would work. So we need to ask: what are the hard problems that OO makes significantly easier? I don't think anyone has answered that. I suspect it's that OO is good when the problem involves some well-defined system that exists objectively outside the program - for example, a physical system. One can look at that outside thing and ask, "what are its components?" and represent each with a class. The objective reality takes care of the hardest part of OO, which is knowing what the classes should be. (Whereas most of the time that just takes our first hard problem - what should the system do? - and makes it even harder.) As you make mistakes and have to change your classes, you can interrogate the outside system to find out what the mistakes are, and the new classes are likely to be refinements of the old ones rather than wholly incompatible. This answer boils down to saying that OO's sweet spot is right where it originated: simulation. But that's something of a niche, not the general-purpose complexity-tackling paradigm it was sold as. (There's an interview on Youtube of Steve Jobs in 1995 or so saying that OO means programmers can build software out of pre-existing components and that this makes for at least one order of magnitude more productivity - that "at least" being a marvelous Jobsian touch.) The reason OO endures as a general-purpose paradigm is inertia. Several generations of programmers have been taught it as the way to program -- which it is not, except that thinking makes it so. How did it get to become so standard? Story of the software industry to date: software is hard, so we come up with a theory of how we would like it to work, do that, and filter out the conflicting evidence.
- rbarooah 15y ago
- kjlef 15y agoI'm not much of a python programmer, but in Java I use classes all the time. The main reason is that small classes make writing unit tests easy. I can use a mocking library like http://www.easymock.org/ http://www.easymock.org/ to mock out whatever dependencies the code I'm trying to test depends on. I agree on some of the presenters philosophical ideas, mainly that adding complexities into the codebase before they're needed is almost always a terrible idea, but saying that classes lead to complex code is simply not true, at least in my experience.
- aroberge 15y agoOf course you use classes all the time in Java: it forces you to do so by design. I have significantly more experience with Python than with Java and I find that writing unit tests on functions is as easy if not easier than writing tests for (small) classes. And Diederich's point about small classes being a code smell (my words - not his) is hard to refute.
- lrobb 15y agoAre java programmers the most egregious abusers of the Gang of Four? Twice now, I've been faced with a metric crap-ton of Java source files for reading / writing to a device. Anything you could possibly imagine, gets defined as a class. There's dozens of classes, and more SLOC than I can count.. Meanwhile, an equivalent C program comes in at 2k SLOC, and 4 files. You().Can('not').Do(any(thing)).Concise().In(java())! When I was primarily doing C++, I gradually switched to generic programming once visual studio supported STL... And then when I started using Perl I found I didn't need to write classes at all. Reminds me of this Paul Graham quote: "This practice is not only common, but institutionalized. For example, in the OO world you hear a good deal about "patterns". I wonder if these patterns are not sometimes evidence of case (c), the human compiler, at work. When I see patterns in my programs, I consider it a sign of trouble. The shape of a program should reflect only the problem it needs to solve. Any other regularity in the code is a sign, to me at least, that I'm using abstractions that aren't powerful enough-- often that I'm generating by hand the expansions of some macro that I need to write." http://www.paulgraham.com/icad.html http://www.paulgraham.com/icad.html
- omaranto 15y agoYou might enjoy Steve Yegge making fun of this aspect of Java culture, if you haven't this particular post before: http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom...
- lrobb 15y agoI had not seen that! Explains so much... In the neighboring programming-language kingdoms, taking out the trash is a straightforward affair, very similar to the way we described it in English up above. As is the case in Java, data objects are nouns, and functions are verbs. But unlike in Javaland, citizens of other kingdoms may mix and match nouns and verbs however they please, in whatever way makes sense for conducting their business. For instance, in the neighboring realms of C-land, JavaScript-land, Perl-land and Ruby-land, someone might model taking out the garbage as a series of actions — that is to say, verbs, or functions. Then if they apply the actions to the appropriate objects, in the appropriate order (get the trash, carry it outside, dump it in the can, etc.), the garbage-disposal task will complete successfully, with no superfluous escorts or chaperones required for any of the steps. There's rarely any need in these kingdoms to create wrapper nouns to swaddle the verbs. They don't have GarbageDisposalStrategy nouns, nor GarbageDisposalDestinationLocator nouns for finding your way to the garage, nor PostGarbageActionCallback nouns for putting you back on your couch. They just write the verbs to operate on the nouns lying around, and then have a master verb, take_out_garbage(), that springs the subtasks to action in just the right order.
- jakejake 15y agoOver-engineered code is frustrating to read and work with. If your project doesn't require classes then it's fine to not create a bunch of complexity with OO style and design patterns. But if you just dismiss the techniques because they're difficult or you don't want to spend time learning, then you're likely to re-invent the wheel over and over.
- aroberge 15y agoThat's not what the video was about at all. The speaker never implied to use an approach that would lead to reinvent the wheel over and over ... quite the contrary in fact.
- jakejake 15y agoWell, his message I think is easily translated into "classes suck - never use them" by those who may choose read into it that way. He also dismisses a few rather basic design patters as basically jokes (literally with laughs from the audience as if these ideas are idiotic rather than foundations of software design) without making the distinction that these concepts are actually really important. It's easy for people who are very smart and experienced to dismiss the rules - that applies for all things, not just programming. But if you never learn the basics then you'll stumble over silly things that have be solved by much smarter people.
- aroberge 15y agoStandard design patterns are important ... if you program in Java and similar languages. But many of these are completely unnecessary when using a dynamic language like Python - and many others.
- jakejake 15y agoTo me it's just simply a way to organize code, there's nothing inherently good or bad about classes. You can use or abuse them like anything else. It never hurts to know the basic GoF patterns and when to use them to simplify your code or solve a common problem. You can use them with or without classes. Most Javascript libraries that I look at today use closures, prototypes and anonymous objects that might as well be classes. Patterns seem to be making a huge appearance in client-side code in the last year. I'll admit that I don't know anything about Python, though.
- petercooper 15y agoI notice no mentions of Ruby yet, and I wonder if this discussion is pretty much impossible to have in the Ruby world, given Ruby is solely an object oriented language, even if you try to use it procedurally. Not writing classes would be against so many Ruby conventions and accepted style that it's a non-topic? Or is it that classes in Python specifically aren't that much of a win over the language's implementation of other paradigms? I sure haven't hit into any problems using classes or objects in Ruby, even when sometimes the use feels a little contrived to begin with, but.. I also have no choice :-) (maybe Rubyists have a Stockholm Syndrome with OO ;-))
- ryanf 15y agoI think part of the reason this doesn't come up in Ruby is that Ruby doesn't have Python's powerful namespacing system. In Python, it's reasonable to have a package with just functions in it, whereas in Ruby, writing a top-level method means polluting every object in the system. You can write modules that have singleton methods on them, but you still don't have anything as flexible as Python's "from pkg import foo, bar"—the caller needs to either write "ModuleName.my_function" at every call site, or use "include" and end up with the module's methods as part of the consuming class's interface.
- judofyr 15y agoImplicit self and late binding in Ruby makes it hard to implement a proper namespacing system without breaking other semantics :( After working with Perl (which has a similar namespacing), I must it's something I really miss when I come back to Ruby.
- heretohelp 15y agoEverything in Ruby that is an object, is an object in Python as well. It's more that Python programmers prefer not to proliferate excess complexity or layering. They hate ravioli code.
- jamie_ca 15y agoI think the appropriate translation would be: Python - take your __init__+1 method class and just make it a function Ruby - take your initalize+1 method class and just make it a static method (and maybe make that class a module instead)
- martininmelb 15y agoAs an 'end-user' of Python, I rarely find myself writing my own classes, but I am really pleased that the authors of many of the libraries that I use decided to write classes. I think the distinction between 'end-users' and people who make libraries is an important one and one that Jack seemed to overlook.
- swah 15y agoThis discussion would be much more constructive if we have an actual problem to code a solution for... then we could see the merits of each approach...
- ufo 15y agoOne thing that I still wonder about is, in cases where I need for some object to hold some state, whether it is better to use a class or a closure. Both approaches are similar in many ways but still subtly different. Is there a good rule of thumb, kind of like the one for classes vs tagged unions? (classes let you add new classes but don't like if you add a new method, tagged unions let you add new methods but make it hard to add a new type).
- lbolla 15y agoWhat surprises me is that no-one mentioned inheritance when speaking about classes. I am not a fan of classes and I prefer the functional approach, whenever possible. In my experience, I found that classes make sense as part of a hierarchy, that is, when subclassed to override/specialize methods or to describe interfaces, like type-classes in Haskell.
- cageface 15y agoDefining abstract datatypes and legal operations on those datatypes is useful whether you do it with objects or Haskell type classes or C++ generics. It's easy to go overboard with objects but explicit interface definitions are a lot easier to reason about then big grab bags of functions.
- daviddaviddavid 15y agoI dislike classes but I like bundling data along with methods which operate on that data. I often find that JavaScript's classless objects are a great solution. It's just so natural to be able to create an object with object literal syntax without first creating an ad hoc class and then instantiating it: var o = { name: 'David', greet: function () { console.log('Hi, I am ' + this.name); } };
- njharman 15y ago> bundling data along with methods which operate on that data What can you possibly believe a class is other than exactly that?
- haldean 15y agoPolymorphism and type hierarchies.
- daviddaviddavid 15y agoI realize that is one thing that classes offer. My point is that you can achieve exactly the same thing with objects rather than classes. Very often when writing Python (or whatever) you'll create a class which is only ever instantiated once and the instantiation is done simply for the benefit of calling one method. In such cases I find classes to be overkill. What I really want is the object. I don't need to factor the object's behavior out into a class. Many of my sympathies are expressed much more thoroughly and eloquently in the many papers you can find online by Walter Smith outlining the motivations behind NewtonScript.
- jmaygarden 15y agoWouldn't this be better? function greet(name) { console.log('Hi, I am ' + name); } var o = function() { greet('David'); };
- gruseom 15y agoIt may interest people to know that Alan Kay has said that they got granularity wrong with classes. Functions are a good granularity, and so are systems, but classes don't work. To get true encapsulation as he originally envisioned it, you need to think in terms of interoperating systems. The example he gave of getting it right is the internet (but not the web). This was all from Kay's talk last year in Germany.
- Alexandervn 15y agoDo you have a source (video, transcript) to that talk of Alan Kay in Germany?
- gg_ 15y agoI believe he is referring to this talk: http://tele-task.de/archive/video/flash/14029/ http://tele-task.de/archive/video/flash/14029/
- gruseom 15y agoYes thanks - and if anyone watches it, be sure also to check out the stuff on Bob Barton. I learned about a magnificent part of computing history thanks to that talk. Edit: here you go: http://news.ycombinator.com/item?id=2855500 http://news.ycombinator.com/item?id=2855500 http://news.ycombinator.com/item?id=2928672 http://news.ycombinator.com/item?id=2928672 http://news.ycombinator.com/item?id=2856567 http://news.ycombinator.com/item?id=2856567 http://news.ycombinator.com/item?id=2855508 http://news.ycombinator.com/item?id=2855508 The first two are teaser comments. The second two are the important pieces. Also the paper Kay praises at the start of the talk is short and very worth reading.
- Ingaz 15y ago>> true encapsulation as he originally envisioned it I suppose that "true" objects are Erlang processes. (I'm only partly joking) OOP gone wrong when it became structs with methods.
- danbmil99 15y agoThe best use of Python Classes is as a stand-in for Javascript-style object syntax, ala: class foo: pass f = foo() f.bar = "yes" which is easier to type and more readable than: f = {} f['bar'] = "yes" (credit where due: learned this trick from TB)
- hboon 15y agoWho or what is TB?
- stock_toaster 15y agoYou can simplify it a bit by building the container class on the fly, if desired. >>> x = type('Foo', (object,), {}) >>> x.bar = 1 >>> x.bar 1
- njharman 15y agoStop writing class hierarchies. Classes are fine. Over-engineering classes, like over-engineering anything is bad, m'kay?
- malkia 15y agoWish someone asked his opinion on "singletons". I've been fan of them in the past, and now I totally despise them... But then it came to me, that I don't despise the semantics of he singleton - I just write that as a bunch of functions, rather than class with methods that is "singleton-ized". There were also some revelations - like printer().getSingleton() (or instead of printer, put keyboard, mouse, memory, etc.) - at some point some of these "singletons" might be become "multi-tons" - for example gamepad().getSingleton() already is a trouble...
- Ingaz 15y agoThe best "singleton" for me is python module. Import is the constructor of singleton. Python itself controls single initialization of object.
- justncase80 15y agoTao of Leo #25: http://justinmchase.com/2012/02/22/tao-of-leo-25/ http://justinmchase.com/2012/02/22/tao-of-leo-25/ "The singleton pattern combines all the perf benefits of a global variable with all of the code maintenance benefits of a global variable."
- mclin 15y agoI agree with most of this issues, but it sounds like his editor doesn't have auto import and jump to definition like Eclipse. eg complaint about nested packages. (not that I agree that this is a necessary class): instead of MuffinMail.MuffinHash.MuffinHash, type MuffinHash and have your editor add: from MuffinMail.MuffinHash import MuffinHash.
- timruffles 15y agoTools to reduce the burden of tautology are workarounds. You can rely on tools to make the worst design/language less painful, but it's not an argument against avoiding tautology.
- eliben 15y agoI think most of the comments here are taking it too far. Not using classes in some cases is not equivalent to stopping using classes altogether. Classes are yet another tool in the programmer's toolbox. Sometimes the tool makes sense, sometimes it doesn't. The trick is to know when. But jumping to absolutes is surely not the right way to go about it.
- dragonbonheur 15y agoI don't understand. I started to program computers when I was 12. I used BASIC. And now they have conferences because they don't know how to write data structures and subroutines? No wonder software gets bloated...
- dragonbonheur 15y agoAnd just last week I read about a post where some guys were amazed that one coud write loops without for {;;} while {} or do { } while Facepalm.
- darylteo 15y agoFor awhile I did not really know how to take this whole thing. Upon reflecting, I realise I probably do lean away from Classes in general... it was strange to come upon that realisation, as I come from a strong Java/OOP background. The benefits of Classes are still related to data abstraction. But it is pointless if you are still accessing the data directly anyway (getters and setters be damned). So, I would only legitimately prescribe a class only when you are building a data structure. Nowadays, my classes are either - composition of primitive data (as I don't have tuples(python)/structs or dynamic objects (javascript)) - or function groups (like a business logic layer of some sort, which could have just as easily have been just bags of functions in modules). I'm going to be teaching my students Classes in a couple of weeks... food for thought before then.
- rurounijones 15y ago"Stop writing classes... " No Ooooh, you wanted to make a case for not using classes in certain situations? Then enough with the linkbait title.
- ObnoxiousJul 15y agoOOP obfuscate our programs by blurring the frontier between data and treatment. I can't agree more, since I made the mistake quite a few times myself.
- TeeWEE 15y agoSome arguments from this guy are: 1) Do not use a class if it doesnt have state Agreed 2) Keep it simple and do not think about the future, fix it later if needed He probably never wrote API code in libraries needed for other people. Once you choose something you are bound to it. In this case classes and interfaces are very helpfull. For example the API class example in the beginning (no state) is a good example. In the future the api might be extended with more functionality. The last thing you want is have 3rd parties rewrite their code cause you changed the API. So a class is good to provide a Facade (yes Facade Pattern). 3) package names are not for taxonomy but for preventing nameclashes True. However in Java the editor is so powerfull that you never ever have to lookup package names. This goes automatically (IDE parses the code on the fly). Java package names also use URI patterns like: com.google.android.hardware.vibrator. Which can only be used by somebody who has the google.com domain name. This prevents name clashes ALL the time. In python you sometimes have to rename a whole project. And the taxonomy gives you an indication what the code actually does. 4) Dont overuse exceptions and keep shorts names. Agreed. However i am not sure what the best granularity is. In a public Library API, i would be very carefull about this. Otherwise I agree with him.