Search Results

Search found 8664 results on 347 pages for 'lost with coding'.

Page 48/347 | < Previous Page | 44 45 46 47 48 49 50 51 52 53 54 55  | Next Page >

  • Is there a concise way to map a string to an enum in Objective-C?

    - by zekel
    I have a string I want to parse and return an equivalent enum. I need to use the enum type elsewhere, and I think I like how I'm defining the class. The problem is that I don't know a good way to check the string against the enum values without being redundant about the order of the enums. typedef enum { ZZColorRed, ZZColorGreen, ZZColorBlue, } ZZColorType; - (ZZColorType)parseColor:(NSString *)inputString { // inputString will be @"red", @"green", or @"blue" (trust me) // how can I turn that into ZZColorRed, etc. without // redefining their order like this? NSArray *colors = [NSArray arrayWithObjects:@"red", @"green", @"blue", nil]; return [colors indexOfObject:inputString]; } In Python, I'd probably do something like the following, although to be honest I'm not in love with that either. ## maps url text -> constant string RED_CONSTANT = 1 BLUE_CONSTANT = 2 GREEN_CONSTANT = 3 TYPES = { 'red': RED_CONSTANT, 'green': GREEN_CONSTANT, 'blue': BLUE_CONSTANT, } def parseColor(inputString): return TYPES.get(inputString) ps. I know there are color constants in Cocoa, this is just an example.

    Read the article

  • What's the deal with a leading underscore in PHP class methods?

    - by nocash
    While looking over various PHP libraries I've noticed that a lot of people choose to prefix some class methods with a single underscore, such as public function _foo() ...instead of... public function foo() I realize that ultimately this comes down to personal preference, but I was wondering if anyone had some insight into where this habit comes from. My thought is that it's probably being carried over from PHP 4, before class methods could be marked as protected or private, as a way of implying "do not call this method from outside the class". However, it also occurred to me that maybe it originates somewhere (a language) I'm not familiar with or that there may be good reasoning behind it that I would benefit from knowing. Any thoughts, insights and/or opinions would be appreciated.

    Read the article

  • Using 'this': where is good and where is not [closed]

    - by abatishchev
    I like to use 'this' statement for all non-local variables: for properties, for class variables, etc. I do this for code easy reading, easy understanding where from this variable has got. object someVar; object SomeProperty { get; set } void SomeMethod(object arg1, object arg2) { this.SomeProperty = arg1; this.someVar = arg2; } How do you think, what is proper way to use 'this'?

    Read the article

  • Is this a good approach to execute a list of operations on a data structure in Python?

    - by Sridhar Iyer
    I have a dictionary of data, the key is the file name and the value is another dictionary of its attribute values. Now I'd like to pass this data structure to various functions, each of which runs some test on the attribute and returns True/False. One approach would be to call each function one by one explicitly from the main code. However I can do something like this: #MYmodule.py class Mymodule: def MYfunc1(self): ... def MYfunc2(self): ... #main.py import Mymodule ... #fill the data structure ... #Now call all the functions in Mymodule one by one for funcs in dir(Mymodule): if funcs[:2]=='MY': result=Mymodule.__dict__.get(funcs)(dataStructure) The advantage of this approach is that implementation of main class needn't change when I add more logic/tests to MYmodule. Is this a good way to solve the problem at hand? Are there better alternatives to this solution?

    Read the article

  • codingstyle "blanking after open and before close brackets"

    - by Oops
    I really like the "blanking after open and before close brackets"-codingstyle in modern codes Java/C#/C++ . e.g. calling a function: foo(myparam); // versus foo( myparam ); Do you have a better name for this codingstyle? where does it come from? Do you like it either, what is the reason for you to use it or not use it? a few years ago people said "you are blanking" if one has used too much blank space characters in a forumspost or email. many thanks in advance regards Oops edit: two cons, any pros out there?

    Read the article

  • Best way to design a class in python

    - by Fraz
    So, this is more like a philosophical question for someone who is trying to understand classes. Most of time, how i use class is actually a very bad way to use it. I think of a lot of functions and after a time just indent the code and makes it a class and replacing few stuff with self.variable if a variable is repeated a lot. (I know its bad practise) But anyways... What i am asking is: class FooBar: def __init__(self,foo,bar): self._foo = foo self._bar = bar self.ans = self.__execute() def __execute(self): return something(self._foo, self._bar) Now there are many ways to do this: class FooBar: def __init__(self,foo): self._foo = foo def execute(self,bar): return something(self._foo, bar) Can you suggest which one is bad and which one is worse? or any other way to do this. This is just a toy example (offcourse). I mean, there is no need to have a class here if there is one function.. but lets say in __execute something() calls a whole set of other methods.. ?? Thanks

    Read the article

  • Circular increment: Which is "better"?

    - by Helper Method
    When you have a circular buffer represented as an array, and you need the index to wraparound (i.e., when you reach the highest possible index and increment it), is it "better" to: return (i++ == buffer.length) ? 0: i; Or return i++ % buffer.length; Has using the modulo operator any drawbacks? Is it less readable than the first solution?

    Read the article

  • Notepad++ tabs to spaces

    - by Helephant
    Does anyone know how to convert tabs to spaces in Notepad++? I found a webpage that suggests it's possible (http://www.texteditors.info/notepad-replacements-compared.php) but I couldn't find any information about how to do it. I like to be able to do that because some web forms don't respect code with tabs in it.

    Read the article

  • What are the advantages of squashing assignment and error checking in one line?

    - by avakar
    This question is inspired by this question, which features the following code snippet. int s; if((s = foo()) == ERROR) print_error(); I find this style hard to read and prone to error (as the original question demonstrates -- it was prompted by missing parentheses around the assignment). I would instead write the following, which is actually shorter in terms of characters. int s = foo(); if(s == ERROR) print_error(); This is not the first time I've seen this idiom though, and I'm guessing there are reasons (perhaps historical) for it being so often used. What are those reasons?

    Read the article

  • What is a more "ruby way" to write this code?

    - by steadfastbuck
    This was a homework assignment for my students (I am a teaching assistant) in c and I am trying to learn Ruby, so I thought I would code it up. The goal is to read integers from a redirected file and print some simple information. The first line in the file is the number of elements, and then each integer resides on its own line. This code works (although perhaps inefficiently), but how can I make the code more Ruby-like? #!/usr/bin/ruby -w # first line is number of inputs (Don't need it) num_inputs = STDIN.gets.to_i # read inputs as ints h = Hash.new STDIN.each do |n| n = n.to_i h[n] = 1 unless h[n] and h[n] += 1 end # find smallest mode h.sort.each do |k,v| break puts "Mode is: #{k}", "\n" if v == h.values.max end # mode unique? v = h.values.sort print "Mode is unique: " puts v.pop == v.pop, "\n" # print number of singleton odds, # odd elems repeated odd number times in desc order # even singletons in desc order odd_once = 0 odd = Array.new even = Array.new h.each_pair do |k, v| odd_once += 1 if v == 1 and k.odd? odd << k if v.odd? even << k if v == 1 and k.even? end puts "Number of elements with an odd value that appear only once: #{odd_once}", "\n" puts "Elements repeated an odd number of times:" puts odd.sort.reverse, "\n" puts "Elements with an even value that appear exactly once:" puts even.sort.reverse, "\n" # print fib numbers in the hash class Fixnum def is_fib? l, h = 0, 1 while h <= self return true if h == self l, h = h, l+h end end end puts "Fibonacci numbers:" h.keys.sort.each do |n| puts n if n.is_fib? end

    Read the article

  • How to override virtual function in good style? [C++]

    - by Knowing me knowing you
    Hi, guys I know this question is very basic but I've met in few publications (websites, books) different style of override virtual function. What I mean is: if I have base class: class Base { public: virtual void f() = 0; }; in some publications I saw that to override this some authors would just say: void f(); and some would still repeat the virtual keyword before void. Which form of overwriting is in good style? Thank you for your answers.

    Read the article

  • Python indentation in "empty lines"

    - by niscy
    Which is preferred ("." indicating whitespace)? A) def foo(): x = 1 y = 2 .... if True: bar() B) def foo(): x = 1 y = 2 if True: bar() My intuition would be B (that's also what vim does for me), but I see people using A) all the time. Is it just because most of the editors out there are broken?

    Read the article

  • Did anyone created the Java Code Formatter Profile for Eclipse IDE that conforms to the Android Code

    - by yvolk
    Android Code Style Guide defines "Android Code Style Rules". To conform to these rules one have to change quite a number of settings of the Java Code Formatter (Window-Preferences-Java-Formatter) default profile (in Eclipse IDE). Did anyone managed to configure the formatter to follow the "Android Code Style Rules" already? PS: I've tried to do this myself but I've found that there are too many formatter options available, and most of them are not mentioned in the Code Style Guide :-(

    Read the article

  • Ternary operator or chosing from two arrays with the boolean as index

    - by ajax333221
    Which of these lines is more understandable, faster jsPerf, easier to maintain?: arr = bol ? [[-2,1],[-1,2]] : [[-1,0],[-1,1]]; //or arr = [[[-1,0],[-1,1]], [[-2,1],[-1,2]]][bol*1]; I usually write code for computers (not for humans), but this is starting to be a problem when I am not the only one maintaining the code and work for a team. I am unsure, the first example looks neat but are two different arrays, and the second is a single array and seem to transmit what is being done easier. I also considered using an if-else, but I don't like the idea of writing two arr = .... Or are there better options? I need serious guidance, I have never worried about others seeing my code.

    Read the article

  • Are python list comprehensions always a good programming practice?

    - by dln385
    To make the question clear, I'll use a specific example. I have a list of college courses, and each course has a few fields (all of which are strings). The user gives me a string of search terms, and I return a list of courses that match all of the search terms. This can be done in a single list comprehension or a few nested for loops. Here's the implementation. First, the Course class: class Course: def __init__(self, date, title, instructor, ID, description, instructorDescription, *args): self.date = date self.title = title self.instructor = instructor self.ID = ID self.description = description self.instructorDescription = instructorDescription self.misc = args Every field is a string, except misc, which is a list of strings. Here's the search as a single list comprehension. courses is the list of courses, and query is the string of search terms, for example "history project". def searchCourses(courses, query): terms = query.lower().strip().split() return tuple(course for course in courses if all( term in course.date.lower() or term in course.title.lower() or term in course.instructor.lower() or term in course.ID.lower() or term in course.description.lower() or term in course.instructorDescription.lower() or any(term in item.lower() for item in course.misc) for term in terms)) You'll notice that a complex list comprehension is difficult to read. I implemented the same logic as nested for loops, and created this alternative: def searchCourses2(courses, query): terms = query.lower().strip().split() results = [] for course in courses: for term in terms: if (term in course.date.lower() or term in course.title.lower() or term in course.instructor.lower() or term in course.ID.lower() or term in course.description.lower() or term in course.instructorDescription.lower()): break for item in course.misc: if term in item.lower(): break else: continue break else: continue results.append(course) return tuple(results) That logic can be hard to follow too. I have verified that both methods return the correct results. Both methods are nearly equivalent in speed, except in some cases. I ran some tests with timeit, and found that the former is three times faster when the user searches for multiple uncommon terms, while the latter is three times faster when the user searches for multiple common terms. Still, this is not a big enough difference to make me worry. So my question is this: which is better? Are list comprehensions always the way to go, or should complicated statements be handled with nested for loops? Or is there a better solution altogether?

    Read the article

< Previous Page | 44 45 46 47 48 49 50 51 52 53 54 55  | Next Page >