|
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. |
On December 28 2013 12:43 berated- wrote:Show nested quote +On December 28 2013 05:16 Yoshi- wrote:On December 28 2013 04:53 sluggaslamoo wrote:
Still destroys the 3-5 lines recommendation that I gave that certain people told me was impossible. T_T Are you trolling or that stupid? You said sth among the lines "your functions shouldn't be longer than 3-5 lines", and not "the average length of your function should be around 3-5 lines", if you write a getter/setter for every variable you obviously get an extremely low average. In many if/while expression you didn't used the brackets(which many people would most probably consider to be bad), if you would have written it in the "proper way" or if you ever need to add another statement to those structure, it would obviously inflate your average. If you would have used a different coding style like Allman, it would also have increased your line count. So yea your 3-5 lines recommendation is bullshit, since you 1) weren't talking about averages and 2) it heavily depends on the coding style. Or you used the ternary operator, many people would have used a normal if structure instead, since those are much easier to understand at the first look, but obviously much longer. And you were also saying that you shouldn't repeat yourself. public List<Cell> getColumn(int columnNumber) { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(columnNumber, i)); return row; } public List<Cell> getDiagonalRowTop() { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(i, i)); return row; } public List<Cell> getDiagonalRowBottom() { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(i, widthTiles-i-1)); return row; }
' You were going strong with the braces point, until... the don't repeat yourself is just a limitation of java imo. What you really want here (I believe) is the equivalent of filtering the list with a closure, but, good luck finding that in java. I guess you could create an interface that is is like CellFilter that has a single method acceptCell and then have a ColumnCellFilter(int column), TopDiagonalFilter, and BottomDiagonalFilter that is used to filter the list, but I'm thinking that an interface and 3 implementation classes to prevent looping over the same loop 3 times (with separate get cell criteria) probably isn't worth it. Maybe java 8. I still can't believe everyone is focusing on code complexity and not the fact that he uploaded his project using netbeans.
Lambdas, yield, map and collect would make it DRY. You wouldn't use the same methods though, you would yield cells from the board using a lambda and then in combination with another lambda which took parameters, you would pass in as a block based on the different permutations to solve it.
Nothing wrong with not adding braces, its the same as adding comments. Don't add them if you don't need them.
If you really find a fault with this because I used Netbeans then you're really just a troll. I use Linux/Xmonad and Vim, but I was rushed for time and thought it would be easier for him if I uploaded a Netbeans project. So I did it in Windows/Netbeans.
Also you guys need to upload your own code before you say anything.
|
On December 28 2013 05:16 Yoshi- wrote:Show nested quote +On December 28 2013 04:53 sluggaslamoo wrote:
Still destroys the 3-5 lines recommendation that I gave that certain people told me was impossible. T_T Are you trolling or that stupid? You said sth among the lines "your functions shouldn't be longer than 3-5 lines", and not "the average length of your function should be around 3-5 lines", if you write a getter/setter for every variable you obviously get an extremely low average.
In many if/while expression you didn't used the brackets(which many people would most probably consider to be bad), if you would have written it in the "proper way" or if you ever need to add another statement to those structure, it would obviously inflate your average. If you would have used a different coding style like Allman, it would also have increased your line count. So yea your 3-5 lines recommendation is bullshit, since you 1) weren't talking about averages and 2) it heavily depends on the coding style. Or you used the ternary operator, many people would have used a normal if structure instead, since those are much easier to understand at the first look, but obviously much longer. And you were also saying that you shouldn't repeat yourself. public List<Cell> getColumn(int columnNumber) { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(columnNumber, i)); return row; } public List<Cell> getDiagonalRowTop() { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(i, i)); return row; } public List<Cell> getDiagonalRowBottom() { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(i, widthTiles-i-1)); return row; }
How would you do it?
On November 12 2013 01:52 Yoshi- wrote:Show nested quote +On November 11 2013 07:30 sluggaslamoo wrote:
If you have any functions that are longer than 10 lines seriously have a look at them and see if you can't break them down. Most of my functions are 1-3 (ruby) lines long, this is due to functional programming though, I'd say for PHP they should float between 3 to 5 lines on average.
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
Would you like some cold water for your burn?
I really just think you don't know much about programming, keep limiting yourself and thinking everything is bullshit. And trolling one of the bigger contributors to this thread while contributing none of your own.
My average was 2.97, adding a brace using the standard method would maybe add 1 or 2 lines to some methods, a lot of them none at all. So 4-5? That still fits within 3-5 lines. Only two methods were longer than 5 lines as well.
By the way not once did I think about lines of code throughout, if you have good basics, that's what you'll get. Actually as I was rushing this, I was barely thinking at all, just sticking to basics. That's the entire point, functional decomposition.
Let me know if you want any programming lessons. 
|
Hi guys, in the midst of your blazing argument I know some of you like game engines, so I posting this here to grab your attention because I'm making one.
/me fade away ...
|
On December 28 2013 04:53 sluggaslamoo wrote:Show nested quote +On November 12 2013 01:52 Yoshi- wrote:You said sth among the lines "your functions shouldn't be longer than 3-5 lines", and not "the average length of your function should be around 3-5 lines" On November 12 2013 01:52 Yoshi- wrote:On November 11 2013 07:30 sluggaslamoo wrote:
If you have any functions that are longer than 10 lines seriously have a look at them and see if you can't break them down. Most of my functions are 1-3 (ruby) lines long, this is due to functional programming though, I'd say for PHP they should float between 3 to 5 lines on average.
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 Would you like some cold water for your burn?
zing.
okay for real, I'm pretty over it. Kind of excited for this though...
On December 28 2013 13:53 CecilSunkure wrote:Hi guys, in the midst of your blazing argument I know some of you like game engines, so I posting this here to grab your attention because I'm making one. /me fade away ...
I'll totally check it out, as you already know I learned a ton from your asciiengine ^^
e: whoa quotes were all fucked up
|
On December 28 2013 13:02 Zocat wrote:Show nested quote +On December 28 2013 10:46 mihajovics wrote:On December 28 2013 09:40 Millitron wrote:On December 28 2013 09:06 mihajovics wrote:On December 28 2013 05:40 Millitron wrote:On December 28 2013 05:21 mihajovics wrote:I'm in academia (linguistics) and I want to learn to code. I'm drifting towards a computer sciencey part of my field. Everybody's first recommendation is to learn python because you can do quick mockups, there are lots of existing libraries, etc. But I feel like on the long run I should learn C and some Computer Science in general (especially the math behind algorithms, etc). I'd be interested in some feedback. Thank you. EDIT: I have a basic background in C and python already, but I want to break out of the "look up every second command and syntax from the tutorial while coding" phase  I would talk to your school's CS department, and see if you could take Principles of Programming Languages and Theory of Computation, or whatever they're called at your school. Those are the two courses that involve the most linguistics stuff. You might have to take an Algorithms course as a prereq for Theory of Comp. If you're only interested in CS for the linguistics, you don't really need to learn any more coding than these courses require. If these courses aren't really an option, I highly recommend "Introduction to Theory of Computation" by Sipser. Great textbook, covers theory of comp really well, and a little PPL. I mainly need to code relatively simple, practical tasks, like grammar parsers, etc. to generate (or examine already existing) data... These vary a lot in the specifics, but in general all involve some simple database structure (tokenized text). I guess for these kinds of tasks python is great, because it's fast (to code and learn) and has a lot of out of the box solutions to fall back on. My aversion though stems from feeling like a cop out. The thing is that some newer theories are shifting towards "bigger picture" methods that involve stuff like neural networks, etc. So computational efficiency (might) actually start to matter. It just feels like that learning C would help one to grasp this whole "computer thing" so to say, how things actually work, and I guess that would help in all sorts of ways. Am I wrong on this? The other part I need to know, CS, I understand is basically completely separate from coding. It's the concepts behind several different technical implementations. Thanks for the tips, I'll sniff around for some classes next semester and till then I'll check out the book you recommended. Theory of Computation will have you work with the three big kinds of automata. Finite State Machines, Push-down Automata (also known as grammars), and Turing Machines. You'll learn what kinds of things each can and cannot compute, you'll definitely code a few FSM's and grammars, and you'll probably also code a few Turing Machines. My personal favorite parts of the class were when we had to code "simulators" for each kind of automaton. At the end of discussing one kind of automata, the prof would give us a file structure describing that automaton, and we had to code a simulator which would read the file, parse out the definitions of that particular automaton, and run it. For instance, for FSM's, the file would contain a list of states, a list of transitions, a start state, end states, and one or more sets of input. Our sim would read the file, build the FSM, and run it on the given input(s). The prof had a bunch of different finite state machines following the same format, which he would use to test our simulators. The FSM version of the assignment is pretty easy, its not much more than a linked list, but it can get pretty complicated for Push-down Automata and Turing Machines. I would say that any language will give you some idea of how computers work. C will show you the nitty-gritty details, while say python or java will hide the minute details and do a better job showing you the bigger picture. I don't really know anything about neural networks, suffice to say you will probably want to pick a language that focuses on parallelism, considering that, if my understanding is correct, each neuron is a simple computer, and they're all running simultaneously. Principles of Programming Languages should cover the concepts behind the technical implementations you mentioned. Unless by "technical implementations" you meant your PC, in which case you'll want to take Systems. I kind of already studied formal languages and automatons, but I could definitely use a refresh for sure. These approaches were extremely popular in the 60s and 70s for studying language. Really interesting topic! Ultimately all these approaches were unsuccessful, except for some very specific subsets of problems, hence all newer research is based on some sort of statistical, distributional model. But I'm getting off topic, thanks a lot for the input! I think I'll just go with whatever language has a course I can take. Formal languages & grammars are still important in the CS field. Since our programming languages are based upon those. So if you say to a CS guy "I need to do a grammar parser" they will answer accordingly. You're probably looking for stuff like language modelling (n-grams) with smoothing (Good-Turing, ....), Katz's back-off ... Look at Matlab or R. Both have toolboxes.
thanks, i'm familiar with both, and I use them when I need to do statistical calculations. for n-grams, I wrote my own python code though...  What I meant was that purely formal language approaches were unsuccessful... grammar parsers are still important and relevant.
|
On December 28 2013 13:35 sluggaslamoo wrote:Show nested quote +On December 28 2013 05:16 Yoshi- wrote:On December 28 2013 04:53 sluggaslamoo wrote:
Still destroys the 3-5 lines recommendation that I gave that certain people told me was impossible. T_T Are you trolling or that stupid? You said sth among the lines "your functions shouldn't be longer than 3-5 lines", and not "the average length of your function should be around 3-5 lines", if you write a getter/setter for every variable you obviously get an extremely low average.
In many if/while expression you didn't used the brackets(which many people would most probably consider to be bad), if you would have written it in the "proper way" or if you ever need to add another statement to those structure, it would obviously inflate your average. If you would have used a different coding style like Allman, it would also have increased your line count. So yea your 3-5 lines recommendation is bullshit, since you 1) weren't talking about averages and 2) it heavily depends on the coding style. Or you used the ternary operator, many people would have used a normal if structure instead, since those are much easier to understand at the first look, but obviously much longer. And you were also saying that you shouldn't repeat yourself. public List<Cell> getColumn(int columnNumber) { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(columnNumber, i)); return row; } public List<Cell> getDiagonalRowTop() { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(i, i)); return row; } public List<Cell> getDiagonalRowBottom() { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(i, widthTiles-i-1)); return row; }
How would you do it? Show nested quote +On November 12 2013 01:52 Yoshi- wrote:On November 11 2013 07:30 sluggaslamoo wrote:
If you have any functions that are longer than 10 lines seriously have a look at them and see if you can't break them down. Most of my functions are 1-3 (ruby) lines long, this is due to functional programming though, I'd say for PHP they should float between 3 to 5 lines on average.
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 Would you like some cold water for your burn? I really just think you don't know much about programming, keep limiting yourself and thinking everything is bullshit. And trolling one of the bigger contributors to this thread while contributing none of your own. My average was 2.97, adding a brace using the standard method would maybe add 1 or 2 lines to some methods, a lot of them none at all. So 4-5? That still fits within 3-5 lines. Only two methods were longer than 5 lines as well. By the way not once did I think about lines of code throughout, if you have good basics, that's what you'll get. Actually as I was rushing this, I was barely thinking at all, just sticking to basics. That's the entire point, functional decomposition. Let me know if you want any programming lessons.  ![[image loading]](http://4.bp.blogspot.com/_DYO8vtmkLco/SqTg0mP_HjI/AAAAAAAAAGU/CVNGW-YG65A/s320/adidas3.jpg)
I agree with your sentiment of splitting functionality into short functions that do one thing well, for the most part it is completely possible and good practice. Most (but not all) functions shouldn't be longer than 10 lines.
However there is code reuse in those three functions quoted. Since each function is creating a list, iterating through the tiles, adding cells to the list and returning it.
One could write a function which does the work described, and takes input telling it what parameters it should use with getCell while iterating through the tile space.
The result would be one slightly longer function which does the work of those three functions, and also has functionality that extends beyond them. (If you wanted to get tiles in a different manner, modify the parameters of this function rather than defining another function.)
EDIT:
On further reflection, passing in arguments to replace those functions is probably a bit of a stretch. You can still use a map function which iterates over widthTiles and pass in smaller functions that get the required cell. This would make your four getter functions one liners rather than 4-5 liners (Four lines is clearly too long for any function /s).
I don't write Java so unsure of implementing the mapping function as I've heard it doesn't properly support first class functions.
JS proof of concept:
var getDiagBottom = function(i) { return getCell(i, widthTiles - i - 1); };
var getDiagTop = function(i) { return getCell(i, i); };
// Returns a function which searches along a specific column. var getColumn = function(columnNumber) { return function(i) { getCell(columnNumber, i); }; };
// Takes an iterator function to build a list of tiles. var getTiles = function(fn) { var tiles = []; for (var i = 0; i < widthTiles; i++) tiles.push(fn(i)); return tiles; };
// getTiles(getColumn(1)); // getTiles(getDiagTop); // getTiles(getDiagBottom);
|
On December 28 2013 20:50 InvaderUK wrote:Show nested quote +On December 28 2013 13:35 sluggaslamoo wrote:On December 28 2013 05:16 Yoshi- wrote:On December 28 2013 04:53 sluggaslamoo wrote:
Still destroys the 3-5 lines recommendation that I gave that certain people told me was impossible. T_T Are you trolling or that stupid? You said sth among the lines "your functions shouldn't be longer than 3-5 lines", and not "the average length of your function should be around 3-5 lines", if you write a getter/setter for every variable you obviously get an extremely low average.
In many if/while expression you didn't used the brackets(which many people would most probably consider to be bad), if you would have written it in the "proper way" or if you ever need to add another statement to those structure, it would obviously inflate your average. If you would have used a different coding style like Allman, it would also have increased your line count. So yea your 3-5 lines recommendation is bullshit, since you 1) weren't talking about averages and 2) it heavily depends on the coding style. Or you used the ternary operator, many people would have used a normal if structure instead, since those are much easier to understand at the first look, but obviously much longer. And you were also saying that you shouldn't repeat yourself. public List<Cell> getColumn(int columnNumber) { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(columnNumber, i)); return row; } public List<Cell> getDiagonalRowTop() { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(i, i)); return row; } public List<Cell> getDiagonalRowBottom() { List<Cell> row = new ArrayList(); for(int i = 0; i < widthTiles; i++) row.add(getCell(i, widthTiles-i-1)); return row; }
How would you do it? On November 12 2013 01:52 Yoshi- wrote:On November 11 2013 07:30 sluggaslamoo wrote:
If you have any functions that are longer than 10 lines seriously have a look at them and see if you can't break them down. Most of my functions are 1-3 (ruby) lines long, this is due to functional programming though, I'd say for PHP they should float between 3 to 5 lines on average.
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 Would you like some cold water for your burn? I really just think you don't know much about programming, keep limiting yourself and thinking everything is bullshit. And trolling one of the bigger contributors to this thread while contributing none of your own. My average was 2.97, adding a brace using the standard method would maybe add 1 or 2 lines to some methods, a lot of them none at all. So 4-5? That still fits within 3-5 lines. Only two methods were longer than 5 lines as well. By the way not once did I think about lines of code throughout, if you have good basics, that's what you'll get. Actually as I was rushing this, I was barely thinking at all, just sticking to basics. That's the entire point, functional decomposition. Let me know if you want any programming lessons.  ![[image loading]](http://4.bp.blogspot.com/_DYO8vtmkLco/SqTg0mP_HjI/AAAAAAAAAGU/CVNGW-YG65A/s320/adidas3.jpg) I agree with your sentiment of splitting functionality into short functions that do one thing well, for the most part it is completely possible and good practice. Most (but not all) functions shouldn't be longer than 10 lines. However there is code reuse in those three functions quoted. Since each function is creating a list, iterating through the tiles, adding cells to the list and returning it. One could write a function which does the work described, and takes input telling it what parameters it should use with getCell while iterating through the tile space. The result would be one slightly longer function which does the work of those three functions, and also has functionality that extends beyond them. (If you wanted to get tiles in a different manner, modify the parameters of this function rather than defining another function.) EDIT: On further reflection, passing in arguments to replace those functions is probably a bit of a stretch. You can still use a map function which iterates over widthTiles and pass in smaller functions that get the required cell. This would make your four getter functions one liners rather than 4-5 liners (Four lines is clearly too long for any function /s). I don't write Java so unsure of implementing the mapping function as I've heard it doesn't properly support first class functions. JS proof of concept: var getDiagBottom = function(i) { return getCell(i, widthTiles - i - 1); };
var getDiagTop = function(i) { return getCell(i, i); };
// Returns a function which searches along a specific column. var getColumn = function(columnNumber) { return function(i) { getCell(columnNumber, i); }; };
// Takes an iterator function to build a list of tiles. var getTiles = function(fn) { var tiles = []; for (var i = 0; i < widthTiles; i++) tiles.push(fn(i)); return tiles; };
// getTiles(getColumn(1)); // getTiles(getDiagTop); // getTiles(getDiagBottom);
Yup exactly, not just map, but functional programming in general. Javascript is a functional programming language, lambdas, map, etc, are not available in Java, although you could use anonymous functions but it would look ugly as hell.
If I were to use a programming language that supported functional programming though, my code would look completely different from top to bottom, not just for row solving.
If I were to look at that example specifically, I might do it like this in Ruby. Off the top of my head so there might be some weirdness, but you get the general idea.
class Array def to_row #etc end
class Row def solved? #etc end
def check_results end_game if [ { |i| [0..3].map {|col| cells[col, i]} }, { |i| [0..3].map {|row| cells[i, row]} }, {|i| cells[i, i]}, {|i| cells[i, 3-i-1]} ].select { |proc| [0..3].map{|i| proc(i)}.to_row.solved? }.any? end
|
So I've been messing around with Clojure a little bit and I think I'm slowly starting to 'get' it. I'd appreciate any feedback on this simple L-System parser/renderer that I've ported over from Scala. The thing is that I know that some things are not as elegant/idiomatic as they could be (especially the step function in core.clj) but I don't know enough yet to make it look nicer. That said, Clojure is really fun to use. I really like the clean syntax.
|
How do you guys come up with good variable names? For example, I had to create a method to check if a row or a column is in bounds. However, I didn't know how to properly name the variable, so I just ended up with this name: "int rowOrCol".
Another question, if you type 0 as an array index instead of a constant variable (e.g. MINIMUM_INDEX), is that acceptable or is it still a "magic number"?
Edit:
One more question, is it possible to enumerate an ArrayList in Java by using just generics? I've tried something like:
for (T t : arrayList) { t.method(); }
It didn't work. As far as I know, it could work in Objective-C if I type something like:
for (id *object in arrayList) { [object method]; }
|
SOLVED the problem, scroll down if youre interested, ignore this post if not 
I finally got around to program my..thingy.. which can capture a screenshot from and send fake input to windows that arent in the foreground. I used the JNA library and it works but only kind of. Im gonna skip the capture part, because that works flawlessly, so theres no problem with getting the window etc. It just seems to be ignoring the input. Relevant code:
interface User32 extends com.sun.jna.platform.win32.User32 { User32 INSTANCE = (User32) Native.loadLibrary(User32.class, W32APIOptions.UNICODE_OPTIONS); LRESULT SendMessage(HWND hWnd, int Msg, WPARAM wParam, LPARAM lParam); } private static final User32 USER = User32.INSTANCE; public static void main(String[] args) throws IOException, InterruptedException { WinDef.HWND target = USER.FindWindow(null, "windowname"); USER.PostMessage(target, 0x0201, new WinDef.WPARAM(0x01), new WinDef.LPARAM(16 | 41 << 16)); Thread.sleep(50); USER.PostMessage(target, 0x0202, new WinDef.WPARAM(0x00), new WinDef.LPARAM(16 | 41 << 16)); }
Now if i try this with "TeamSpeak 3" it will successfully fake the input and push a button, but on another application it just gets ignored. It doesnt matter if i use SendMessage or PostMessage. I used Spy++ to confirm that the input is being sent to the right window and that the window received it. Heres the output:
![[image loading]](http://i.imgur.com/bbJjZ1J.jpg) Now there are a few more messages being sent with a manual input. After a bit of twiddling around i managed to reproduce them too.
WinDef.HWND target = USER.FindWindow(null, "windowname"); USER.SendMessage(target, 0x0084,null, new WinDef.LPARAM(16 | 41 << 16)); USER.SendMessage(target, 0x0020,new WinDef.WPARAM(0x001A067C), new WinDef.LPARAM(1 | 0x0201 << 16)); USER.PostMessage(target, 0x0201, new WinDef.WPARAM(0x01), new WinDef.LPARAM(16 | 41 << 16)); Thread.sleep(50); USER.SendMessage(target, 0x0084,null, new WinDef.LPARAM(16 | 41 << 16)); USER.SendMessage(target, 0x0020,new WinDef.WPARAM(0x001A067C), new WinDef.LPARAM(1 | 0x0202 << 16)); USER.PostMessage(target, 0x0202, new WinDef.WPARAM(0x00), new WinDef.LPARAM(16 | 41 << 16));
![[image loading]](http://i.imgur.com/aFbSI5v.png) But it still doesnt work. What am i doing wrong? PostMessage SHOULD work, because its what hotkeynet uses and it works there. http://hotkeynet.com/ref/sendwinm.html
SendWinM is used with background windows. It calls the operating system command PostMesssage which does not wait after each character for a response from the receiving thread. This makes SendWinM fast but possibly less reliable than SendWinS. Edit: for comparison, this is what it looks like if i use hotkeynet, to broadcast mouseclicks i do in a different window to eve while its not in the foreground:
![[image loading]](http://i.imgur.com/0DVv0HZ.jpg)
Edit: SOLVED By checking the hotkeynet output above i noticed that it sends a mousemove message first, which isnt necessary for a manual click. But if i want to fake it i have to move the mouse in position first, just click(postion) didnt work. So by adding
USER.SendMessage(target,0x0200,null,new WinDef.LPARAM(16|41<<16)); i managed to make it work!
|
On December 29 2013 04:40 darkness wrote:How do you guys come up with good variable names? For example, I had to create a method to check if a row or a column is in bounds. However, I didn't know how to properly name the variable, so I just ended up with this name: "int rowOrCol". Another question, if you type 0 as an array index instead of a constant variable (e.g. MINIMUM_INDEX), is that acceptable or is it still a "magic number"? Edit: One more question, is it possible to enumerate an ArrayList in Java by using just generics? I've tried something like: for (T t : arrayList) { t.method(); }
It didn't work. As far as I know, it could work in Objective-C if I type something like: for (id *object in arrayList) { [object method]; }
Yes its possible, maybe you aren't typecasting correctly? There's a few examples in the java code I posted on github before.
Nothing wrong with magic numbers if it is obvious to the programmer, such as 0. However the reason you want to avoid them is flexibility, if you want variable board sizing for example, your program won't work with magic numbers.
The method should return the condition of whether the object is in bounds, there's no need for a variable I don't think, if that makes any sense.
e.g
return index < rbound && index > lbound
Depends how complicated it is, or you could create a class which is a generalisation of row and column, and that can have a bounds method. Or if you mean the actual row or column has to be in bounds, then pass the generalisation into the container.
cellArray.inBounds(index)
or
container.inBounds(cellArray)
|
On December 29 2013 04:40 darkness wrote:How do you guys come up with good variable names? For example, I had to create a method to check if a rthere are only two hard things in computer science cache invalidation and naming thingsow or a column is in bounds. However, I didn't know how to properly name the variable, so I just ended up with this name: "int rowOrCol". Another question, if you type 0 as an array index instead of a constant variable (e.g. MINIMUM_INDEX), is that acceptable or is it still a "magic number"? Edit: One more question, is it possible to enumerate an ArrayList in Java by using just generics? I've tried something like: for (T t : arrayList) { t.method(); }
It didn't work. As far as I know, it could work in Objective-C if I type something like: for (id *object in arrayList) { [object method]; }
"There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors."
So, naming things is hard. Also, putting some named constant instead of 0 if you just want to access the first element of an array would just be confusing (something like MIN_IDX or whatever would imply to me that it's not 0).
If you want to loop over an array like you described, you do have to have the type variable T declared somewhere (either in a class or in a method). I just tried this and it worked as expected:
import java.util.ArrayList;
public class ArrayListTest { public static <T> void iterate(ArrayList<T> list) { for(T t: list) { System.out.println(t.toString()); } }
public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add(2); list.add(3); iterate(list); } } Output:
2 3
|
On December 29 2013 20:23 klo8 wrote: "There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors."
The two top sources of bugs in programming are concur buffer overfl*&^(%, rency, and off-by-one errors.
(not original, I'm not nearly that clever :p)
|
Programming newbie, making a Rubik's Cube solver, feeling really dumb right now.
I think I can manage to get the cube structure, the transformation subroutine, and solver algorithm going, but I don't know how the hell I should store the cubelet data, the data that tells me that the TL corner of the top face is connected to the TL of the left face and TR of the back face so that I can rotate them together and so on.
Right now, I'm using this and using split to retrieve the face + side/corner pairs:
my cubelets[6][9]; { cubelets[0][0] = ( ["1 0", "4 2"] ); cubelets[0][1] = "4 1"; cubelets[0][2] = ( ["4 0", "3 2"] ); cubelets[0][3] = "1 1"; cubelets[0][5] = "3 1"; cubelets[0][6] = ( ["1 2", "2 0"] ); cubelets[0][7] = "2 1"; cubelets[0][8] = ( ["2 2", "3 0"] );
cubelets[1][0] = ( ["0 0", "4 2"] ); cubelets[1][1] = "0 3"; cubelets[1][2] = ( ["0 6", "2 0"] ); cubelets[1][3] = "4 5"; cubelets[1][5] = "2 3"; cubelets[1][6] = ( ["4 8", "5 6"] ); cubelets[1][7] = "5 3"; cubelets[1][8] = ( ["2 6", "5 0"] );
cubelets[2][0] = ( ["0 6", "1 2"] ); cubelets[2][1] = "0 7"; cubelets[2][2] = ( ["0 8", "3 0"] ); cubelets[2][3] = "1 5"; cubelets[2][5] = "3 3"; cubelets[2][6] = ( ["1 8", "5 0"] ); cubelets[2][7] = "5 1"; cubelets[2][8] = ( ["3 6", "5 2"] );
...
But I'm pretty sure that this is either non-optimal or I might be approaching this problem completely the wrong way. Am I doing something wrong? Any advice on how to go about doing this?
e: Only on Layer 1 and it's already about 250 lines of code, I'm definitely screwing something up but may as well keep on going
|
Wouldn't using a 3*3*3 array containing the color of the faces do it ?
|
I have no idea why I can't find the answer anywhere, but how do you execute scripts in Python? I've used notepad to save lines of code in a .py file and then try calling it in the interpreter with "python myfile.py" and "$ python myfile.py" with no success. Stuck for hours, asking here as a last resort.
|
Anyone have advice on good books/pointers on designing stuff? Writing in object orientated languages, one thing I struggle with is how to design my classes. Things like: should this be static function or instance. Should this be in constructor or passed into function. Should I pass an object and act on a copy, or mutate the object that was passed in.
Also the same question about good references for how to set up relational database schema? e.g. surrogate keys vs natural keys.
One thing I was just working on was... + Show Spoiler + One thing I just worked on had records which had either a "rast" or "patric" type annotation. Each annotation type provided different information i.e. different columns. So for each record I stored "annotation type" as an "enum", and had 2 tables {rast, patric} annotation. My question in this case is twofold: 1) better to have split it into 2 tables or used one larger table (annotation) and nulled out columns as appropriate 2) better to use enum to describe annotation type or a values table (e.g. annotation type column => annotation_types table => "rast" | "patric". 3) and this might be mysql specific. Some of the columns in the annotation table are rather large - 500char+. When to use varchar(500) or "TEXT". Say the column "length" is 500+ characters. I've seen before comments that TEXT and BLOB columns can make table operations that don't even effect these columns really expensive. Should I have stored "length" in a seperate table, and had a lookup table from the main record to it?
The data seemed pretty relational but it feels weird to columns with so much text into a database.
I'm looking through Effective Java which I find pretty interesting, and it kind of covers questions of the second instance, but not really questions of the first instance.
|
On December 30 2013 06:01 _IPX wrote: I have no idea why I can't find the answer anywhere, but how do you execute scripts in Python? I've used notepad to save lines of code in a .py file and then try calling it in the interpreter with "python myfile.py" and "$ python myfile.py" with no success. Stuck for hours, asking here as a last resort.
If ">python myfile.py" doesn't work, is the "python.exe" in your PATH? I'm assuming you're on windows that is.
|
|
|
On December 30 2013 06:01 _IPX wrote: I have no idea why I can't find the answer anywhere, but how do you execute scripts in Python? I've used notepad to save lines of code in a .py file and then try calling it in the interpreter with "python myfile.py" and "$ python myfile.py" with no success. Stuck for hours, asking here as a last resort. You don't want to call it in the interpreter - you want to use that same command from the command line ^^
|
|
|
|
|
|