coding-style

Is glib usable in an unobtrusive way?

女生的网名这么多〃 提交于 2019-12-05 03:35:54
I was looking for a good general-purpose library for C on top of the standard C library, and have seen several suggestions to use glib . How 'obtrusive' is it in your code? To explain what I mean by obtrusiveness, the first thing I noticed in the reference manual is the basic types section , thinking to myself, "what, am I going to start using gint , gchar , and gprefixing geverything gin gmy gcode gnow?" More generally, can you use it only locally without other functions or files in your code having to be aware of its use? Does it force certain assumptions on your code, or constraints on your

C# StyleCop - Using “this.” prefix for base class members like current class members or not?

点点圈 提交于 2019-12-05 03:32:23
StyleCop has a rule about using "this." prefix to calling class members (SA1101). Is this rule holds true about a member (for example a method) of a class which is inherited from its base class. Example: class BaseClass { protected void F1() { ... } } class ChildClass : BaseClass { protected void F2() { ... } protected void F3() { this.F2(); // This is correct acording to SA1101 // F1 is a member of base class and if I dont put this prefix, stylecop will not show any message. this.F1(); // Is this correct? F1(); // Or this? } } I know this is just for better readability. The documentation for

Optimal declaration of variables with regards to scope

不问归期 提交于 2019-12-05 02:57:17
I ask this question mostly in regards to C programming, but insights on any language are welcome. When it comes to C, I know it only lets variable declarations occur at the very beginning of a block of code. And I have been under the impression that one should declare all variables to be used within a function at the very beginning of the function. But there are many occasions where I'll have a variable that is only used within a loop (or similar block). an example would be a temporary variable for some return value: while ( whatever ) { int ret; ret = getSomeValue(); } or where some state may

Performance Implications of Using Spaces Instead of Tabs for Indentation

余生颓废 提交于 2019-12-05 02:37:56
I currently use soft tabs (i.e. spaces) for indenting my Ruby code, if I were to use hard tabs would it increase the performance when the code is interpreted? I assume it's faster to read one tab character than parse 4 space characters (however negligible). Do you have an idea of all the phases involved in interpreting from source? Only the very first one, lexical analysis, has to deal with whitespace, and in the case of whitespace, "deal with" means "ignore it". This phase only takes a tiny fraction of the total time, it's generally done using regular expression and pretty much has linear

which style is preferred?

徘徊边缘 提交于 2019-12-05 02:09:59
Option 1: def f1(c): d = { "USA": "N.Y.", "China": "Shanghai" } if c in d: return d[c] return "N/A" Option 2: def f2(c): d = { "USA": "N.Y.", "China": "Shanghai" } try: return d[c] except: return "N/A" So that I can then call: for c in ("China", "Japan"): for f in (f1, f2): print "%s => %s" % (c, f(c)) The options are to either determine whether the key is in directory before hand (f1), or just fallback to the exception (f2). Which one is preferred? Why? Typically, exceptions carry some overhead, and are meant for truly 'exceptional' cases. In this case, this sounds like a normal part of the

Refactor nested IF statement for clarity [closed]

拟墨画扇 提交于 2019-12-05 02:03:03
I want to refactor this mumbo jumbo of a method to make it more readible, it has way to many nested IF's for my liking. How would you refactor this? public static void HandleUploadedFile(string filename) { try { if(IsValidFileFormat(filename) { int folderID = GetFolderIDFromFilename(filename); if(folderID > 0) { if(HasNoViruses(filename) { if(VerifyFileSize(filename) { // file is OK MoveToSafeFolder(filename); } else { DeleteFile(filename); LogError("file size invalid"); } } else { DeleteFile(filename); LogError("failed virus test"); } } else { DeleteFile(filename); LogError("invalid folder ID

Java: instantiate variables in loop: good or bad style?

与世无争的帅哥 提交于 2019-12-05 01:54:12
Ive got one simple question. Normally I write code like this: String myString = "hello"; for (int i=0, i<10; i++) { myString = "hello again"; } Because I think the following would not be good style cause it would create too many unnecessary objects. for (int i=0, i<10; i++) { String myString = "hello again"; } Is this even correct? Or is this just the case when Ive got an explicit object like an object from a class I created? What if it was a boolean or an int? What is better coding style? Instantiate it once before the loop and use it in the loop or instantiate it every time in the loop again

What is the best way to print a table with delimiters in Python

你。 提交于 2019-12-05 01:53:18
问题 I want to print a table mixed with string and float values, as tab delimited output printout. Sure I can get the job done: >>> tab = [['a', 1], ['b', 2]] >>> for row in tab: ... out = "" ... for col in row: ... out = out + str(col) + "\t" ... print out.rstrip() ... a 1 b 2 But I have a feeling there is a better way to do it in Python, at least to print each row with specified delimiter, if not the whole table. Little googling (from here) and it is already shorter: >>> for row in tab: ...

Use of Process with using block [duplicate]

空扰寡人 提交于 2019-12-05 01:44:57
This question already has answers here : Closed 8 years ago . Possible Duplicate: What happens if I don't close a System.Diagnostics.Process in my C# console app? As System.Diagnostics.Process inherits from Component which implements IDisposable , should I always create a Process with a using block? For example, this...: using (var process = new Process()) { process.StartInfo.FileName = "some process.exe"; process.Start(); process.WaitForExit(); } ...instead of this: var process = new Process { StartInfo = { FileName = "some process.exe" } }; process.Start(); process.WaitForExit(); I ask

In OOP, what is the best practice in regards to using “this” inside a class?

前提是你 提交于 2019-12-05 01:31:36
Something I've always wondered; in a class where you can reference a member by either using 'this.[NAME]' or simply [NAME], which is preferred? For example in Java: public class foo { public int bars = 0; private void incrementBars(){ bars++; } } and public class foo { public int bars = 0; private void incrementBars(){ this.bars++; } } 'seem' to have the same effect. In cases where I instantiate multiple instances of class foo, I'd, so far, do something like: for (foo f : listOfFoos){ f.incrementBars(); } and it seems to still work. Is it technically ambiguous, and if so is there a preferred