The Big Programming Thread - Page 473
| 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. | ||
|
Nesserev
Belgium2760 Posts
| ||
|
tofucake
Hyrule19167 Posts
| ||
|
bangsholt
Denmark138 Posts
Sequence diagrams: This is very useful, when you're communicating with other services - then you can basically draw what happens when and why it happens. Alternative is to go to the source code and try to figure out what is going it - that's of course possible as well, but I would think it will take at least 3-4 times the time that it takes to look at the sequence diagram. It's also good for when you are designing - if your interaction between whatever two things you are drawing looks awkward... It's probably because it is, and you should try to redesign it into something that's prettier and easier to understand. Class diagram: A very nice and simple way to get an overview of a whole system, but where it really shines in my opinion, is when you are designing your systems - because you do design before you code, RIGHT? This makes it easy to whip up a design which you can then discuss with your fellow coders or engineers or whoever your team consists of. What happens if you have 20 classes? You make two diagrams or more. And added to this, most of the time the UML diagrams will always be slightly outdated in comparison to what is happening in the code, but you should strive to keep all your documentation to a high level and updated - it will make your life easier when you get back to something you wrote 5 months ago, and don't remember how to use. | ||
|
RoyGBiv_13
United States1275 Posts
On May 02 2014 01:27 tofucake wrote: MinGW is also run through msys though, which is the whole unix environment thingy. Cygwin compiles against Cygwin.dll and can cause some issues if other versions of it are present on the target system. MinGW compiles into the program so that doesn't happen, but it's more complicated. +1 for MinGW for your C++ compiler on windows. Alternatively, you can just download the eclipse IDE with the dev tools packed in. Ideally, use a Linux virtual machine for your C++ development. Using just the default programs installed with ubuntu is on par or slightly more friendly than trying to do development on windows. Additionally, Linux is incredibly convenient for things like version control, text manipulation, build options, etc... On May 02 2014 02:43 bangsholt wrote: To the UML discussion, let's dive into the two I've used the most. Sequence diagrams: This is very useful, when you're communicating with other services - then you can basically draw what happens when and why it happens. Alternative is to go to the source code and try to figure out what is going it - that's of course possible as well, but I would think it will take at least 3-4 times the time that it takes to look at the sequence diagram. It's also good for when you are designing - if your interaction between whatever two things you are drawing looks awkward... It's probably because it is, and you should try to redesign it into something that's prettier and easier to understand. Class diagram: A very nice and simple way to get an overview of a whole system, but where it really shines in my opinion, is when you are designing your systems - because you do design before you code, RIGHT? This makes it easy to whip up a design which you can then discuss with your fellow coders or engineers or whoever your team consists of. What happens if you have 20 classes? You make two diagrams or more. And added to this, most of the time the UML diagrams will always be slightly outdated in comparison to what is happening in the code, but you should strive to keep all your documentation to a high level and updated - it will make your life easier when you get back to something you wrote 5 months ago, and don't remember how to use. Well put. UML is just another tool in your bag for designing a program, but code is a mutating organism, and you'll quickly find that trying to keep your design documents in line with your code is a losing battle. | ||
|
whiteLotus
1833 Posts
| ||
|
tofucake
Hyrule19167 Posts
| ||
|
Fawkes
Canada1935 Posts
Two rows in a SQL table. User A logs in. User B logs in. A pulls up the table. B pulls up the table. A adds 4 rows to the table. B adds 6 rows to the table. B commits. C logs in and pulls the table. C adds 10 rows to the table. C commits. A rollsback. How many rows are in the table? My example might not work, I don't remember exactly what it was. My general questions are: 1. When commits happen, does it only commit that specific user's changes? Does this mean we lost A's row additions? 2. Does A's rollback do anything? I don't know how to word the questions exactly, I just wish to learn about the nature of commit and rollbacks in a multiple user scenario. | ||
|
delHospital
Poland261 Posts
But in general, the idea is that changes executed in one transaction are invisible to concurrent transactions. If you rollback, it's as if the transaction never happened. Therefore, two transactions executed concurrently (and committed) either work as if they happened one after another or, if what happened is inconsistent with this view, the second commit fails. | ||
|
Manit0u
Poland17495 Posts
On May 02 2014 03:42 RoyGBiv_13 wrote: +1 for MinGW for your C++ compiler on windows. Alternatively, you can just download the eclipse IDE with the dev tools packed in. Ideally, use a Linux virtual machine for your C++ development. Using just the default programs installed with ubuntu is on par or slightly more friendly than trying to do development on windows. Additionally, Linux is incredibly convenient for things like version control, text manipulation, build options, etc... Well put. UML is just another tool in your bag for designing a program, but code is a mutating organism, and you'll quickly find that trying to keep your design documents in line with your code is a losing battle. Best thing would be getting rid of Windows entirely (like I did 6 years ago) or dual-boot. Windows for games and .NET, Linux for everything else. | ||
|
delHospital
Poland261 Posts
On May 02 2014 06:44 Manit0u wrote: Best thing would be getting rid of Windows entirely (like I did 6 years ago) or dual-boot. Windows for games and .NET, Linux for everything else. I disagree. Linux in a VM is surprisingly usable. And dual-boot is surprisingly annoying. Whenever I tried it, after some time I got fed up with constant reboots, and ended up doing everything under Windows. | ||
|
RoyGBiv_13
United States1275 Posts
On May 02 2014 07:03 delHospital wrote: I disagree. Linux in a VM is surprisingly usable. And dual-boot is surprisingly annoying. Whenever I tried it, after some time I got fed up with constant reboots, and ended up doing everything under Windows. Get the best of both worlds, two computers. + Show Spoiler + windows gaming desktop + linux programming laptop is my home setup two desktop workstations, one of each, with synergy so they share a mouse and keyboard for my work setup. | ||
|
RoyGBiv_13
United States1275 Posts
| ||
|
Blisse
Canada3710 Posts
That said, I think once you've installed MinGW and its MSYS components, and you learn a bit about how to screw around with your environment PATH variable, you're in great shape for Windows only. There's not much else you need other than to replace the command prompt with either Git Bash << Console2 << ConEmu shell. Sometimes I prefer Linux because it feels a lot more hack-y than Windows, and using Debian flavours means I'm an apt-get away from downloading any new packages I need to help development, but usually Windows is popular enough to have a port available and sometimes I prefer having the installer do things for me (Windows has a huge leg up over Ubuntu because of this I feel). I'm a bit biased since I use Windows products more, but I'm much more effective doing things on Windows than I am somewhere else. I think if you can get over the initial entry barrier to Windows development it's actually pretty much on par with a lot of unix systems until you hit stumbling blocks because there's no Windows-variant of something you need, which isn't that common but is a bit annoying. You can say the same the other way though edit: forgot to say, piping makes things amazing, i really wish windows did it better o: and by better i mean at all' edit2: holy shit you can pipe in windows | ||
|
enigmaticcam
United States280 Posts
On May 02 2014 04:12 Fawkes wrote: Had a question on some SQL proficiency test I was doing for fun. I haven't had a chance to work with transactions to understand exactly how commits and rollbacks work with multiple users. Don't know it exactly, but it went something like... Two rows in a SQL table. User A logs in. User B logs in. A pulls up the table. B pulls up the table. A adds 4 rows to the table. B adds 6 rows to the table. B commits. C logs in and pulls the table. C adds 10 rows to the table. C commits. A rollsback. How many rows are in the table? My example might not work, I don't remember exactly what it was. My general questions are: 1. When commits happen, does it only commit that specific user's changes? Does this mean we lost A's row additions? 2. Does A's rollback do anything? I don't know how to word the questions exactly, I just wish to learn about the nature of commit and rollbacks in a multiple user scenario. My only experience is with Microsoft SQL server, and from that context the answer would be 16. User A's rows would be rolled back as one single transaction, but the other User's transactions would commit, effectively adding 16 rows. Things can get tricky when you start asking what the table values are during those transactions, and there is more than one way to define how you want that to work. But since you're only asking how many rows are in the table after everything is finished, there is no conflict between the transactions. | ||
|
Beamer
United States242 Posts
Let's say I have a Rectangle class with a constructor which takes in 2 double values, length and width. Also, I have a container class that is used to hold a bunch of Rectangle objects, called RectanglePack. This RectanglePack stores Rectangle objects in a vector. I want to add 10 Rectangles to RectanglePack. Here's one way I could do this: + Show Spoiler [Method 1] + // Method 1 In this case, RectanglePack.Add receives a Rectangle pointer and adds it to the vector like this: + Show Spoiler + void RectanglePack::Add(Rectangle* rectangle) Another way I could implement this is the following: + Show Spoiler [Method 2] + // Method 2 In this case, RectanglePack.Add receives an actual Rectangle object and adds it to the vector like this: + Show Spoiler + void RectanglePack::Add(Rectangle rectangle) One last way I could implement this is by using the following: + Show Spoiler [Method 3] + // Method 3 I think Method 1 and Method 3 are very similar. The only difference is that one vector holds the pointers to the Rectangle objects and the other vector holds the actual Rectangle objects. Also, Method 1 and Method 3 both have to worry about deallocating memory eventually. I think Method 2 is a little different in that it creates a copy of the Rectangle object in Rectangle::Add and pushes the copy into the vector. However, I'm not sure if my understanding is correct. Also, is there a certain convention for something like this? Is one way preferred over the other ways for some reason? | ||
|
RoyGBiv_13
United States1275 Posts
On May 03 2014 05:44 Beamer wrote: I'm trying to understand the underlying difference between these methods of populating a vector in C++: Let's say I have a Rectangle class with a constructor which takes in 2 double values, length and width. Also, I have a container class that is used to hold a bunch of Rectangle objects, called RectanglePack. This RectanglePack stores Rectangle objects in a vector. I want to add 10 Rectangles to RectanglePack. Here's one way I could do this: + Show Spoiler [Method 1] + // Method 1 In this case, RectanglePack.Add receives a Rectangle pointer and adds it to the vector like this: + Show Spoiler + void RectanglePack::Add(Rectangle* rectangle) Another way I could implement this is the following: + Show Spoiler [Method 2] + // Method 2 In this case, RectanglePack.Add receives an actual Rectangle object and adds it to the vector like this: + Show Spoiler + void RectanglePack::Add(Rectangle rectangle) One last way I could implement this is by using the following: + Show Spoiler [Method 3] + // Method 3 I think Method 1 and Method 3 are very similar. The only difference is that one vector holds the pointers to the Rectangle objects and the other vector holds the actual Rectangle objects. Also, Method 1 and Method 3 both have to worry about deallocating memory eventually. I think Method 2 is a little different in that it creates a copy of the Rectangle object in Rectangle::Add and pushes the copy into the vector. However, I'm not sure if my understanding is correct. Also, is there a certain convention for something like this? Is one way preferred over the other ways for some reason? Your understanding is nearly correct, and this link is relavent: http://stackoverflow.com/questions/655065/when-should-i-use-the-new-keyword-in-c Method 1&3 are semantically the same, and the rectangle variable will get optimized out by the compiler, with a pointer to the new rectangle being placed directly on the stack for the mRectanglePack.Add() function. Any time you see the "new" keyword, it's probably going to be placed on the heap (technically "free store"). As you mentioned, that memory will need to be cleaned up, so you may want to stick with Method 3, and bookkeep those pointers separately if you are uncomfortable deleting everything in the Vector. Method 2 is placing the rectangle objects on the stack, then passing by value. This creates another copy on the stack, for the Add() function. This function is going to create a third copy in push_back(). I have no clue how push_back stores the value, but let's assume is does the right thing and saves off the value passed to it on the stack so it doesn't lose it. Then the three copies will be popped off the stack as it goes back into the for loop. There is no convention for this, it's a judgement call. Just so long as you aren't doing:
| ||
|
nunez
Norway4003 Posts
re:interface in method 2 - if you use Rectangle const& in your add function for lvalues and an overload for with Rectangle&& for rvalue refs you might avoid unecessary copies (if your rectangle class does not have move-semantics the rvalue ref overload is pontless tough): #include<iostream> output: [jeh@gimli tl]$ ./tl when add is called with an rvalue reference (temporary variable) it moves the imba_t instead of copying! when add is called with a const reference it only does one copy. when add is called with argument by value it only does two copies, one to copy into function and one to copy into vector. instead of working with raw pointers you can use automatic memory management with unique_ptr or shared_ptr instead: #include<iostream> [jeh@gimli tl]$ ./tl they support copy / move semantics in the same way as the previous example (except now it's the pointer being copied or moved), and the destructor is automatically called when the reference count goes down to 0 (with shared_ptr). re:initalization i'd maybe use reserve and emplace_back, constructing the elements in-place bypassing copy/move all together and ~explicitly allocating the blob of memory the vector needs (imba_t ctor takes int): vector<imba_t> death_ball; or, if possible and your compiler is up to snuff use ctor number 7 from vector with an initializer_list (imba_t ctor takes int and char): vector<imba_t> death_ball({{0,'b'},{1,'c'},{2,'f'}});maybe make a ctor for your rectangle container that takes an initializer list for your rectangles as argument. if rvalue references are too daunting just stick to const&, an rvalue will bind to it so you can still write .add(imba_t()); except it will copy it into the vector instead of move. alternatively you could also use generate_n and a lambda or functor: int i=0,N=10; | ||
|
Cyx.
Canada806 Posts
edit2: So one thing - methods 1 and 3 aren't exactly similar. Add(new Rectangle)pushes back a pointer, while Add(*rectangle)pushes back a copy of the new rectangle you allocated. If the third method was written as Add(rectangle)ie. passing the pointer instead of dereferencing it, then they're functionally the same. And Method 1 won't be able to deallocate the memory once that loop is done - it pushes back copies, not the actual objects, so you'll be creating heap-allocated rectangles that you can't get rid of until your program dies. Method 2 is probably the most idiomatic - don't allocate memory manually if you don't have to =) | ||
|
Shield
Bulgaria4824 Posts
Basically it's like:
The idea is to just return
But how is this done? Regex/Pattern matching easily comes to mind, but I don't really understand it that well. I use Java, so an example for Java would be better. String#replaceAll("\\<[^>]*>","") only clears tags. | ||
|
phar
United States1080 Posts
http://stackoverflow.com/questions/6751105 You could probably use something like JAXP. If you can guarantee that there are no nested tags you could probably do it with regex. | ||
| ||