|
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 February 06 2015 07:02 obesechicken13 wrote: Setting: I had an interview question yesterday in C#, for .net
The guy asked me to fill in pseudocode for a login.
My Answer: I just verified the password, then you had to make an update and an insert. At first I didn't get what was so special about the question, but he hinted that maybe one of the queries would fail. So like with a banking transfer, you'd want to make sure either both queries (withdrawl from your account, deposit to another account) worked or that neither did. How would I handle that?
I eventually answered rollbacks, but I had never used transactions with sql querieis so I didn't mention anything about transactions but he still seemed to be like "alright, at least he knows to use rollbacks".
Is there a better way? Do you need try catch exception handling or is there a better way to check if queries don't work properly?
The database type would be MSSQL but I doubt it'd matter.
This property is called "atomicity" and is part of ACID: http://en.wikipedia.org/wiki/ACID#Atomicity
Here is a good example: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.commit(v=vs.110).aspx
You commit a transaction when no error occurs.
|
On February 06 2015 07:15 bangsholt wrote: First query... Well, that's binary, in the sense that you're comparing a username and a hash to a username and a hash. If that fails, you don't log the user in.
Second and third query you wrap in a transaction, which you would do with the SqlTransaction class in C# + MSSQL - then when you commit - i.e. you've reached the point where you want to know if everything is consistent or not, then you do a try catch on that - and if the commits fails, you rollback and tell the user it didn't go as expected. Thanks.
Followup question:
A transaction cannot be rolled back after a COMMIT TRANSACTION statement is executed, except when the COMMIT TRANSACTION is associated with a nested transaction that is contained within the transaction being rolled back. In this instance, the nested transaction will also be rolled back, even if you have issued a COMMIT TRANSACTION for it. https://msdn.microsoft.com/en-us/library/ms181299.aspx
I understand there's all this theory as to what changes can be rolled back based on atomicity. But why does microsoft say that the rollback can't be done if the commit is executed? Does a failed commit not execute? edit: nvm
On February 06 2015 07:23 darkness wrote:Show nested quote +On February 06 2015 07:02 obesechicken13 wrote: Setting: I had an interview question yesterday in C#, for .net
The guy asked me to fill in pseudocode for a login.
My Answer: I just verified the password, then you had to make an update and an insert. At first I didn't get what was so special about the question, but he hinted that maybe one of the queries would fail. So like with a banking transfer, you'd want to make sure either both queries (withdrawl from your account, deposit to another account) worked or that neither did. How would I handle that?
I eventually answered rollbacks, but I had never used transactions with sql querieis so I didn't mention anything about transactions but he still seemed to be like "alright, at least he knows to use rollbacks".
Is there a better way? Do you need try catch exception handling or is there a better way to check if queries don't work properly?
The database type would be MSSQL but I doubt it'd matter. This property is called "atomicity" and is part of ACID: http://en.wikipedia.org/wiki/ACID#AtomicityHere is a good example: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.commit(v=vs.110).aspxYou commit a transaction when no error occurs Thanks
|
On February 06 2015 07:25 obesechicken13 wrote:Show nested quote +On February 06 2015 07:15 bangsholt wrote: First query... Well, that's binary, in the sense that you're comparing a username and a hash to a username and a hash. If that fails, you don't log the user in.
Second and third query you wrap in a transaction, which you would do with the SqlTransaction class in C# + MSSQL - then when you commit - i.e. you've reached the point where you want to know if everything is consistent or not, then you do a try catch on that - and if the commits fails, you rollback and tell the user it didn't go as expected. Thanks. Followup question: Show nested quote +A transaction cannot be rolled back after a COMMIT TRANSACTION statement is executed, except when the COMMIT TRANSACTION is associated with a nested transaction that is contained within the transaction being rolled back. In this instance, the nested transaction will also be rolled back, even if you have issued a COMMIT TRANSACTION for it. https://msdn.microsoft.com/en-us/library/ms181299.aspxI understand there's all this theory as to what changes can be rolled back based on atomicity. But why does microsoft say that the rollback can't be done if the commit is executed? Does a failed commit not execute?
And that's the D from ACID I guess:
Durability Main article: Durability (database systems)
Durability means that once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors. In a relational database, for instance, once a group of SQL statements execute, the results need to be stored permanently (even if the database crashes immediately thereafter). To defend against power loss, transactions (or their effects) must be recorded in a non-volatile memory.
|
And to answer your other question about using try-catch for that: It's pretty widely used. You try-catch the query and use finally for rollbacks and other cleanup, just to make sure you didn't leave any mess in the DB.
Edit: Just make sure you don't call the same methods in the catch and finally blocks. Not sure about C# but in Java you really need to be careful.
Guess what will be returned here?
public class ExceptionTest { public int returnValue(int x) { System.out.println("Returning value: " + x);
return x; } public int testException() { try { throw new Exception(); } catch (Exception e) { return returnValue(1); } finally { return returnValue(2); } } public static void main(String args[]) { System.out.println("Returned value: " + new ExceptionTest().testException()); } }
+ Show Spoiler [return] + Returning value: 1 Returning value: 2 Returned value: 2
Note to self: Do not modify anything as a in catch if you have finally in there too because they'll both go off.
|
On February 06 2015 05:59 spinesheath wrote:Show nested quote +On February 06 2015 05:14 Cynry wrote: That namespace discussion and the recent one about i++ or ++i makes me wonder if noobs should really bother learning this kind of optimisation, considering we probably still write suboptimal programs anyway. Like today at school, some guy was telling how setting a variable to 0 is slower than using a xor or something. Does any of this really matter, be it right or not, when we still go through any set of variable 3 times more than is needed ? I don't think so, but maybe I'm wrong.. Performance optimizations like that not only don't matter, they are outright bad unless you profiled your application's performance and determined that doing these things actually improves your performance significantly. Hint: typically you'll find some awful algorithm that hogs all the processing power and can't be improved by more than .00001% by using an xor instead of an assignment. In the case of ++i over i++ it's just a straight up potential improvement with no downsides unless you specifically need the semantics of i++. Both are equally readable. Whether you should use either of them is another question, but if you use one of them, use ++i. Performance optimizations at instuction level will usually be done by a moderately intelligent comiler anyways, no need to bother with them. Let me be clear: Do not attempt to optimize your code for performance unless you actually have a performance problem AND profiled it.
yes, ++i and i++ is a question of semantics. ++i is a reasonable default if that is not a factor. and for namespaces, they have little to do with performance, you use it for code structure and symbol visibility.
|
On February 06 2015 05:09 RoyGBiv_13 wrote: [...]
Many people use f.lux to dim the screen after sundown, which is an option for helping to avoid high energy light. It takes about an hour after seeing a moderately bright blue or purple light to fully adjust back to clear night vision, so avoid doing anything that requires eyesight immediately after computer usage, such as driving.
Note that f.lux isn't really supposed to dim the screen. Its idea is to shift the color temperature. This technically still dims the screen somewhat, but only because there's no way to do the color change other than cutting something off from the range of the color channels. For example when white (255,255,255) turns into (255,237,217), that change obviously loses some brightness.
The difference in the brightness in a room in the middle of the day compared to the lighting at home at night can be a lot bigger than the brightness that's lost through f.lux. It's currently 10 in the morning where I am. I have a colorimeter here and it can measure ambient light. It measures around 600 lux on the table in front of me. When I close the blinds, it's measuring 80 lux in the same spot, but you can still do everything normally in the room, can still read fine etc.
I have no good idea where I'm going with this train of thought here. Perhaps, when you have a monitor that's set up in a certain way to be good to work at in the middle of the day, and you notice you don't have any problems with your eyes when using it in daytime, the big change you could do to try to fix issues in the evening and night is reducing the monitor's brightness by a serious amount, something that feels ridiculous at first look? The room should probably also never be completely dark so that there's no big difference between the monitor's picture and your surroundings, just like it is in daytime?
|
On February 06 2015 07:23 darkness wrote:Show nested quote +On February 06 2015 07:02 obesechicken13 wrote: Setting: I had an interview question yesterday in C#, for .net
The guy asked me to fill in pseudocode for a login.
My Answer: I just verified the password, then you had to make an update and an insert. At first I didn't get what was so special about the question, but he hinted that maybe one of the queries would fail. So like with a banking transfer, you'd want to make sure either both queries (withdrawl from your account, deposit to another account) worked or that neither did. How would I handle that?
I eventually answered rollbacks, but I had never used transactions with sql querieis so I didn't mention anything about transactions but he still seemed to be like "alright, at least he knows to use rollbacks".
Is there a better way? Do you need try catch exception handling or is there a better way to check if queries don't work properly?
The database type would be MSSQL but I doubt it'd matter. This property is called "atomicity" and is part of ACID: http://en.wikipedia.org/wiki/ACID#AtomicityHere is a good example: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.commit(v=vs.110).aspxYou commit a transaction when no error occurs. It's better if you wrap stuff in a TransactionScope instead of manually handling it via SqlTransaction. TransactionScope has a lot of improvements and is the recommended way for using transactions in .NET.
Unless you call the .Complete() method, the transaction will be rolled back automatically when the TransactionScope instance is disposed (otherwise it will be completed when disposing) - no need for complex try/catch blocks.
Unfortunately, the default settings for TransactionScope are not the best for SQL Server, and it is recommended to create a small utils to properly create the TransactionScope for use with SQL Server (though this was written in 2010; the default behaviour may have changed by now).
+ Show Spoiler + private void BlahSomething() { using(TransactionScope scope = TransactionUtils.CreateTransactionScope()) { // do database work, maybe use entity framework scope.Complete(); } }
|
Wooow nailed my programming exam, most likely a 1.0 incoming. Thanks to all you kind people that helped me with my newbie-questions. 
On the other hand: may our prof have a edgy stone in his shoe for the rest of his life. He deliberately tried to set us up with the exercise required to get an 4.0. He wanted us to make some sort of ticker that displays a sentence. We did that in one of his lectures, but as always when he shows something/explain something, it got terrible confusing since he always makes lots of errors, typos and so on. Lucky me remembered the system("cls")-command, but a lot got burned by it, especially those who wherent in that exact lecture. "Ticker" might be kind of familiar, but he used the german word "Laufschrift" and I can tell you, most of us will look at you in confusion if you ask them what a "laufschrift" is. You could also tell by the format of the exampaper that it got smushed in there and what usually would be the part for a 4.0 got merged with the 3.0 part. We are now coordinating a complaint. I bet he is "verbeamtet"(which means its effectively impossible to fire him), but at least he shouldnt teach in first semester. Another professor is giving programming lectures for our semester and does a way better job (unfortunately I was unable to attend his lectures because they colided with other lectures of mine), next semester we will have a lot less students and guess who is losing and whos keeping his draft? Yep, shitty old prof that doesnt really care get to keep his and good prof that really teaches stuff loses his draft and they get merged with us.
|
Happy Korean birthday darkness
|
On February 06 2015 22:45 Artesimo wrote:Wooow nailed my programming exam, most likely a 1.0 incoming. Thanks to all you kind people that helped me with my newbie-questions. 
Congrats to that.
Lauftext/Laufschrift is a word you should know as a German. A ticker is something different. A ticker can be implemented as a Lauftext, but it can also just update as a whole. Think Bundesliga-Ticker, where you can always read the whole match and nothing is scrolling. Overall you should be able to clarify such definitions during an exam.
And if he discussed it in one of his lectures, yes he did try to set you up. For passing. If it's on a slide it can be part of the exam, if a whole lecture is spent on it, it _will_ be in the exam. "a lot got burned by it, especially those who wherent in that exact lecture." If people are not in the lecture / don't bother to learn the missed stuff, then it's their fault, not the professor's one.
You have the freedom to miss lectures, you have the responsibility to still know the material.
|
On February 06 2015 22:45 Artesimo wrote:I bet he is "verbeamtet"(which means its effectively impossible to fire him)
I'm guessing this is the same as English 'tenure'?
|
On February 07 2015 01:07 obesechicken13 wrote:Happy Korean birthday darkness 
Haha, thank you. I didn't expect that!
|
On February 07 2015 02:42 Zocat wrote:Show nested quote +On February 06 2015 22:45 Artesimo wrote:Wooow nailed my programming exam, most likely a 1.0 incoming. Thanks to all you kind people that helped me with my newbie-questions.  Congrats to that. Lauftext/Laufschrift is a word you should know as a German. A ticker is something different. A ticker can be implemented as a Lauftext, but it can also just update as a whole. Think Bundesliga-Ticker, where you can always read the whole match and nothing is scrolling. Overall you should be able to clarify such definitions during an exam. And if he discussed it in one of his lectures, yes he did try to set you up. For passing. If it's on a slide it can be part of the exam, if a whole lecture is spent on it, it _will_ be in the exam. "a lot got burned by it, especially those who wherent in that exact lecture." If people are not in the lecture / don't bother to learn the missed stuff, then it's their fault, not the professor's one. You have the freedom to miss lectures, you have the responsibility to still know the material.
Maybe, ticker is the direct translation, BUT he did not set us up for passing. This laufschrift-thing we did in one of the very early lectures and if you seen him presenting something one time, you realize how small effort he puts into his lectures. Whiel every other part of the test checked some sort of principle/general knowledge like classes, call by value/call by refference etc. this one was just something you can play around with, nothing more and it was totally out of context since the rest of the test belonged together. Also, it didnt really fit into his usal examstyle. I know that I am not a skilled programmer, but a newb and still, that seems pretty clear to me. 2 guys who easilie made the exam are coordinating this complaint about the prof and they showed us the video from a few lectures of the other prof. He writes stuff on the board/explains, his presentation isnt just a straight copy of "c++ primer". Amongst the students our prof is a known problem and we will try to take care that he has to change something.
To be precise, his exams are usually: 4.0 implement a class, its attributes and methods. 3.0 read in a file/implement some inheritage 2.0 let the different values of the classes interact/use static 1.0 overload an operator
This exam: 4.0 the ticker thing 3.0 implement classes (a club with leader and member), read in file with memberlist 2.0 interaction of values/changing values 1.0 overload operator
even if he didnt set us up, he does a pretty poor job in his lectures and its not frustration that is speaking out of me it is the sad truth, especially now that I have seen the lectures of the other prof.
|
Hey guys, I have a general question about classes.
I've been working on an ASP.NET MVC project and a thought came up while designing my controller. For those of you that don't know, a controller is simply an object that handles incoming HTTP requests via URLs and returns a response.
One of my particular controller actions takes several query parameters, and returns a collection of items that are sorted based off of these parameters. As you could expect, the logic to check all of these and order the collection was pretty lengthy at around 25-30 lines.
I didn't want to bloat my controller class with a bunch of business logic, so I created a separate class called ControllerUtilities that would execute the logic, and return the ordered collection. End result was I was able to get my controller to be much slimmer, replacing the 25-30 lines with 2.
Is this an acceptable use for classes? Is it okay to create a separate "helper/utility" class if the only reason for its existence is to take some chunk of code out of another class to make it slimmer?
Just wanted to get input from you guys as still relatively new to oop best practices. This is something I've done a lot on clientside javascript using modules, but just wasn't sure how it should be handled here.
And also related, do I get any non-trivial performance hits from doing the above? I would assume it would take more memory, and possibly more processing time, to create a new object to perform some logic, as opposed to piling on all the logic into one huge monolithic class.
Thanks!
|
Just make the helper class a private method inside the original class unless you expect that business logic to be used repeatedly by other classes. It's not monolithic if that's all that is required. I would even be hesitant to move it to another function unless it made sense to move it to another function, unlike a "if (x) { doWork; doMoreWork(); }" kinda deal where the benefit is essentially just fewer lines to read and not necessarily much more clarity of intent.
|
I'm not a fan of "helper" or "utility" classes, especially if they contain business logic. I consider them ok for things like validation (throw if null or not in range and so on) or other pieces of 2-4 line of code that would otherwise be repeated all over the place.
In your case there probably is a better approach that uses generic sorting functionality, but you'd probably need to transform your current algorithm in a not-trivial way. For example you could try to create a custom comparer (initialized with the query parameters) for your items and sort the collection using that comparer. Ideally that comparer would be completely independent from the original problem. Maybe there are completely different, better approaches.
In general, I only like classes that have a name that describes the class well. "Helper" or "utility" really doesn't say much. Neither does "data" or "information".
In any case, if all you achieved by creating that new class is that you moved the code from one place to another without making the code itself any better, you might as well keep it in its original location. You don't improve anything by clumsily hiding code like that.
|
Your controller methods should be very slim. Their only purpose is to connect your view action to your model, and nothing more. While I would agree with spinesheath that simply moving the code from one place to another doesn't really add any value, in this case that's exactly what you want to do since you're using an MVC architecture.
Where then should it go? That depends on how much of it is business logic. Your every day list sort is certainly not business logic, but if you're sorting based on specific constraints it might be. I would try to separate what is pertinent to business logic from what isn't, and put both pieces in places they can be reused.
|
There are some basic guidelines when it comes to this stuff (assuming you're using some form of ORM).
If you need a method (no matter how complex) that only needs to do stuff with a single entity (table), you can put this method in the entity class. In case of methods that need to do stuff on multiple tables/rows you can create repositories in your model (you would have say 'User' entity and you could create 'UserRepository') and put your code in there as public methods. If you need more than 5 methods per repository you should think about creating a service though.
Here's a nice article on repositories: http://www.whitewashing.de/2013/03/04/doctrine_repositories.html
|
For next semester I like a 90% chance I pick my own project.
So far I only decided that I want to do something in C. any suggestions?
|
so i'm kind of bored and decided to dick around with some c++. careful, i'm like the noobiest person to ever enter the url to this thread
i downloaded visual c++ 2010 express and started dicking around in that program. just adding integers and showing the result in a window
we did a little programming in VBA at university (that's what we'll mostly due) and i decided to mess around with c++ since i have very, very basic understanding of how to write very, very basic programs
my question is, what good is c++ for? could i write a program, which i would execute, and have it ask me for 3 numbers, which would then be assigned to a ax² + bx + c = 0 style equation, which the program would then solve? i'm guessing that yes, i would. i would call it "polynomialesforidiots.exe" for example
what else could i do?
tl;dr, what real world applications does c++ have?
|
|
|
|