The Big Programming Thread - Page 390
| Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
|
Manit0u
Poland17490 Posts
| ||
|
WolfintheSheep
Canada14127 Posts
On November 12 2013 02:37 Manit0u wrote: And remember, extends are evil. Interface inheritance > implementation inheritance. That's a rather limiting opinion. While it's important to know the difference between "is-a" and "can-do" relationships, saying one is evil is just flat-out wrong. | ||
|
misirlou
Portugal3241 Posts
On November 11 2013 11:18 ArchmageKruzer wrote: For science fair this year, one of my friends is trying to build a program to compare frequencies of chunks of various DNA codes (quite personally I don't really understand it myself, but it's basically you get ~ one million dna bases, and you pick out how many AGTGGACA are in there, rinse and repeat for a ton more chunks). So the obvious way is to just run a loop and compare the data, but that would take a long time as I understand it. Is there a more effective way to do this? (btw, I am really bad at coding. I am extremely good at logic stuff, but idk much syntax and terms and shit. i.e I'm in regular high school programming learning about java. we just got past karel, and are in the middle of if statements) please try not to use too much complicated syntax, or if you do, give me some sort of reference sheet so that I can look at it while I read you post. Ty! Not really string search related, but a BIG way to speed it up would be using multi threading. I have never made parallel code in Java (although i have created some seperate threads to handle http requests). Don't know if it is as simple as creating a thread and just running it or if something else is involved, have a look at it. From experience, C and openMP are plain simple to use. In your case, you want to analyze several subjects and very big strings that compose each subject. Either break up the string into the cores you have available, or have each core process a different subject. If you have an i7, thats already 8x faster than running it single core , and it's as simple as doing
There are other ways of doing it but I guess they're a bit harder than this. For example, you can use MPI to run it through multiple computers, say you have 10 computers at 2 cores each (normal school computers) that's 20 cores running your code ![]() | ||
|
Frigo
Hungary1023 Posts
On November 12 2013 02:37 Manit0u wrote: And remember, extends are evil. Interface inheritance > implementation inheritance. components > delegation > mixins > interfaces > inheritance | ||
|
Fawkes
Canada1935 Posts
| ||
|
klo8
Austria1960 Posts
On November 12 2013 01:52 Yoshi- wrote: Yea that is bullshit I all up for splitting code into smaller part, but saying that you should average between 3 to 5 lines is just ridiculous I agree. Saying that 3-5 lines (or even 10 lines) per method should be your average doesn't even make much sense because it completely disregards how complicated the thing is the method does. You can't really do the A* algorithm in 10 lines and if you break it up into chunks of 10 LoC long methods, it would hurt readability more than it would help. Also, in a lot of cases, more lines are actually more readable, especially in functional code where you might be tempted to do as much as you possibly can in one statement/expression, just because you can. (like chaining a map and a filter and a reduce in one line is cool when you're first learning it because you'd probably need at least 5-10 lines to do the same with loops, but IMO it hinders readability) | ||
|
sluggaslamoo
Australia4494 Posts
On November 12 2013 02:37 Manit0u wrote: And remember, extends are evil. Interface inheritance > implementation inheritance. Yes I remember that article, when I was a naive young child and all I did was Java I followed that mantra like a religion. Then 2 years later after learning a lot more, I realised it was a pile of crap written by a guy who just wanted to make a name for himself His idea leads to re-writing the same code over and over again with overly complex inheritance hierarchies, it just doesn't suit real world applications.A lot of the Java doctrines don't make a whole lot of sense, and is really just to get around the pitfalls of the language. Nevertheless, just because Java doesn't support implementation inheritance well doesn't mean we shouldn't use it. OO is about code re-use, and there is zero code re-use in interfaces. Really we should never use interfaces, unless we explicitly need polymorphism and we need to branch out to more than one "Generalisation", e.g for combining like objects in a collection. I'd say always favour implementation inheritance over interface inheritance, but not more than one level. Favour composition over inheritance. Although Java could just have mixins or traits and then we wouldn't have to worry about any of this crap. My favourite articles are the ones that start with "Why Java doesn't need X". Then proceeding to give denialistic reasons as to why X feature is such a "minor change at best" because Java can already do it by writing 100 lines more code. Yes Mr. Java expert, so can you tell me why my enterprise projects that are written in Java are 100x bigger than the (insert dynamic language here) projects that do the exact same thing? Oh I see, its a feature of Java. They are right in a sense, Java doesn't need any more features, as long as you think writing boiler plate is the best thing since sliced bread. | ||
|
sluggaslamoo
Australia4494 Posts
On November 12 2013 07:47 klo8 wrote: I agree. Saying that 3-5 lines (or even 10 lines) per method should be your average doesn't even make much sense because it completely disregards how complicated the thing is the method does. You can't really do the A* algorithm in 10 lines and if you break it up into chunks of 10 LoC long methods, it would hurt readability more than it would help. Also, in a lot of cases, more lines are actually more readable, especially in functional code where you might be tempted to do as much as you possibly can in one statement/expression, just because you can. (like chaining a map and a filter and a reduce in one line is cool when you're first learning it because you'd probably need at least 5-10 lines to do the same with loops, but IMO it hinders readability) Functional programming is only hard to read when you don't understand it, otherwise its easier to read. Its almost like saying we shouldn't use object orientation because it makes the code more complicated than doing things in a structural (procedural) manner. On November 12 2013 02:23 spinesheath wrote: Breaking code up into smaller parts only makes sense if you can come up with good names for these parts. If you functions are small but their names don't clearly describe what they do, you made your code worse by splitting it up. If you follow the proper principle of functional decomposition this will never happen. On November 12 2013 01:52 Yoshi- wrote: Yea that is bullshit I all up for splitting code into smaller part, but saying that you should average between 3 to 5 lines is just ridiculous "I don't know how to do it, therefore its bullshit" You guys aren't trying hard enough. I always check the commit log and whenever a developer writes a function that is more than 10 lines long I can almost always find a way to break it up into more re-useable functions or find a better way to write it, this then cascades down into being used into his other functions and before you know it I end up deleting a hundred lines of code. Most people just don't know how to break things down efficiently. I see this problem in almost all projects I start work in, I can immediately see far better abstractions but its just not worth fixing it unless there is very high test coverage. If I had some sort of way to calculate lines of code per method in any of my contract projects you would get an average of 3 lines per method. Most of my model methods are a single line long, my controller methods about 3-5 lines. It makes it extremely readable and maintainable. D.R.Y my friend ![]() | ||
|
KaiserJohan
Sweden1808 Posts
| ||
|
sluggaslamoo
Australia4494 Posts
On November 12 2013 07:16 Frigo wrote: components > delegation > mixins > interfaces > inheritance mixins are really a form of inheritance that fulfills the role of components + delegation ![]() Therefore if you have access to mixins you really aren't gonna use delegation, components or inheritance. While it is still possible to use, whenever I have used composition, delegation or inheritance in the past I always ended up deleting it later and just used a mixin. I assume you meant components as in a library of functions, such as helpers. I definitely use a lot of composition for models but that is kind of a given. Unless you meant components as in external libraries, though funnily enough, often they just require you to use a mixin. Usually languages that support mixins have duck-typing making interfaces kind of redundant, you can still use them but only if you're a design nazi. | ||
|
Shield
Bulgaria4824 Posts
On November 12 2013 08:17 KaiserJohan wrote: It feels like a natural progression for a programmer is to start in merry OOP-land and then as you grow slowly realise how ugly it is and transition into more functional languages and paradigms Someone listens too much to Dijkstra and co. ![]() | ||
|
Shikada
Serbia976 Posts
On November 12 2013 08:17 KaiserJohan wrote: It feels like a natural progression for a programmer is to start in merry OOP-land and then as you grow slowly realise how ugly it is and transition into more functional languages and paradigms It should be a natural progression to get at least interested in the functional style of programming, learn its advantages and disadvantages and then later use whichever style is superior to your task. People disgruntled with the verbosity of Java will fall in love with dynamic languages, people that are more mathematically inclined love functional languages, but in the end, the best programmers choose their tools based on merits that are much less emotionally charged. | ||
|
wherebugsgo
Japan10647 Posts
On November 12 2013 08:13 sluggaslamoo wrote: Functional programming is only hard to read when you don't understand it, otherwise its easier to read. Its almost like saying we shouldn't use object orientation because it makes the code more complicated than doing things in a structural (procedural) manner. If you follow the proper principle of functional decomposition this will never happen. "I don't know how to do it, therefore its bullshit" You guys aren't trying hard enough. I always check the commit log and whenever a developer writes a function that is more than 10 lines long I can almost always find a way to break it up into more re-useable functions or find a better way to write it, this then cascades down into being used into his other functions and before you know it I end up deleting a hundred lines of code. Most people just don't know how to break things down efficiently. I see this problem in almost all projects I start work in, I can immediately see far better abstractions but its just not worth fixing it unless there is very high test coverage. If I had some sort of way to calculate lines of code per method in any of my contract projects you would get an average of 3 lines per method. Most of my model methods are a single line long, my controller methods about 3-5 lines. It makes it extremely readable and maintainable. D.R.Y my friend ![]() No offense, but you sound like a pain in the ass to work with. As other people have said, you can't just make the generalisation that all functions can be broken down into auxiliary functions of size X, or that even if they could, that it would necessarily be better. I can even think of examples right now that seem mathematically quite simple but are ugly as fuck if you try to decompose them too much. For example, writing a ray-object intersection test for a raytracer. Writing various prime factorization algorithms (e.g. Shanks or Dixon's). Writing anything with conditional cases in which calling auxiliary functions spreads the code too much-readability there comes from personal preference, really. If each auxiliary method is only one line or two lines long then you might as well leave it in the original function without separating it in the first place (since by making it auxiliary you need to define it somewhere else, adding lines and spreading out the code, making it more disorganised) | ||
|
sluggaslamoo
Australia4494 Posts
On November 12 2013 10:20 Shikada wrote: It should be a natural progression to get at least interested in the functional style of programming, learn its advantages and disadvantages and then later use whichever style is superior to your task. People disgruntled with the verbosity of Java will fall in love with dynamic languages, people that are more mathematically inclined love functional languages, but in the end, the best programmers choose their tools based on merits that are much less emotionally charged. I've realised that this only applies in theory. If it was the case, everyone would be using Erlang for concurrency, and Smalltalk for object orientation. While Node and Ruby are the children of these languages respectively, most of the time languages are chosen not to fit the appropriate problem domain. Given your principle, we should never have used Java/C++, ever. It should have been Smalltalk for business applications. In reality there is no such thing as a toolbox of languages which you choose fit for the task. Programming languages are not cars. Its not like, oh this is an X problem domain I should use PHP, and for Y I should use Rails, and for Z I should use Java. It just doesn't work that way. If you have the budget, and you fork out for the best developers and they will probably choose to write the app in Scala or Erlang or something. For a high-medium sized budget, Ruby/Node/Python, for a small budget Java/PHP. In the end, there are languages that are just flat-out better than others in every conceivable way, and the only reason for not using them is because your developers don't know how to, or you can't afford developers that can. Hypothetically with an unlimited budget, there is never a time where Java would be the best choice. I can only say different for AAA title games, but this has more to do with corporate dogma, just like how most corporations use Java/.NET for applications. Most indie games are not created using C++, hackathon winners are often in Javascript, Minecraft was created in Java but there was nothing stopping the developer from using JRuby and completing it in a much shorter time-frame. This never stopped a huge amount of people from buying and playing these games. Technically AAA title games "should" be written in C++ in order to work on as low-spec machines as possible. However the irony is that because customer PC specs are so high now, we no longer bother to use the optimisations that C++ gives us because of development costs. In which case we may as well not be using C++ anyway. Starcraft is a good example of this, it obviously has some very complex OO for its map-editor, it also uses garbage collection for its scripts, however in the past developers chose not to use OO or GC in AAA games because it would take more processing power. ---- I was never disgruntled with the verbosity of Java until I tried dynamic languages, I was never mathematically inclined but I love functional programming. Going back to Java is like going back to an old game you loved and realising its now unplayable. Keeping code concise is always going to be every programmers goal, I think the reason people stick with certain languages has more to do with the Peter Principle (generalised form of). People often tell me how certain features are unnecessary and just make things more complicated, until I ask them to implement something which took them 30 lines, and then I show them an example in 2 lines, not to mention in a lot shorter timespan. You can imagine the cascade effect this would have going through an entire project. I am even finding this even for myself. There are certain things I've seen developers do that are beyond the realm of my imagination (genetic algorithms for websites anyone?), they also program in much more difficult languages than I do. Some of them tell me they don't like Ruby but I can never understand why, but I'm sure its the same for me talking to a Java developer. Sure you can argue however you want about how your language does whatever you want, but only because you have never progressed to being able to solve more difficult problems. | ||
|
phar
United States1080 Posts
On November 12 2013 08:13 sluggaslamoo wrote: "I don't know how to do it, therefore its bullshit" You guys aren't trying hard enough. I always check the commit log and whenever a developer writes a function that is more than 10 lines long I can almost always find a way to break it up into more re-useable functions or find a better way to write it, this then cascades down into being used into his other functions and before you know it I end up deleting a hundred lines of code. Most people just don't know how to break things down efficiently. I see this problem in almost all projects I start work in, I can immediately see far better abstractions but its just not worth fixing it unless there is very high test coverage. If I had some sort of way to calculate lines of code per method in any of my contract projects you would get an average of 3 lines per method. Most of my model methods are a single line long, my controller methods about 3-5 lines. It makes it extremely readable and maintainable. D.R.Y my friend ![]() It could be that the things you are doing are quiet different. Not everything can be prettily broken up into short 3-5 line functions. | ||
|
phar
United States1080 Posts
On April 30 2013 10:57 sluggaslamoo wrote: As a whole, I'd say 95% of all developers in the world are absolutely atrocious because of the reason you gave. A very tiny fraction of developers actually know how to write good code and engage in computer science. I do not work with those kinds of developers and neither do my workmates because they are simply a waste of time. I don't know why this is my loss, I get paid a substantially higher amount compared to your developers and get to work in a really good environment because of said reasons. Developers like yours would not even pass the interview of some of the companies I've worked in. | ||
|
Cyx.
Canada806 Posts
So did like three whole days of posts get burned out of this thread, or is that from somewhere else? Because I went looking for context on that quote and there is nothing for the day before that date to the day after that date in this thread... and now I feel like I missed a good flame war | ||
|
phar
United States1080 Posts
On November 12 2013 11:41 Cyx. wrote: So did like three whole days of posts get burned out of this thread, or is that from somewhere else? Because I went looking for context on that quote and there is nothing for the day before that date to the day after that date in this thread... and now I feel like I missed a good flame war Sorry, unrelated thread from awhile back. http://www.teamliquid.net/forum/viewmessage.php?topic_id=410026¤tpage=18#341 | ||
|
sluggaslamoo
Australia4494 Posts
On November 12 2013 11:12 wherebugsgo wrote: No offense, but you sound like a pain in the ass to work with. As other people have said, you can't just make the generalisation that all functions can be broken down into auxiliary functions of size X, or that even if they could, that it would necessarily be better. I can even think of examples right now that seem mathematically quite simple but are ugly as fuck if you try to decompose them too much. For example, writing a ray-object intersection test for a raytracer. Writing various prime factorization algorithms (e.g. Shanks or Dixon's). Writing anything with conditional cases in which calling auxiliary functions spreads the code too much-readability there comes from personal preference, really. If each auxiliary method is only one line or two lines long then you might as well leave it in the original function without separating it in the first place (since by making it auxiliary you need to define it somewhere else, adding lines and spreading out the code, making it more disorganised) Who is more of a pain, someone who cleans up code, or someone who comes up with obscure examples to try to prove someone wrong? I have never had anyone call me a pain for cleaning up their code, in fact they are often surprised and happy they learned something and I get thanked for it. How many times are you going to use these algorithms (not to mention combinations of these) in a normal application, maybe... never? One of the projects I am working on involve very very complex algorithms as it is a cost estimator for hvac systems. It generates every possible hvac system from a hundred different parts and estimates things like electricity consumption, pollution, etc, and ranks them on the basis of the best solution for particular requirements which also get factored into the calculation (size of building, etc). It then has a bunch of sorting/grouping algorithms because you get 1000+ results at a time, and you need something to help you sift through them. My model methods are mostly 1 line (even with complex calculations), and other files are maybe 3-5 lines? Ill give a real example. A solution is a combination of a heating and cooling system, you have hundreds of each. Each system is a combination of a multitude of different parts, distribution, control and plant. You need to generate a system for every permutation of these parts, and then a solution for every permutation of these systems. Think about how you would implement combining a list of all combinations of parts to form a system, and combining every single permutation of these systems into a solution with 3 lines per method. For the sake of the example, because generating the systems is basically the exact same implementation as combining the systems together, just do the last bit.
I can imagine a million developers write now writing a multitude of loop statements and if-statements inside a single method to do the same thing. Also is it really hard to read or "decomposed too much" like you mentioned? If you are going to make the statement that you can't understand what it does, well would you say them same to a person who only understands procedural programming to your OO code? To anyone that is good at Ruby, they could tell you what it does in an instant, much faster than it would take to read through multitudes of loop statements. This is no big deal either, its a very trivial example, I could give you a bunch more complicated examples where I am doing the complex math and processing and the code is very concise but I had to choose one that was general enough and doesn't give too much away. Apart from the fact that I completely disagree and there is a way to make any algorithm much more concise (seen quick-sort written in one line?), and apart from the practicality of writing these yourself rather than just using a library, I'd have to say that my argument does not really apply to any of your examples. In a normal project you aren't going to write a complex algorithm for every... single... method, how many times are you going to write raytrace and pathfinding algorithms? (honestly if you have to write an A* algorithm more than once in a project your doing it wrong), its obviously a damn guideline and not one that applies to a physicist working for NASA. Or you just completely misunderstood what I said. Also tests are not methods or functions. Yes my test code is big, but it has nothing to do with what I said. | ||
|
Zocat
Germany2229 Posts
On November 12 2013 12:04 sluggaslamoo wrote: Ill give a real example. A solution is a combination of a heating and cooling system, you have hundreds of each. Each system is a combination of a multitude of different parts, distribution, control and plant. You need to generate a system for every permutation of these parts, and then a solution for every permutation of these systems. lol "permutation" | ||
| ||
, and it's as simple as doing
His idea leads to re-writing the same code over and over again with overly complex inheritance hierarchies, it just doesn't suit real world applications.