Search Results

Search found 15316 results on 613 pages for 'coding style'.

Page 75/613 | < Previous Page | 71 72 73 74 75 76 77 78 79 80 81 82  | Next Page >

  • Better way to write this Java code?

    - by Macha
    public void handleParsedCommand(String[] commandArr) { if(commandArr[0].equalsIgnoreCase("message")) { int target = Integer.parseInt(commandArr[1]); String message = commandArr[2]; MachatServer.sendMessage(target, this.conId, message); } else if(commandArr[0].equalsIgnoreCase("quit")) { // Tell the server to disconnect us. MachatServer.disconnect(conId); } else if(commandArr[0].equalsIgnoreCase("confirmconnect")) { // Blah blah and so on for another 10 types of command } else { try { out.write("Unknown: " + commandArr[0] + "\n"); } catch (IOException e) { System.out.println("Failed output warning of unknown command."); } } } I have this part of my server code for handling the types of messages. Each message contains the type in commandArr[0] and the parameters in the rest of commandArr[]. However, this current code, while working seems very unelegant. Is there a better way to handle it? (To the best of my knowledge, String values can't be used in switch statements, and even then, a switch statement would only be a small improvement.

    Read the article

  • Is it bad to explicitly compare against boolean constants e.g. if (b == false) in Java?

    - by polygenelubricants
    Is it bad to write: if (b == false) //... while (b != true) //... Is it always better to instead write: if (!b) //... while (!b) //... Presumably there is no difference in performance (or is there?), but how do you weigh the explicitness, the conciseness, the clarity, the readability, etc between the two? Note: the variable name b is just used as an example, ala foo and bar.

    Read the article

  • Should Python import statements always be at the top of a module?

    - by Adam J. Forster
    PEP 08 states: Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants. However if the class/method/function that I am importing is only used in rare cases, surely it is more efficient to do the import when it is needed? Isn't this: class SomeClass(object): def not_often_called(self) from datetime import datetime self.datetime = datetime.now() more efficient than this? from datetime import datetime class SomeClass(object): def not_often_called(self) self.datetime = datetime.now()

    Read the article

  • Elegant check for null and exit in C#

    - by aip.cd.aish
    What is an elegant way of writing this? if (lastSelection != null) { lastSelection.changeColor(); } else { MessageBox.Show("No Selection Made"); return; } changeColor() is a void function and the function that is running the above code is a void function as well.

    Read the article

  • Formatting associative array declaration

    - by Drew Stephens
    When declaring an associative array, how do you handle the indentation of the elements of the array? I've seen a number of different styles (PHP syntax, since that's what I've been in lately). This is a pretty picky and trivial thing, so move along if you're interested in more serious pursuits. 1) Indent elements one more level: $array = array( 'Foo' => 'Bar', 'Baz' => 'Qux' ); 2) Indent elements two levels: $array = array( 'Foo' => 'Bar', 'Baz' => 'Qux' ); 3) Indent elements beyond the array constructor, with closing brace aligned with the start of the constructor: $array = array( 'Foo' => 'Bar', 'Baz' => 'Qux' ); 4) Indent elements beyond the array construct, with closing brace aligned with opening brace: $array = array( 'Foo' => 'Bar', 'Baz' => 'Qux' ); Personally, I like #3—the broad indentation makes it clear that we're at a break point in the code (constructing the array), and having the closing brace floating a bit to the left of all of the array's data makes it clear that this declaration is done.

    Read the article

  • Javascript clarity of purpose

    - by JesDaw
    Javascript usage has gotten remarkably more sophisticated and powerful in the past five years. One aspect of this sort of functional programming I struggle with, esp with Javascript’s peculiarities, is how to make clear either through comments or code just what is happening. Often this sort of code takes a while to decipher, even if you understand the prototypal, first-class functional Javascript way. Any thoughts or techniques for making perfectly clear what your code does and how in Javascript? I've asked this question elsewhere, but haven't gotten much response.

    Read the article

  • Surprise for a programmer [closed]

    - by penelope
    help! my boyfriend's birthday is next month. since he is a programmer, I'd love to make him a cake with the code for "happy birthday" (and perhaps something awesome) written in icing on top. not being a programmer myself, i have no idea where to begin. any suggestions would be greatly appreciated!

    Read the article

  • Custom button template in wpf

    - by Archana R
    Hello, I want to create a simple button template with an image and text inside it. But i want to keep the System button's look and feel. Can anyone tell me step by step how to create it? P.S. : I have already tried it with CustomControl in wpf and BasedOn property.

    Read the article

  • Can I assign a object to a integer variable?

    - by AKN
    Let say I have a object. I'm assigning that to an integer. MyClass obj1 = 100;//Not valid Let's say, I have a parameterized constructor which accepts an integer. MyClass(int Num) { // .. do whatever.. } MyClass obj1 = 100;//Now, its valid Likewise on any circumstance, does the vice-versa becomes valid?!. eg) int Number = obj1;//Is it VALID or can be made valid by some tweeks

    Read the article

  • Is there a standard literal constant that I can use instead of "utf-8" in C# (.Net 3.5)?

    - by Hamish Grubijan
    Hi, I would like to find a better way to do this: XmlNode nodeXML = xmlDoc.AppendChild( xmlDoc.CreateXmlDeclaration( "1.0", "utf-8", String.Empty) ); I do not want to think about "utf-8" vs "UTF-8" vs "UTF8" vs "utf8" as I type code. I would like to make my code less prone to typos. I am sure that some standard library has declatred "utf-8" as a const / readonly string. How can I find it? Also, what about "1.0"? I am assuming that major XML versions have been enumerated somewhere as well. Thanks!

    Read the article

  • Are regexes really maintainable?

    - by Rich Bradshaw
    Any code I've seen that uses Regexes tends to use them as a black box: Put in string Magic Regex Get out string This doesn't seem a particularly good idea to use in production code, as even a small change can often result in a completely different regex. Apart from cases where the standard is permanent and unchanging, are regexes the way to do things, or is it better to try different methods?

    Read the article

  • java api design - NULL or Exception

    - by srini.venigalla
    Is it better to return a null value or throw an exception from an API method? Returning a null requires ugly null checks all over, and cause a major quality problem if the return is not checked. Throwing an exception forces the user to code for the faulty condition, but since Java exceptions bubble up and force the caller code to handle them, in general, using custom exceptions may be a bad idea (specifically in java). Any sound and practical advice?

    Read the article

  • Is there a downside to adding an anonymous empty delegate on event declaration?

    - by serg10
    I have seen a few mentions of this idiom (including on SO): // Deliberately empty subscriber public event EventHandler AskQuestion = delegate {}; The upside is clear - it avoids the need to check for null before raising the event. However, I am keen to understand if there are any downsides. For example, is it something that is in widespread use and is transparent enough that it won't cause a maintenance headache? Is there any appreciable performance hit of the empty event subscriber call?

    Read the article

  • using KVO to update an NSTableView filtered by an NSPredicate

    - by KingRufus
    My UI is not updating when I expect it to. The application displays "projects" using a view similar to iTunes -- a source list on the left lets you filter a list (NSTableView) on the right. My filters update properly when they are examining any simple field (like name, a string), but not for arrays (like tags). I'm removing a tag from one of my objects (from an NSMutableArray field called "tags") and I expect it to disappear from the list because it no longer matches the predicate that is bound to my table's NSArrayController. ProjectBrowser.mm: self.filter = NSPredicate* srcPredicate = [NSPredicate predicateWithFormat:@"%@ IN %K", selectedTag, @"tags"]; Project.mm: [self willChangeValueForKey:@"tags"]; [tags removeAllObjects]; [self didChangeValueForKey:@"tags"]; I've also tried this, but the result is the same: [[self mutableArrayValueForKey:@"tags"] removeAllObjects]; Interface Builder setup: a ProjectBrowser object is the XIB's File Owner an NSArrayController (Project Controller) has its Content Array bound to "File's Owner".projects Project Controller's filter predicate is bound to "File's Owner".filter NSTableView's column is bound to "Project Controller".name

    Read the article

  • How to get all n sets of three consecutives elements in an array or arraylist with a for statement ?

    - by newba
    Hi, I'm trying to do a convex hull approach and the little problem is that I need to get all sets of three consecutive vertices, like this: private void isConvexHull(Ponto[] points) { Arrays.sort(points); for (int i = 0; i <points.length; i++) { isClockWise(points[i],points[i+1],points[i+2]); } //... } I always do something that I don't consider clean code. Could please help me find one or more ways to this? I want it to be circular, i.e., if my fisrt point of the a set is the last element in the array, the 2nd element will be the 3rd in the list and the 3rd in that set will be the the 2nd element in the list, and so on. They must be consecutive, that's all.

    Read the article

  • Where to Declare Structures, etc?

    - by cam
    Should all structs and classes be declared in the header file? If I declare a struct/class in a source file, what do I need to put in the header file so that it can be used in other files? Also, are there any resources that show some standard practices of C++ out there?

    Read the article

  • Most readable way to write simple conditional check

    - by JRL
    What would be the most readable/best way to write a multiple conditional check such as shown below? Two possibilities that I could think of (this is Java but the language really doesn't matter here): Option 1: boolean c1 = passwordField.getPassword().length > 0; boolean c2 = !stationIDTextField.getText().trim().isEmpty(); boolean c3 = !userNameTextField.getText().trim().isEmpty(); if (c1 && c2 && c3) { okButton.setEnabled(true); } Option 2: if (passwordField.getPassword().length > 0 && !stationIDTextField.getText().trim().isEmpty() && !userNameTextField.getText().trim().isEmpty() { okButton.setEnabled(true); } What I don't like about option 2 is that the line wraps and then indentation becomes a pain. What I don't like about option 1 is that it creates variables for nothing and requires looking at two places. So what do you think? Any other options?

    Read the article

  • C++ class initialisation containing class variable initialization

    - by Phil Hannent
    I noticed some code of a colleague today that initialized class variables in the initialization. However it was causing a warning, he says because of the order they are in. My question is why is it better to do variable initialization where it currently is and not within the curly brackets? DiagramScene::DiagramScene( int slideNo, QRectF screenRect, MainWindow* parent ) : QGraphicsScene( screenRect, parent ), myParent( parent ), slideUndoImageCurrentIndex(-1), nextGroupID(0), m_undoInProgress(false), m_deleteItemOnNextUndo(0) line(0), path(0) { /* Setup default brush for background */ scDetail->bgBrush.setStyle(Qt::SolidPattern); scDetail->bgBrush.setColor(Qt::white); setBackgroundBrush(scDetail->bgBrush); }

    Read the article

  • Adding the sum of numbers using a loop statement

    - by Deonna
    I need serious help dividing the positive numbers and the negative numbers. I am to accumulate the total of the negative values and separately accumulate the total of the positive values. After the loop, you are then to display the sum of the negative values and the sum of the positive values. The data is suppose to look like this: -2.3 -1.9 -1.5 -1.1 -0.7 -0.3 0.1 0.5 0.9 1.3 1.7 2.1 2.5 2.9 Sum of negative values: -7.8 Sum of positive values: 12 So far I have this: int main () { int num, num2, num3, num4, num5, sum, count, sum1; int tempVariable = 0; int numCount = 100; int newlineCount = 0, newlineCount1 = 0; float numCount1 = -2.3; while (numCount <= 150) { cout << numCount << " "; numCount += 2; newlineCount ++; if(newlineCount == 6) { cout<< " " << endl; newlineCount = 0; } } **cout << "" << endl; while (numCount1 <=2.9 ) { cout << numCount1 << " "; numCount1 += 0.4; newlineCount1 ++; } while ( newlineCount1 <= 0 && newlineCount >= -2.3 ); cout << "The sum is " << newlineCount1 << endl;** return 0; }

    Read the article

  • Use of 'super' keyword when accessing non-overridden superclass methods

    - by jonny
    I'm trying to get the hang of inheritance in Java and have learnt that when overriding methods (and hiding fields) in sub classes, they can still be accessed from the super class by using the 'super' keyword. What I want to know is, should the 'super' keyword be used for non-overridden methods? Is there any difference (for non-overridden methods / non-hidden fields)? I've put together an example below. public class Vehicle { public int tyreCost; public Vehicle(int tyreCost) { this.tyreCost = tyreCost; } public int getTyreCost() { return tyreCost; } } and public class Car extends Vehicle { public int wheelCount; public Vehicle(int tyreCost, int wheelCount) { super(tyreCost); this.wheelCount = wheelCount; } public int getTotalTyreReplacementCost() { return getTyreCost() * wheelCount; } } Specifically, given that getTyreCost() hasn't been overridden, should getTotalTyreReplacementCost() use getTyreCost(), or super.getTyreCost() ? I'm wondering whether super should be used in all instances where fields or methods of the superclass are accessed (to show in the code that you are accessing the superclass), or only in the overridden/hidden ones (so they stand out).

    Read the article

  • Microsoft C++ Language Reference

    - by eSKay
    Whenever any question is asked, and a reference text is needed, I never see MSDN C++ Language Reference being referred. I was browsing through it and I personally feel that it is extremely well written. Is there some specific reason it is not used as often as a standard? Is it because it contains some VC++ specific features?

    Read the article

< Previous Page | 71 72 73 74 75 76 77 78 79 80 81 82  | Next Page >