The Big Programming Thread - Page 74
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. | ||
Craton
United States17233 Posts
| ||
Frigo
Hungary1023 Posts
On August 18 2011 10:14 Craton wrote: If something is O(n log n) and I'm sorting 1,000,000 numbers, how do I calculate that. I'm missing something. The constant term in the big O expression can be arbitrary, you need specific knowledge of how many steps does the algorithm take* and what are these steps, to calculate anything. In practice it is easier and better to measure the speed of the algorithm in msec instead of steps. *: Also these steps can vary for the input data, so you are better off calculating maximum and average cases, assuming a specific distribution of the input data or extreme cases. On August 15 2011 08:08 EvanED wrote: This is a bit of a red herring, which can be demonstrated two ways. First, calling fgets(buf, size, stdin) works fine, and is just like calling gets(buf) except that if it reads more than size bytes, it will stop early. (There's a difference in how it handles the trailing newline if it doesn't stop early, as well.) It's not like fgets is imbued with psychic powers to see what's coming up in the stream, it just makes a "best-effort" attempt to fit it in the given buffer. The second way you can see this is calling fgets on an actual file -- it doesn't figure out that the current line is too long to fit into the buffer and does nothing, it again makes that best-effort attempt. (And as a third way: just because the runtime thinks something is a file doesn't mean it is -- it could be a device node, a fifo, etc. -- and doesn't mean it can seek randomly.) Obviously the statement is true only for gets, fgets is completely different, it is not "just like" gets. This difference between gets and other methods makes it possible to employ various workarounds so you don't need to know the length of the line in advance to ensure safe operation. For the same reason it is also possible to use them on streams. I'd say the guy is best off by implementing an automatically growing vector structure and associated getline functions instead of sacrificing safety with gets or fooling around with a naked fgets. | ||
catamorphist
United States297 Posts
| ||
Pigsquirrel
United States615 Posts
| ||
Craton
United States17233 Posts
On August 19 2011 03:16 catamorphist wrote: When I interview C# programmers I ask them what things in C# and .NET bother them the most and what they would fix if they were designing the language. Don't be that guy who says "I can't think of anything." What would you change? This strikes me as a question more relevant for programmers with a couple years experience than someone fresh out of college. | ||
catamorphist
United States297 Posts
On August 19 2011 04:29 Craton wrote: Finished interview, went okay. What would you change? This strikes me as a question more relevant for programmers with a couple years experience than someone fresh out of college. Well, yeah, I'm mostly talking about people who have some amount (maybe a year or two) of C# experience. But man, there's sure a lot of low-hanging fruit: - Cruft from prior versions. Three separate framework ways to parse XML nowadays. Non-generic collections that are basically obsolete implementations of the generic ones. A ton of different threading APIs (i.e. Thread, ThreadPool, BackgroundWorker, Task, BeginInvoke, and to some degree the new C# 5 async) that all solve overlapping problems. - Events and properties aren't first-class, which is frustrating. I can't even get a delegate pointing to the "get" method of a property without writing a big blob of reflection. I can't pass an event down to a method so that the method can fire it without making a wrapper function. - Type inference is spotty. Why can it infer return types of initialized local variables, but not types of initialized class properties? Why can it infer return types of anonymous functions, but not named functions? These are just arbitrary decisions. - While I'm at it, generic type constraints are crappy and limited. I can't even write public class X<T> where T : new(int, string) and make a constructor constraint. Come on! - Reference types are nullable by default and can't be declared as permanently non-nullable, which may be the most crash-inducing design decision ever yet invented on this planet. - No language support for purity or immutability constraints. I figured they would add an immutability check in 4.0 when they added generic covariance and contravariance, but nope. - No language support for declaring or enforcing the thread safety of methods or declaring a method atomic with respect to callers. No language or library support for any kind of STM. - No language support for pattern matching or destructuring function arguments. - No language support to make tuples easier to work with (e.g. as they have in F#.) A lot of the problems with .NET and C# won't seem like problems until you have some experience and encounter them a few times, or unless you have used other languages that do a better job of tackling them. But if someone can't think of *any* significant problems, then I get concerned, because I worry that the person in question just doesn't know what good code is and what's a hacky workaround. | ||
themikeman
United States8 Posts
| ||
Sachem
United States116 Posts
| ||
rabidch
United States20288 Posts
On August 19 2011 11:43 Sachem wrote: You should add this as a guide to obfuscation, old, but I still get a great laugh out of it: http://thc.org/root/phun/unmaintain.html i spent 5 minutes laughing at the lisp code | ||
EscPlan9
United States2777 Posts
I'm back home now so I do not have access to the actual code being used, but this is the pseudocode for what we're trying to do: User selects which WSDL they want to work with After selecting the WSDL - parse the WSDL for the operation names (CreateCart/ModifyCart/etc) - display the operation names in a drop-down box After selecting the operation names.. - other stuff involving the operation they want to do, and what course to follow based on the WSDL information for that operation There will be a lot of information to parse through to display the options to the user for how to proceed from there. Oh, and the users will be internal quality assurance testers (it's a program to help them produce tests for the constantly changing software going out to customers) Again, I just want to know what you think would be the best way to store these different types of information after parsing the WSDL (preferably just once), and perhaps if you think there's a better way to parse the WSDL than doing XMLReader in C#. | ||
catamorphist
United States297 Posts
On August 20 2011 07:43 EscPlan9 wrote: A project I'm contributing to at work involves parsing a WSDL using C#. I read about XMLReader being the best option in terms of performance, so at the moment have used that. What we want to be able to do is parse the WSDL once and get all the nodes, elements and attributes then. My main question is - how to best store all this information? I was thinking dynamic arrays, and have some experience doing it with ArrayLists (using Java), but I've heard recommendations for using List<T>. Perhaps there's a better way? I'm back home now so I do not have access to the actual code being used, but this is the pseudocode for what we're trying to do: User selects which WSDL they want to work with After selecting the WSDL - parse the WSDL for the operation names (CreateCart/ModifyCart/etc) - display the operation names in a drop-down box After selecting the operation names.. - other stuff involving the operation they want to do, and what course to follow based on the WSDL information for that operation There will be a lot of information to parse through to display the options to the user for how to proceed from there. Oh, and the users will be internal quality assurance testers (it's a program to help them produce tests for the constantly changing software going out to customers) Again, I just want to know what you think would be the best way to store these different types of information after parsing the WSDL (preferably just once), and perhaps if you think there's a better way to parse the WSDL than doing XMLReader in C#. XmlReader is very low-level. The typical way to parse XML in C# nowadays is using the XDocument API, which is built on top of XmlReader. I don't really see why the parser's performance would be relevant when parsing a handful of WSDL files; one wouldn't expect them to be very large. | ||
EscPlan9
United States2777 Posts
![]() Good point about the performance part - the largest WSDL is maybe 1 MB? Either way its not like hundreds of MBs. I don't think there would be enough of a difference for that to be such a huge factor in the decision. This application is going to be dynamically creating labels, text fields and checkboxes based on information it receives from the WSDL (i.e. checkboxes on boolean), so I will need to organize a lot of information parsed from the WSDL for later use. Is there any significant difference between storing a lot of this information in ArrayList rather than List<t> ? I suspect this question might be too context specific for what I'm working with. If that's the case, I'm more familiar with working on ArrayLists, so I would just choose that. | ||
catamorphist
United States297 Posts
On August 20 2011 10:53 EscPlan9 wrote: Wow XDocument looks really handy and easy to use compared to XMLReader ![]() Good point about the performance part - the largest WSDL is maybe 1 MB? Either way its not like hundreds of MBs. I don't think there would be enough of a difference for that to be such a huge factor in the decision. This application is going to be dynamically creating labels, text fields and checkboxes based on information it receives from the WSDL (i.e. checkboxes on boolean), so I will need to organize a lot of information parsed from the WSDL for later use. Is there any significant difference between storing a lot of this information in ArrayList rather than List<t> ? I suspect this question might be too context specific for what I'm working with. If that's the case, I'm more familiar with working on ArrayLists, so I would just choose that. The only significant difference is that ArrayList will be more of a pain in the ass to deal with, since it's not generic. (There's a performance problem with big ArrayLists containing value types due to boxing, but it wouldn't be significant unless you had much more data.) I personally have literally never used an ArrayList since C# 3.0 came out, and I can't think of any reason to ever recommend it. List<T> is basically strictly better. But if you don't mind casting your data every time you access the list, then I guess you can use whatever you please. | ||
Craton
United States17233 Posts
| ||
TadH
Canada1846 Posts
Having said that my question should be pretty simple. Let me explain myself a bit, I'm working as a networking engineer for a company, we have this software called Paessler Network Monitor. We are using it to track devices on our network that are installed remotely, and to track their data usage. This network monitoring software also comes with a separate application called the billing tool, this tool comes with a few scripts and templates scripted in lua. Basically it has these scripts stored in a directory. You open up the billing tool, create a new customer name, and associate a template with it. Now the templates I'm guessing are customizable. And I've been playing around with it for a while, because I'm honestly not retarded, but I can't for the life of me get it to generate any kind of billing report. If someone could explain to me specifically what I am able to modify in the script and possible values and things like that I would be eternally grateful Script 1 + Show Spoiler + luanet.load_assembly("Paessler.Billingtool") invoice = GetInvoice() if SensorChannel(1) ~= nil then freeVolumeGB = 15 costPerGB = 50 currency = "$" sumGB = math.ceil((SensorChannel(1):GetRawSum()/1024/1024/1024)) calculateGB = sumGB - freeVolumeGB if calculateGB <= 0 then calculateGB = 0 end invoice:AddItem(SensorChannel(1).Name , sumGB .. "GB"); invoice:AddItem("Free volume" , freeVolumeGB); invoice:AddItem("Each GB over " .. freeVolumeGB, costPerGB .. currency); invoice:AddItem(calculateGB .. "GB to pay", calculateGB*costPerGB .. currency); invoice:SetTotal("Total", calculateGB*costPerGB .. currency); else invoice:AddItem("Error: " , "Channel 1 not available in this type of sensor"); end Script 2 + Show Spoiler + luanet.load_assembly("Paessler.Billingtool") invoice = GetInvoice() if GetPercentile() ~= nil then percentile = math.ceil(GetPercentile()/1000*8) costPerKbit = 1.50 currency = "$" totalCost = percentile * costPerKbit invoice:AddItem("Percentile", percentile .. "kbit/s"); invoice:AddItem("Charge per kbit/s", costPerKbit .. currency); invoice:SetTotal("Total", totalCost .. currency); else invoice:AddItem("No percentile available", ""); end I know it's probably very simple for one of you guys in here, but it's just not clicking for me. Any help or advice is appreciated. | ||
Craton
United States17233 Posts
On August 23 2011 00:09 TadH wrote: Script 1 + Show Spoiler + luanet.load_assembly("Paessler.Billingtool") //load code invoice = GetInvoice() //get invoice then stores it as 'invoice' if SensorChannel(1) ~= nil then //dunno what sensorchannel is freeVolumeGB = 15 costPerGB = 50 currency = "$" //probably sets as USD sumGB = math.ceil((SensorChannel(1):GetRawSum()/1024/1024/1024)) //calculates total gigabytes calculateGB = sumGB - freeVolumeGB //calculates unused gigabytes if calculateGB <= 0 then //GB can't be negative calculateGB = 0 end //these lines seem to add text to the invoice being made invoice:AddItem(SensorChannel(1).Name , sumGB .. "GB"); invoice:AddItem("Free volume" , freeVolumeGB); invoice:AddItem("Each GB over " .. freeVolumeGB, costPerGB .. currency); invoice:AddItem(calculateGB .. "GB to pay", calculateGB*costPerGB .. currency); invoice:SetTotal("Total", calculateGB*costPerGB .. currency); else invoice:AddItem("Error: " , "Channel 1 not available in this type of sensor"); end Script 2 + Show Spoiler + luanet.load_assembly("Paessler.Billingtool") invoice = GetInvoice() //seems to calculate the USD cost per Kilobit if GetPercentile() ~= nil then percentile = math.ceil(GetPercentile()/1000*8) costPerKbit = 1.50 currency = "$" totalCost = percentile * costPerKbit //adds text to invoice invoice:AddItem("Percentile", percentile .. "kbit/s"); invoice:AddItem("Charge per kbit/s", costPerKbit .. currency); invoice:SetTotal("Total", totalCost .. currency); else invoice:AddItem("No percentile available", ""); end Added comments. You should have documentation about what else you're able to use. | ||
Sentient
United States437 Posts
On August 19 2011 04:49 catamorphist wrote: Well, yeah, I'm mostly talking about people who have some amount (maybe a year or two) of C# experience. But man, there's sure a lot of low-hanging fruit: I've done C# exclusively for about six years, and I want to criticize some of your low-hanging fruit decisions, but first let me offer my own. I can't stand how the DependencyProperty system in WPF and Silverlight so flagrantly violates type safety. Consider the following cases:
Worse yet, GetValue(SomeDependencyPropertyOfTypeInt) returns type object! You have to cast it to the appropriate type. More than once my head has exploded trying to refactor a DependencyProperty from one type to another, because it requires hunting down every single cast operation. I don't understand why you can't do SetValue<type>(SomeDependencyProperty, a) and GetValue<type>(SomeDependencyProperty). Then the compiler could warn you and even disambiguate the 5/5.0 issue. About some of your examples: - Reference types are nullable by default and can't be declared as permanently non-nullable, which may be the most crash-inducing design decision ever yet invented on this planet. This is the one that prompted me to reply. It is not a crash-inducing design decision, but a crash-prevention design decision. If you ever have a null reference exception, then it means your program is in a state that the programmer didn't anticipate. You can cover it up with a valid reference, but ultimately the state is still undefined. Worse, your program will probably still crash anyway, but it will happen later and in a place where it is difficult to find the original cause. Null reference exceptions pinpoint the exact place the programmer went wrong, so I make it a point to never catch them. I throw them as exceptions all the time because they are a symptom, not the cause. If you really need a value type then use struct, not class. Or, if the object is well encapsulated, you can do something like this: class MyClass [The rest is spoilered so as not to clutter the thread. The ones I don't mention I either agree with (especially the type constraints with new) or am neutral on.] + Show Spoiler + - Events and properties aren't first-class, which is frustrating. I can't even get a delegate pointing to the "get" method of a property without writing a big blob of reflection. I can't pass an event down to a method so that the method can fire it without making a wrapper function. I'm not sure why you would want to do this. For events, it seems like a bad idea to release control of your events. If you really need to, you can pass an Action object via lambda with () => MyEvent(...), which saves a lot of typing. I agree it would be nice to have access to the property getters and setters, but this is only useful in the context of reflection anyway, and it doesn't take all that much to get the MethodInfo for the property's getter and setter. - Cruft from prior versions. Three separate framework ways to parse XML nowadays. Non-generic collections that are basically obsolete implementations of the generic ones. A ton of different threading APIs (i.e. Thread, ThreadPool, BackgroundWorker, Task, BeginInvoke, and to some degree the new C# 5 async) that all solve overlapping problems. The threading pool examples all serve very different problems that overlap only tangentially. The XML parsers make more sense in the context of mobile platforms and Silverlight where they aren't all available simultaneously. - Type inference is spotty. Why can it infer return types of initialized local variables, but not types of initialized class properties? Why can it infer return types of anonymous functions, but not named functions? These are just arbitrary decisions. I'm confused. It can do all of those things. public int Blah() - No language support for purity or immutability constraints. I figured they would add an immutability check in 4.0 when they added generic covariance and contravariance, but nope. There is the readonly keyword, which is perfectly suitable if your data is truly immutable. | ||
TadH
Canada1846 Posts
On August 23 2011 04:09 Craton wrote: Added comments. You should have documentation about what else you're able to use. I actually just found the readme file with the templates. + Show Spoiler + PRTG Billing Tool Scripting Documentation 2011 To customize the billing calculations and output it is necessary to edit or write scripts in the Lua scripting language (see Lua documentation: http://www.lua.org/docs.html). There are already some example scripts in the “\scripts” folder as well as example templates in the “\templates” folder. The Billing Tool executes the script to assign data to the template placeholders. The templates will be rendered as HTML and PDF files afterwards. Load Lua assembly ----------------- To access the Billing Tool functions and data in Lua scripts, the Lua assembly needs to be loaded: luanet.load_assembly("Paessler.Billingtool") Available Lua functions ----------------------- After this step the following functions are available: SensorChannel([channel_ID]):GetRawSum() - Return the total bytes of the selected sensor channel for the specified period. - The [channel_ID] can to be found in the PRTG web interface under the channels tab in the sensor details view. SensorChannel([channel_ID]).Name - Return the name of the selected sensor channel. SensorChannel([channel_ID]).Id - Return the ID of the selected sensor channel. GetPercentile() - Return the calculated percentile value for the spcified period if available. invoice = GetInvoice() - Save the invoice object in the invoice variable to access invoice functions later. invoice:AddItem([name], [value]) - Add an item to the <#itemtable> placeholder in the template. - With style sheets it is possible to customize the layout. Each item is seperated into 2 columns (<td> tags). - The columns are accessible via the CSS class "itemkey" for [name] and "itemvalue" for [value]. invoice:SetTotal([totaltext], [totalvalue]) - Add the total value to the <#itemtable> placeholder in the template. - The <tr> tag in the generated HTML file is accessible via the CSS ID "total". - Via the CSS class "totalvalue" it is possible to style the <span> tag where the [value] will be visible. Itemtable HTML -------------- The following HTML is generated for the <#itemtable> placeholder: <tr> <td class="itemkey"> [name] </td> <td class="itemvalue"> [value] </td> </tr> <tr id="total"> <td class="itemkey" > [totaltext] </td> <td class="itemvalue"> <span class="totalvalue">[totalvalue]</span> </td> </tr> So for example it says: SensorChannel([channel_ID]):GetRawSum() Do I need to add the [ and ] or do I just add the sensor channel between the () so does it look like ([sensor channel]) or (sensor channel) (sensor channel is the device name we're using to pull the data from, so it could be a router or a 3G card etc) Also this: invoice:AddItem([name], [value]) - Add an item to the <#itemtable> placeholder in the template. There is no "<#itemtable> int he template. Would I just type in <#itemtable> at the bottom of the script? I'm just really confused, I've never had to do anything like this before. And I know it's only a basic script lol Thanks for the help. | ||
UltimateHurl
Ireland591 Posts
| ||
TadH
Canada1846 Posts
On August 23 2011 05:19 UltimateHurl wrote: How did I just see this thread? Did my undergrad in software engineering, about to learn lua in my spare time just because. You wanna give me a hand then? :D | ||
| ||